]> mj.ucw.cz Git - moe.git/blob - t/moe/log.py
Config parser complete, but completely untested.
[moe.git] / t / moe / log.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 import traceback
6
7 orig_stdout_fd = os.dup(1)
8 orig_stderr_fd = os.dup(2)
9 progress_file = os.fdopen(orig_stdout_fd, "w", 0)
10
11 class MoeLog:
12
13     def __init__(self):
14         self.verbosity = 0
15         self.progress_file = progress_file
16         self.log_file = None
17
18     def open(self, name):
19         self.log_file = open(name, "w")
20         os.dup2(self.log_file.fileno(), 1)
21         os.dup2(self.log_file.fileno(), 2)
22
23     def say(self, msg):
24         if self.log_file:
25             self.log_file.write(msg)
26
27     def verbose(self, msg):
28         if self.verbosity and self.log_file:
29             self.log_file.write(msg)
30
31     def progress(self, msg):
32         if self.progress_file:
33             self.progress_file.write(msg)
34
35     def shout(self, msg):
36         self.say(msg)
37         self.progress(msg)
38
39     def flush(self):
40         self.log_file.flush()
41
42 default = MoeLog()
43
44 def fatal_exception():
45     os.dup2(orig_stderr_fd, 2)
46     traceback.print_exc()
47     sys.exit(1)