Browse Source

Log also to file

Matthias Vogelgesang 7 years ago
parent
commit
96e5f97545
1 changed files with 10 additions and 1 deletions
  1. 10 1
      cockpit

+ 10 - 1
cockpit

@@ -129,10 +129,19 @@ class LogList(LineList):
     def __init__(self, window, colors):
         self.line_list = LineList(window)
         self.c = colors
+        self.log_file = open('cockpit.log', 'a')
 
     def _log_time(self, s, attr):
         timestamp = datetime.datetime.now().strftime('%H:%M:%S')
-        self.line_list.add_line('[{}] {}'.format(timestamp, s), attr)
+        log = '[{}] {}'.format(timestamp, s)
+        self.line_list.add_line(log, attr)
+        self.log_file.write(log)
+
+        if not log.endswith('\n'):
+            self.log_file.write('\n')
+
+        self.log_file.flush()
+        os.fsync(self.log_file.fileno())
 
     def info(self, s):
         self._log_time(s, self.c.get(Colors.NORMAL))