Support for -s. More comments. Removed out of date documentation and bwcap.
[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 = '/root/node_mgr.log'
10
11 def log(msg):
12     """Write <msg> to the log file."""
13     fd = os.open(LOG_FILE, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0600)
14     if not msg.endswith('\n'): msg += '\n'
15     os.write(fd, '%s: %s' % (time.asctime(time.gmtime()), msg))
16     os.close(fd)
17
18 def log_call(*args):
19     log('running command %s' % ' '.join(args))
20     try: subprocess.call(args)
21     except: log_exc()
22
23 def log_exc():
24     """Log the traceback resulting from an exception."""
25     log(traceback.format_exc())