+import subprocess
+import select
+
+LOG_FILE = '/var/log/nodemanager'
+LOG_SLIVERS = '/var/lib/nodemanager/getslivers.txt'
+LOG_DATABASE = '/var/lib/nodemanager/database.txt'
+
+# basically define 3 levels
+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=LOG_NODE
+
+def set_level(level):
+ global LOG_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)
+
+def log(msg,level=LOG_NODE):
+ """Write <msg> to the log file if level >= current log level (default LOG_NODE)."""
+ if (level > LOG_LEVEL):
+ return
+ try:
+ fd = os.open(LOG_FILE, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600)
+ if not msg.endswith('\n'): msg += '\n'
+ os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg))
+ os.close(fd)
+ except OSError:
+ sys.stderr.write(msg)
+ sys.stderr.flush()
+
+date_width=24
+def log_exc(msg="",name=None):
+ """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
+# data from PLC cannot be reached
+# we show this message instead of the exception stack instead in this case
+def log_missing_data (msg,key):
+ log("%s: could not find the %s key in data (PLC connection down?) - IGNORED"%(msg,key))
+
+def log_data_in_file (data, file, message="",level=LOG_NODE):
+ if (level > LOG_LEVEL):
+ return
+ import pprint, time
+ try:
+ f=open(file,'w')
+ now=time.strftime("Last update: %Y.%m.%d at %H:%M:%S %Z", time.localtime())
+ f.write(now+'\n')
+ if message: f.write('Message:'+message+'\n')
+ pp=pprint.PrettyPrinter(stream=f,indent=2)
+ pp.pprint(data)
+ f.close()
+ verbose("logger:.log_data_in_file Owerwrote %s"%file)
+ except:
+ log_exc('logger.log_data_in_file failed - file=%s - message=%r'%(file,message))
+
+def log_slivers (data):
+ log_data_in_file (data, LOG_SLIVERS, "raw GetSlivers")
+def log_database (db):
+ log_data_in_file (db, LOG_DATABASE, "raw database")