Properly add the UUID to all the disks
[ganeti-local] / lib / utils / log.py
index 2ddd0f9..2afbfd2 100644 (file)
@@ -25,6 +25,7 @@
 import os.path
 import logging
 import logging.handlers
+from cStringIO import StringIO
 
 from ganeti import constants
 from ganeti import compat
@@ -51,13 +52,13 @@ class _ReopenableLogHandler(logging.handlers.BaseRotatingHandler):
 
     self._reopen = False
 
-  def shouldRollover(self, _): # pylint: disable-msg=C0103
+  def shouldRollover(self, _): # pylint: disable=C0103
     """Determine whether log file should be reopened.
 
     """
     return self._reopen or not self.stream
 
-  def doRollover(self): # pylint: disable-msg=C0103
+  def doRollover(self): # pylint: disable=C0103
     """Reopens the log file.
 
     """
@@ -70,6 +71,9 @@ class _ReopenableLogHandler(logging.handlers.BaseRotatingHandler):
     # TODO: Handle errors?
     self.stream = open(self.baseFilename, "a")
 
+    # Don't reopen on the next message
+    self._reopen = False
+
   def RequestReopen(self):
     """Register a request to reopen the file.
 
@@ -85,7 +89,7 @@ def _LogErrorsToConsole(base):
   This needs to be in a function for unittesting.
 
   """
-  class wrapped(base): # pylint: disable-msg=C0103
+  class wrapped(base): # pylint: disable=C0103
     """Log handler that doesn't fallback to stderr.
 
     When an error occurs while writing on the logfile, logging.FileHandler
@@ -105,7 +109,7 @@ def _LogErrorsToConsole(base):
       assert not hasattr(self, "_console")
       self._console = console
 
-    def handleError(self, record): # pylint: disable-msg=C0103
+    def handleError(self, record): # pylint: disable=C0103
       """Handle errors which occur during an emit() call.
 
       Try to handle errors with FileHandler method, if it fails write to
@@ -114,13 +118,13 @@ def _LogErrorsToConsole(base):
       """
       try:
         base.handleError(record)
-      except Exception: # pylint: disable-msg=W0703
+      except Exception: # pylint: disable=W0703
         if self._console:
           try:
-            # Ignore warning about "self.format", pylint: disable-msg=E1101
+            # Ignore warning about "self.format", pylint: disable=E1101
             self._console.write("Cannot log message:\n%s\n" %
                                 self.format(record))
-          except Exception: # pylint: disable-msg=W0703
+          except Exception: # pylint: disable=W0703
             # Log handler tried everything it could, now just give up
             pass
 
@@ -266,3 +270,48 @@ def SetupLogging(logfile, program, debug=0, stderr_logging=False,
         raise
 
   return compat.partial(_ReopenLogFiles, reopen_handlers)
+
+
+def SetupToolLogging(debug, verbose, threadname=False,
+                     _root_logger=None, _stream=None):
+  """Configures the logging module for tools.
+
+  All log messages are sent to stderr.
+
+  @type debug: boolean
+  @param debug: Disable log message filtering
+  @type verbose: boolean
+  @param verbose: Enable verbose log messages
+  @type threadname: boolean
+  @param threadname: Whether to include thread name in output
+
+  """
+  if _root_logger is None:
+    root_logger = logging.getLogger("")
+  else:
+    root_logger = _root_logger
+
+  fmt = StringIO()
+  fmt.write("%(asctime)s:")
+
+  if threadname:
+    fmt.write(" %(threadName)s")
+
+  if debug or verbose:
+    fmt.write(" %(levelname)s")
+
+  fmt.write(" %(message)s")
+
+  formatter = logging.Formatter(fmt.getvalue())
+
+  stderr_handler = logging.StreamHandler(_stream)
+  stderr_handler.setFormatter(formatter)
+  if debug:
+    stderr_handler.setLevel(logging.NOTSET)
+  elif verbose:
+    stderr_handler.setLevel(logging.INFO)
+  else:
+    stderr_handler.setLevel(logging.WARNING)
+
+  root_logger.setLevel(logging.NOTSET)
+  root_logger.addHandler(stderr_handler)