-# $Id$
-# $URL$
"""A very simple logger that tries to be concurrency-safe."""
import subprocess
import select
-LOG_FILE = '/var/log/nm'
-LOG_SLIVERS = '/var/log/getslivers.txt'
+LOG_FILE = '/var/log/nodemanager'
+LOG_SLIVERS = '/var/lib/nodemanager/getslivers.txt'
+LOG_DATABASE = '/var/lib/nodemanager/database.txt'
-# Thierry - trying to debug this for 4.2
# basically define 3 levels
LOG_NONE=0
LOG_NODE=1
def log_exc(msg="",name=None):
"""Log the traceback resulting from an exception."""
- if name:
+ 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()))
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=""):
+def log_data_in_file (data, file, message="",level=LOG_NODE):
+ if (level > LOG_LEVEL):
+ return
import pprint, time
try:
f=open(file,'w')
pp=pprint.PrettyPrinter(stream=f,indent=2)
pp.pprint(data)
f.close()
+ verbose("logger:.log_data_in_file Owerwrote %s"%file)
except:
- log_verbose('log_data_in_file failed - file=%s - message=%r'%(file,message))
+ 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")
#################### 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()
# time out in seconds - avoid hanging subprocesses - default is 5 minutes
default_timeout_minutes=5
+# returns a bool that is True when everything goes fine and the retcod is 0
def log_call(command,timeout=default_timeout_minutes*60,poll=1):
message=" ".join(command)
log("log_call: running command %s" % message)
verbose("log_call: timeout=%r s" % timeout)
verbose("log_call: poll=%r s" % poll)
trigger=time.time()+timeout
- try:
- child = subprocess.Popen(command, bufsize=1,
+ result = False
+ try:
+ child = subprocess.Popen(command, bufsize=1,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
buffer = Buffer()
while True:
if returncode != None:
buffer.flush()
# child is done and return 0
- if returncode == 0:
- log("log_call: command completed (%s)" % message)
+ if returncode == 0:
+ log("log_call:end command (%s) completed" % message)
+ result=True
break
# child has failed
else:
- log("log_call: command return=%d (%s)" %(returncode,message))
- raise Exception("log_call: failed with returncode %d"%returncode)
+ log("log_call:end command (%s) returned with code %d" %(message,returncode))
+ break
# no : still within timeout ?
if time.time() >= trigger:
buffer.flush()
child.terminate()
- raise Exception("log_call: terminated command - exceeded timeout %d s"%timeout)
+ log("log_call:end terminating command (%s) - exceeded timeout %d s"%(message,timeout))
+ break
except: log_exc("failed to run command %s" % message)
-
+ return result