[gi-docgen/ebassi/print-lock] Make log.log() thread safe




commit 26e3cb5ddf26bb6f33c2fdf171f409a57364be9e
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sun May 22 13:56:38 2022 +0100

    Make log.log() thread safe
    
    Since print() is not thread safe, we need to make log() a critical path,
    and acquire a lock in order to print out the message.
    
    Fixes: #129

 gidocgen/log.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
---
diff --git a/gidocgen/log.py b/gidocgen/log.py
index e5a2262..3511df5 100644
--- a/gidocgen/log.py
+++ b/gidocgen/log.py
@@ -5,6 +5,7 @@ import os
 import platform
 import sys
 import time
+import threading
 
 
 def setup_output():
@@ -28,6 +29,7 @@ log_quiet = False
 log_fatal_warnings = False
 log_warnings_counter = 0
 log_epoch = 0
+log_lock = threading.Lock()
 
 colors = {
     'NONE': "[0m",
@@ -144,13 +146,14 @@ def log(text, prefix=None, location=None, out=None):
     @location: (optional): a location string, or a Location object
     @out: (optional): a File object
     '''
-    res = []
-    if prefix:
-        res += [str(prefix), ': ']
-    if location:
-        res += [str(location), ' ']
-    res += [text]
-    print(''.join(res), file=out)
+    with log_lock:
+        res = []
+        if prefix:
+            res += [str(prefix), ': ']
+        if location:
+            res += [str(location), ' ']
+        res += [text]
+        print(''.join(res), file=out)
 
 
 def error(text, location=None):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]