From fc6d232a9357211a44dad3300ff64571620aa1bf Mon Sep 17 00:00:00 2001 From: necoro <> Date: Fri, 13 Jul 2007 04:09:51 +0000 Subject: new fancier log output --- portato/helper.py | 69 ++++--------------------------------------------------- 1 file changed, 4 insertions(+), 65 deletions(-) (limited to 'portato/helper.py') diff --git a/portato/helper.py b/portato/helper.py index cc797b9..f25fa34 100644 --- a/portato/helper.py +++ b/portato/helper.py @@ -12,74 +12,13 @@ """ Some nice functions used in the program. - -@var DEBUG: Boolean controlling whether to printout debug messages. -@type DEBUG: boolean """ -import traceback, os.path, sys, types -import os, signal - -DEBUG = True - -def set_debug (d): - """Sets the global DEBUG variable. Do not set it by your own - always use this function. - - @param d: True to enable debugging; False otherwise - @type d: boolean""" - - global DEBUG - DEBUG = d - -def debug(*args, **kwargs): - """Prints a debug message including filename and lineno. - A variable number of positional arguments are allowed. - - If debug(obj0, obj1, obj2) is called, the text part of the output - looks like the output from print obj0, obj1, obj2. - - @keyword name: Use the given name instead the correct function name. - @keyword file: Output file to use. - @keyword minus: The value given is the amount of frames to ignore in the stack to return the correct function call. - This should be used if you are wrapping the debug call. - @keyword warn: Prints the message as a warning. Value of DEBUG is ignored. - @keyword error: Prints the message as an error. Value of DEBUG is ignored.""" - - if not DEBUG and not ("warn" in kwargs or "error" in kwargs): return - - stack = traceback.extract_stack() - minus = -2 - if "minus" in kwargs: - minus = minus - kwargs["minus"] - a, b, c, d = stack[minus] - a = os.path.basename(a) - out = [] - for obj in args: - out.append(str(obj)) - text = ' '.join(out) - if "name" in kwargs: - text = 'In %s (%s:%s): %s' % (kwargs["name"], a, b, text) - else: - text = 'In %s (%s:%s): %s' % (c, a, b, text) - - outfile = sys.stdout - surround = "DEBUG" +import types, os, signal, logging +from logging import debug, info, warning, error, critical, exception - if "warn" in kwargs: - outfile = sys.stderr - surround = "WARNING" - elif "error" in kwargs: - outfile = sys.stderr - surround = "ERROR" - - text = ("***%s*** %s ***%s***" % (surround, text, surround)) - - if "file" in kwargs: - f = open(kwargs["file"], "a+") - f.write(text+"\n") - f.close() - else: - print >> outfile, text +def set_log_level (lvl): + logging.getLogger().setLevel(lvl) def send_signal_to_group (sig): """Sends a signal to all processes of our process group (w/o ourselves). -- cgit v1.2.3