4 """A very simple logger that tries to be concurrency-safe."""
12 LOG_FILE = '/var/log/nm'
13 LOG_SLIVERS = '/var/log/getslivers.txt'
15 # Thierry - trying to debug this for 4.2
16 # basically define 3 levels
20 # default is to log a reasonable amount of stuff for when running on operational nodes
25 assert level in [LOG_NONE,LOG_NODE,LOG_VERBOSE]
29 log('(v) '+msg,LOG_VERBOSE)
31 def log(msg,level=LOG_NODE):
32 """Write <msg> to the log file if level >= current log level (default LOG_NODE)."""
33 if (level > LOG_LEVEL):
36 fd = os.open(LOG_FILE, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600)
37 if not msg.endswith('\n'): msg += '\n'
38 os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg))
44 def log_exc(msg="",name=None):
45 """Log the traceback resulting from an exception."""
47 log("%s: EXCEPTION caught <%s> \n %s" %(name, msg, traceback.format_exc()))
49 log("EXCEPTION caught <%s> \n %s" %(msg, traceback.format_exc()))
51 ########## snapshot data to a file
52 # for some reason the various modules are still triggered even when the
53 # data from PLC cannot be reached
54 # we show this message instead of the exception stack instead in this case
55 def log_missing_data (msg,key):
56 log("%s: could not find the %s key in data (PLC connection down?) - IGNORED"%(msg,key))
58 def log_data_in_file (data, file, message=""):
62 now=time.strftime("Last update: %Y.%m.%d at %H:%M:%S %Z", time.localtime())
64 if message: f.write('Message:'+message+'\n')
65 pp=pprint.PrettyPrinter(stream=f,indent=2)
69 log_verbose('log_data_in_file failed - file=%s - message=%r'%(file,message))
71 def log_slivers (data):
72 log_data_in_file (data, LOG_SLIVERS, "raw GetSlivers")
74 #################### child processes
75 # avoid waiting until the process returns;
76 # that makes debugging of hanging children hard
79 def __init__ (self,message='log_call: '):
85 if c=='\n': self.flush()
89 log (self.message + self.buffer)
92 # time out in seconds - avoid hanging subprocesses - default is 5 minutes
93 default_timeout_minutes=5
95 def log_call(command,timeout=default_timeout_minutes*60,poll=1):
96 message=" ".join(command)
97 log("log_call: running command %s" % message)
98 verbose("log_call: timeout=%r s" % timeout)
99 verbose("log_call: poll=%r s" % poll)
100 trigger=time.time()+timeout
102 child = subprocess.Popen(command, bufsize=1,
103 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
106 # see if anything can be read within the poll interval
107 (r,w,x)=select.select([child.stdout],[],[],poll)
108 if r: buffer.add(child.stdout.read(1))
110 returncode=child.poll()
112 if returncode != None:
114 # child is done and return 0
116 log("log_call: command completed (%s)" % message)
120 log("log_call: command return=%d (%s)" %(returncode,message))
121 raise Exception("log_call: failed with returncode %d"%returncode)
122 # no : still within timeout ?
123 if time.time() >= trigger:
126 raise Exception("log_call: terminated command - exceeded timeout %d s"%timeout)
127 except: log_exc("failed to run command %s" % message)