ForceBalance API  1.3
Automated optimization of force fields and empirical potentials
output.py
Go to the documentation of this file.
1 from logging import *
2 import sys, re
3 
4 class ForceBalanceLogger(Logger):
5  """This logger starts out with a default handler that writes to stdout
6  addHandler removes this default the first time another handler is added.
7  This is used for forcebalance package level logging, where a logger should
8  always be present unless otherwise specified. To silence, add a NullHandler
9  We also by default set the log level to INFO (logging.Logger starts at WARNING)"""
10  def __init__(self, name):
11  super(ForceBalanceLogger, self).__init__(name)
12  self.defaultHandler = RawStreamHandler(sys.stdout)
13  super(ForceBalanceLogger, self).addHandler(self.defaultHandler)
14  self.setLevel(INFO)
15  self.propagate = False
16 
17  def addHandler(self, hdlr):
18  if self.defaultHandler:
19  super(ForceBalanceLogger, self).removeHandler(self.defaultHandler)
20  self.defaultHandler=False
21  super(ForceBalanceLogger, self).addHandler(hdlr)
22 
23  def removeHandler(self, hdlr):
24  super(ForceBalanceLogger, self).removeHandler(hdlr)
25  if len(self.handlers)==0:
26  self.defaultHandler = RawStreamHandler(sys.stdout)
27  super(ForceBalanceLogger, self).addHandler(self.defaultHandler)
28 
29 class RawStreamHandler(StreamHandler):
30  """Exactly like output.StreamHandler except it does no extra formatting
31  before sending logging messages to the stream. This is more compatible with
32  how output has been displayed in ForceBalance. Default stream has also been
33  changed from stderr to stdout"""
34  def __init__(self, stream = sys.stdout):
35  super(RawStreamHandler, self).__init__(stream)
36 
37  def emit(self, record):
38  message = record.getMessage()
39  self.stream.write(message)
40  self.flush()
41 
42 class RawFileHandler(FileHandler):
43  """Exactly like output.FileHandler except it does no extra formatting
44  before sending logging messages to the file. This is more compatible with
45  how output has been displayed in ForceBalance."""
46  def emit(self, record):
47  message = record.getMessage()
48  self.stream.write(message)
49  self.flush()
50 
51 class CleanStreamHandler(StreamHandler):
52  """Similar to RawStreamHandler except it does not write terminal escape codes.
53  Use this for 'plain' terminal output without any fancy colors or formatting"""
54  def __init__(self, stream = sys.stdout):
55  super(CleanStreamHandler, self).__init__(stream)
56 
57  def emit(self, record):
58  message = record.getMessage()
59  message = re.sub("\x1b\[[0-9][0-9]?;?[0-9]?[0-9]?m", "", message)
60  self.stream.write(message)
61  self.flush()
62 
63 class CleanFileHandler(FileHandler):
64  """File handler that does not write terminal escape codes and carriage returns
65  to files. Use this when writing to a file that will probably not be viewed in a terminal"""
66  def emit(self, record):
67  message = record.getMessage()
68  message = re.sub("\x1b\[[0-9][0-9]?;?[0-9]?[0-9]?m", "", message)
69  message = re.sub("\r", "\n", message)
70  self.stream.write(message)
71  self.flush()
72 
73 # set up package level logger that by default prints to sys.stdout
74 setLoggerClass(ForceBalanceLogger)
75 logger=getLogger('forcebalance')
76 logger.setLevel(INFO)
77 
78 class ModLogger(Logger):
79  def error(self, msg, *args, **kwargs):
80  msg = '\n'.join(['\x1b[91m%s\x1b[0m' % s for s in msg.split('\n') if len(s.strip()) > 0])+'\n'
81  for hdlr in (self.parent.handlers if self.propagate else self.handlers):
82  if hasattr(hdlr, 'stream'):
83  hdlr.savestream = hdlr.stream
84  hdlr.stream = sys.stderr
85  super(ModLogger, self).error(msg, *args, **kwargs)
86  for hdlr in (self.parent.handlers if self.propagate else self.handlers):
87  if hasattr(hdlr, 'stream'):
88  hdlr.stream = hdlr.savestream
89 
90 # module level loggers should use the default logger object
91 setLoggerClass(ModLogger)
92 
def addHandler(self, hdlr)
Definition: output.py:18
Exactly like output.FileHandler except it does no extra formatting before sending logging messages to...
Definition: output.py:48
def removeHandler(self, hdlr)
Definition: output.py:24
def emit(self, record)
Definition: output.py:61
def __init__(self, name)
Definition: output.py:11
File handler that does not write terminal escape codes and carriage returns to files.
Definition: output.py:70
def __init__(self, stream=sys.stdout)
Definition: output.py:36
def error(self, msg, args, kwargs)
Definition: output.py:84
Exactly like output.StreamHandler except it does no extra formatting before sending logging messages ...
Definition: output.py:35
Similar to RawStreamHandler except it does not write terminal escape codes.
Definition: output.py:57
def emit(self, record)
Definition: output.py:39
This logger starts out with a default handler that writes to stdout addHandler removes this default t...
Definition: output.py:10