Take the new doc out of the branch and into trunk
[nodemanager.git] / logger.py
1 """A very simple logger that tries to be concurrency-safe."""
2
3 import os, sys
4 import subprocess
5 import time
6 import traceback
7
8
9 LOG_FILE = '/var/log/nm'
10
11 def log(msg):
12     """Write <msg> to the log file."""
13     try:
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     except OSError:
19         sys.stderr.write(msg)
20         sys.stderr.flush()
21
22 def log_call(*args):
23     log('running command %s' % ' '.join(args))
24     try: subprocess.call(args, close_fds=True)
25     except: log_exc()
26
27 def log_exc():
28     """Log the traceback resulting from an exception."""
29     log(traceback.format_exc())