diff options
author | Marco Capitani <[email protected]> | 2018-02-27 14:14:10 +0100 |
---|---|---|
committer | Marco Capitani <[email protected]> | 2018-02-27 17:04:46 +0100 |
commit | 06d2451f950961072a1dbb92e69944334ec966b2 (patch) | |
tree | 39a7aa55d76d7deb43ddc110357e4870211b43ea | |
parent | 0d614408344a210dfe761ba55e7c531ef4d7f252 (diff) | |
download | rumba-06d2451f950961072a1dbb92e69944334ec966b2.tar.gz rumba-06d2451f950961072a1dbb92e69944334ec966b2.zip |
log: add log to file and flush logger functionalities
-rw-r--r-- | rumba/log.py | 41 | ||||
-rw-r--r-- | rumba/ssh_support.py | 16 | ||||
-rw-r--r-- | 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__) |