From 06d2451f950961072a1dbb92e69944334ec966b2 Mon Sep 17 00:00:00 2001 From: Marco Capitani Date: Tue, 27 Feb 2018 14:14:10 +0100 Subject: log: add log to file and flush logger functionalities --- rumba/log.py | 41 +++++++++++++++++++++++++++++++++++++++++ rumba/ssh_support.py | 16 ++++++++++++---- rumba/utils.py | 16 ++++++++++++---- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/rumba/log.py b/rumba/log.py index 893f5f7..6c9cd10 100644 --- a/rumba/log.py +++ b/rumba/log.py @@ -27,8 +27,11 @@ import logging import logging.handlers import multiprocessing +import os import sys +import time + DEBUG = logging.DEBUG INFO = logging.INFO @@ -277,6 +280,10 @@ def reset_logging_level(): set_logging_level(logging.ERROR, '') +def flush_log(): + time.sleep(0.1) + + def flush_and_kill_logging(): """ Flushes all queued log messages and stops the logging facility. @@ -286,3 +293,37 @@ def flush_and_kill_logging(): """ global logging_listener logging_listener.stop() + + +class LogOptions(object): + + @staticmethod + def _get_handlers(): + global logging_listener + return tuple(logging_listener.handlers) + + @staticmethod + def _set_handlers(*handlers): + global logging_listener + logging_listener.handlers = handlers + + def _add_handler(self, handler): + handler.setFormatter(RumbaFormatter()) + handlers = self._get_handlers() + (handler,) + self._set_handlers(*handlers) + return self + + def log_to_file(self, path='rumba.log'): + new_handler = logging.handlers.RotatingFileHandler(path) + return self._add_handler(new_handler) + + def reset_logging(self): + self._set_handlers(*tuple()) + return self + + def log_to_console(self): + new_handler = logging.StreamHandler(sys.stdout) + return self._add_handler(new_handler) + + +options = LogOptions() # module instance used for configuration diff --git a/rumba/ssh_support.py b/rumba/ssh_support.py index 98caca2..88334ea 100644 --- a/rumba/ssh_support.py +++ b/rumba/ssh_support.py @@ -31,11 +31,19 @@ import time import rumba.log as log -# Fix Python 2.x. +# Fix input reordering try: - input = raw_input -except NameError: - pass + import builtins # Only in Python 3 + + def input(prompt=''): + log.flush_log() + return builtins.input(prompt) +except ImportError: # We are in Python 2 + import __builtin__ + + def input(prompt=''): + log.flush_log() + return __builtin__.raw_input(prompt) logger = log.get_logger(__name__) diff --git a/rumba/utils.py b/rumba/utils.py index 7e3ec80..33ba71c 100644 --- a/rumba/utils.py +++ b/rumba/utils.py @@ -29,11 +29,19 @@ import os import rumba.log as log import rumba.model as model -# Fix Python 2.x. +# Fix input reordering try: - input = raw_input -except NameError: - pass + import builtins # Only in Python 3 + + def input(prompt=''): + log.flush_log() + return builtins.input(prompt) +except ImportError: # We are in Python 2 + import __builtin__ + + def input(prompt=''): + log.flush_log() + return __builtin__.raw_input(prompt) logger = log.get_logger(__name__) -- cgit v1.2.3