]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/logger.py
0.4.12
[nhentai.git] / nhentai / logger.py
index 88273ab844a3a31c2bcfe2faa64376de54f219cb..01b492993cef3ef9afb27fbb4802f1e729cb647b 100644 (file)
@@ -2,11 +2,22 @@
 # Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
 #
 import logging
-import os
 import re
+import platform
 import sys
 
 
+if platform.system() == 'Windows':
+    import ctypes
+    import ctypes.wintypes
+
+    # Reference: https://gist.github.com/vsajip/758430
+    #            https://github.com/ipython/ipython/issues/4252
+    #            https://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
+    ctypes.windll.kernel32.SetConsoleTextAttribute.argtypes = [ctypes.wintypes.HANDLE, ctypes.wintypes.WORD]
+    ctypes.windll.kernel32.SetConsoleTextAttribute.restype = ctypes.wintypes.BOOL
+
+
 class ColorizingStreamHandler(logging.StreamHandler):
     # color names to indices
     color_map = {
@@ -21,22 +32,13 @@ class ColorizingStreamHandler(logging.StreamHandler):
     }
 
     # levels to (background, foreground, bold/intense)
-    if os.name == 'nt':
-        level_map = {
-            logging.DEBUG: (None, 'white', False),
-            logging.INFO: (None, 'green', False),
-            logging.WARNING: (None, 'yellow', False),
-            logging.ERROR: (None, 'red', False),
-            logging.CRITICAL: ('red', 'white', False)
-        }
-    else:
-        level_map = {
-            logging.DEBUG: (None, 'white', False),
-            logging.INFO: (None, 'green', False),
-            logging.WARNING: (None, 'yellow', False),
-            logging.ERROR: (None, 'red', False),
-            logging.CRITICAL: ('red', 'white', False)
-        }
+    level_map = {
+        logging.DEBUG: (None, 'blue', False),
+        logging.INFO: (None, 'green', False),
+        logging.WARNING: (None, 'yellow', False),
+        logging.ERROR: (None, 'red', False),
+        logging.CRITICAL: ('red', 'white', False)
+    }
     csi = '\x1b['
     reset = '\x1b[0m'
     disable_coloring = False
@@ -46,7 +48,29 @@ class ColorizingStreamHandler(logging.StreamHandler):
         isatty = getattr(self.stream, 'isatty', None)
         return isatty and isatty() and not self.disable_coloring
 
-    if os.name != 'nt':
+    def emit(self, record):
+        try:
+            message = self.format(record)
+            stream = self.stream
+
+            if not self.is_tty:
+                if message and message[0] == "\r":
+                    message = message[1:]
+                stream.write(message)
+            else:
+                self.output_colorized(message)
+            stream.write(getattr(self, 'terminator', '\n'))
+
+            self.flush()
+        except (KeyboardInterrupt, SystemExit):
+            raise
+        except IOError:
+            pass
+        except:
+            self.handleError(record)
+
+
+    if not platform.system() == 'Windows':
         def output_colorized(self, message):
             self.stream.write(message)
     else:
@@ -64,8 +88,6 @@ class ColorizingStreamHandler(logging.StreamHandler):
         }
 
         def output_colorized(self, message):
-            import ctypes
-
             parts = self.ansi_esc.split(message)
             write = self.stream.write
             h = None
@@ -74,14 +96,17 @@ class ColorizingStreamHandler(logging.StreamHandler):
             if fd is not None:
                 fd = fd()
 
-                if fd in (1, 2):  # stdout or stderr
+                if fd in (1, 2): # stdout or stderr
                     h = ctypes.windll.kernel32.GetStdHandle(-10 - fd)
 
             while parts:
                 text = parts.pop(0)
 
                 if text:
-                    write(text)
+                    if sys.version_info < (3, 0, 0):
+                        write(text.encode('utf-8'))
+                    else:
+                        write(text)
 
                 if parts:
                     params = parts.pop(0)
@@ -96,11 +121,11 @@ class ColorizingStreamHandler(logging.StreamHandler):
                             elif 30 <= p <= 37:
                                 color |= self.nt_color_map[p - 30]
                             elif p == 1:
-                                color |= 0x08  # foreground intensity on
-                            elif p == 0:  # reset to default color
+                                color |= 0x08 # foreground intensity on
+                            elif p == 0: # reset to default color
                                 color = 0x07
                             else:
-                                pass  # error condition ignored
+                                pass # error condition ignored
 
                         ctypes.windll.kernel32.SetConsoleTextAttribute(h, color)
 
@@ -134,6 +159,7 @@ class ColorizingStreamHandler(logging.StreamHandler):
         message = logging.StreamHandler.format(self, record)
         return self.colorize(message, record)
 
+
 logging.addLevelName(15, "INFO")
 logger = logging.getLogger('nhentai')
 LOGGER_HANDLER = ColorizingStreamHandler(sys.stdout)