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
24 assert level in [LOG_NONE,LOG_NODE,LOG_VERBOSE]
30 def log(msg,level=LOG_NODE):
31 """Write <msg> to the log file if level >= current log level (default LOG_NODE)."""
32 if (level > LOG_LEVEL):
35 fd = os.open(LOG_FILE, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600)
36 if not msg.endswith('\n'): msg += '\n'
37 os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg))
44 log('running command %s' % ' '.join(args))
45 try: subprocess.call(args, close_fds=True)
48 def log_exc(name = None):
49 """Log the traceback resulting from an exception."""
51 log("operation on %s failed. \n %s" %(self.name, traceback.format_exc()))
53 log(traceback.format_exc())
55 def log_slivers (data):
58 f=open(LOG_SLIVERS,'w')
59 now=time.strftime("GetSlivers stored at %H:%M:%S", time.localtime())
61 pp=pprint.PrettyPrinter(stream=f,indent=2)
65 log_verbose('Cannot save GetSlivers in %s'%LOG_SLIVERS)