Johannes Köster avatar Johannes Köster committed 9bd5924

Simplified logging decoration.

Comments (0)

Files changed (1)

snakemake/logging.py

 
     def __init__(self, nocolor=False, stream=sys.stderr, timestamp=False):
         super().__init__(stream=stream)
-        self.nocolor = nocolor
+        self.nocolor = nocolor or not self.is_tty or platform.system() == 'Windows'
         self.timestamp = timestamp
 
     @property
         return isatty and isatty()
 
     def emit(self, record):
-        try:
-            self.format(record)  # add the message to the record
-            self._output_lock.acquire()
-            if self.is_tty:
-                self.stream.write(self.colorize(record))
-            else:
-                self.stream.write(record.message)
-            self.stream.write(getattr(self, 'terminator', '\n'))
-            self.flush()
-            self._output_lock.release()
-        except (KeyboardInterrupt, SystemExit):
-            raise
-        except:
-            self.handleError(record)
-            self._output_lock.release()
+        with self._output_lock:
+            try:
+                self.format(record)  # add the message to the record
+                self.stream.write(self.decorate(record))
+                self.stream.write(getattr(self, 'terminator', '\n'))
+                self.flush()
+            except (KeyboardInterrupt, SystemExit):
+                raise
+            except:
+                self.handleError(record)
 
-    def colorize(self, record):
-        if (not self.nocolor
-            and record.levelname in self.colors
-            and platform.system() != 'Windows'):
-            fmt_dict = dict(
-                color=self.COLOR_SEQ % (30 + self.colors[record.levelname]),
-                message=record.message,
-                reset=self.RESET_SEQ
-            )
-            if self.timestamp:
-                return "{color}[{time}] {message}{reset}".format(
-                    time=time.asctime(),
-                    **fmt_dict)
-            else:
-                    return "{color}{message}{reset}".format(**fmt_dict)
-        if not self.timestamp:
-            return record.message
-        return "[{time}] {message}".format(
-            time=time.asctime(),
-            message=record.message)
+    def decorate(self, record):
+        message = [record.message]
+        if self.timestamp:
+            message.insert(0, "[{}] ".format(time.asctime()))
+        if not self.nocolor and record.levelname in self.colors:
+            message.insert(0, self.COLOR_SEQ % (30 + self.colors[record.levelname]))
+            message.append(self.RESET_SEQ)
+        return "".join(message)
 
 logger = logging.getLogger(__name__)
 handler = None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.