X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=logger.py;h=195c1d882fb73fc5729c79fa0777ad97c1fea0b9;hb=refs%2Fheads%2F1.5;hp=3411df50a8e4bd0b35521ca3c4c1a4890875601a;hpb=aac3e5d7c3443d6e1cb33525aefad35be5fe077a;p=nodemanager.git diff --git a/logger.py b/logger.py index 3411df5..195c1d8 100644 --- a/logger.py +++ b/logger.py @@ -1,27 +1,35 @@ -import fcntl -import os +# +# Something relevant +# +"""A very simple logger that tries to be concurrency-safe.""" + +import os, sys import subprocess import time import traceback -from config import LOG_FILE +LOG_FILE = '/var/log/nm' def log(msg): """Write to the log file.""" - # the next three lines ought to be an atomic operation but aren't - fd = os.open(LOG_FILE, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600) - flags = fcntl.fcntl(fd, fcntl.F_GETFD) - fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC) - if not msg.endswith('\n'): msg += '\n' - os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg)) - os.close(fd) + 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() def log_call(*args): log('running command %s' % ' '.join(args)) - try: subprocess.call(args) + try: subprocess.call(args, close_fds=True) except: log_exc() -def log_exc(): +def log_exc(name = None): """Log the traceback resulting from an exception.""" - log(traceback.format_exc()) + if name: + log("operation on %s failed. \n %s" %(self.name, traceback.format_exc())) + else: + log(traceback.format_exc())