Refactoring in progress...
[nodemanager.git] / logger.py
1 """A very simple logger that tries to be concurrency-safe."""
2
3 import os
4 import subprocess
5 import time
6 import traceback
7
8
9 LOG_FILE = '/var/log/pl_node_mgr.log'
10
11
12 def log(msg):
13     """Write <msg> to the log file."""
14     fd = os.open(LOG_FILE, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600)
15     if not msg.endswith('\n'): msg += '\n'
16     os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg))
17     os.close(fd)
18
19 def log_call(*args):
20     log('running command %s' % ' '.join(args))
21     try: subprocess.call(args)
22     except: log_exc()
23
24 def log_exc():
25     """Log the traceback resulting from an exception."""
26     log(traceback.format_exc())