X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=logger.py;h=b38c872b13760715c5c7e5ae23fd37c6e61340ad;hb=ecee05390277f57b02d21ffca0195292bde1defa;hp=f0088064320916cf48f8d6492a13d5fd0d579b93;hpb=33bb6d66fd29039576844c49ceceefe905d68fd3;p=nodemanager.git diff --git a/logger.py b/logger.py index f008806..b38c872 100644 --- a/logger.py +++ b/logger.py @@ -1,5 +1,3 @@ -# $Id$ -# $URL$ """A very simple logger that tries to be concurrency-safe.""" @@ -18,12 +16,15 @@ LOG_NONE=0 LOG_NODE=1 LOG_VERBOSE=2 # default is to log a reasonable amount of stuff for when running on operational nodes -LOG_LEVEL=1 +LOG_LEVEL=LOG_NODE def set_level(level): global LOG_LEVEL - assert level in [LOG_NONE,LOG_NODE,LOG_VERBOSE] - LOG_LEVEL=level + try: + assert level in [LOG_NONE,LOG_NODE,LOG_VERBOSE] + LOG_LEVEL=level + except: + logger.log("Failed to set LOG_LEVEL to %s"%level) def verbose(msg): log('(v) '+msg,LOG_VERBOSE) @@ -41,12 +42,25 @@ def log(msg,level=LOG_NODE): sys.stderr.write(msg) sys.stderr.flush() +date_width=24 def log_exc(msg="",name=None): - """Log the traceback resulting from an exception.""" - if name: - log("%s: EXCEPTION caught <%s> \n %s" %(name, msg, traceback.format_exc())) - else: - log("EXCEPTION caught <%s> \n %s" %(msg, traceback.format_exc())) + """Log traceback resulting from an exception.""" + printout="" + if name: printout += "%s: "%name + printout += "EXCEPTION caught <%s> \n" %msg + for frame in traceback.format_exc().split("\n"): + printout+=(date_width+2)*" "+"%s\n"%frame + log(printout) + +def log_trace(msg="",name=None): + """Log current stack""" + printout="" + if name: printout += "%s: "%name + printout += "LOGTRACE\n" + for frame in traceback.format_stack(): + printout += "..."+frame + log(printout) + ########## snapshot data to a file # for some reason the various modules are still triggered even when the @@ -67,7 +81,7 @@ def log_data_in_file (data, file, message="",level=LOG_NODE): pp=pprint.PrettyPrinter(stream=f,indent=2) pp.pprint(data) f.close() - log("logger:.log_data_in_file Owerwrote %s"%file) + verbose("logger:.log_data_in_file Owerwrote %s"%file) except: log_exc('logger.log_data_in_file failed - file=%s - message=%r'%(file,message)) @@ -77,14 +91,14 @@ def log_database (db): log_data_in_file (db, LOG_DATABASE, "raw database") #################### child processes -# avoid waiting until the process returns; +# avoid waiting until the process returns; # that makes debugging of hanging children hard class Buffer: def __init__ (self,message='log_call: '): self.buffer='' self.message=message - + def add (self,c): self.buffer += c if c=='\n': self.flush() @@ -105,8 +119,8 @@ def log_call(command,timeout=default_timeout_minutes*60,poll=1): verbose("log_call: poll=%r s" % poll) trigger=time.time()+timeout result = False - try: - child = subprocess.Popen(command, bufsize=1, + try: + child = subprocess.Popen(command, bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) buffer = Buffer() while True: @@ -119,7 +133,7 @@ def log_call(command,timeout=default_timeout_minutes*60,poll=1): if returncode != None: buffer.flush() # child is done and return 0 - if returncode == 0: + if returncode == 0: log("log_call:end command (%s) completed" % message) result=True break