From b6162ec4a10430dc3d5ea387a8b095239ce71f8f Mon Sep 17 00:00:00 2001 From: hatRiot Date: Tue, 2 Sep 2014 22:42:18 -0600 Subject: [PATCH] Added color and logging support for nodes * node/src/core/utility.py -- Added a platform check to colorize output -- Added a log and debug check -- We actually write output to a file now --- node/src/core/utility.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/node/src/core/utility.py b/node/src/core/utility.py index 3f31551..69d7a9c 100644 --- a/node/src/core/utility.py +++ b/node/src/core/utility.py @@ -1,21 +1,36 @@ from datetime import date, datetime from src.core.log import * +import platform +import config def msg(string, level=INFO): + """ Handle messages; this takes care of logging and + debug checking, as well as output colors """ - """ - if level is INFO: - print '\033[32m[%s] %s\033[0m' % (timestamp(), string) - elif level is DEBUG: - print '\033[34m[%s] %s\033[0m' % (timestamp(), string) - elif level is ERROR: - print '\033[31m[%s] %s\033[0m' % (timestamp(), string) + string = "[%s] %s" % (timestamp(), string) + if 'linux' in platform.platform().lower(): + if level is INFO: + color_string = '%s%s%s' % ('\033[32m', string, '\033[0m') + elif level is DEBUG: + color_string = '%s%s%s' % ('\033[34m', string, '\033[0m') + elif level is ERROR: + color_string = '%s%s%s' % ('\033[31m', string, '\033[0m') + else: + color_string = string + + if level is DEBUG and not config.DEBUG: + return + + if not level is LOG: + print color_string + + log(string) def timestamp(): - """ + """ Generate a timestamp """ return '%s %s' % (date.today().isoformat(), @@ -26,4 +41,5 @@ def log(string): """ Log string to file """ - pass + with open(config.LOG_FILE, "a+") as f: + f.write("[%s] %s\n" % (timestamp(), string)) \ No newline at end of file