- write log messages to stderr if log file is inaccessible
authorMark Huang <mlhuang@cs.princeton.edu>
Tue, 31 Oct 2006 23:09:58 +0000 (23:09 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Tue, 31 Oct 2006 23:09:58 +0000 (23:09 +0000)
logger.py

index e59ca1c..e4c3639 100644 (file)
--- a/logger.py
+++ b/logger.py
@@ -1,6 +1,6 @@
 """A very simple logger that tries to be concurrency-safe."""
 
-import os
+import os, sys
 import subprocess
 import time
 import traceback
@@ -10,10 +10,14 @@ LOG_FILE = '/root/node_mgr.log'
 
 def log(msg):
     """Write <msg> to the log file."""
-    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)
+    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))