1 # -*- coding: utf-8 -*-
2 from twisted
.python
import log
, failure
3 from audiomangler
.config
import Config
7 from cStringIO
import StringIO
9 from StringIO
import StringIO
11 loglevels
= dict(ERROR
=0, WARNING
=1, INFO
=2, DEBUG
=3)
12 locals().update(loglevels
)
13 loglevels
.update(map(reversed, loglevels
.items()))
15 class FilteredFileLogObserver(log
.FileLogObserver
):
16 def __init__(self
, f
, loglevel
=INFO
):
18 if isinstance(loglevel
, basestring
):
19 loglevel
= loglevels
[loglevel
]
20 self
.loglevel
= loglevel
22 def emit(self
, eventDict
):
23 if '_noignore' not in eventDict
and not eventDict
['isError']: return
24 if eventDict
.get('loglevel', DEBUG
) > self
.loglevel
:
26 encoding
= sys
.stdout
.encoding
27 if eventDict
['isError'] and 'failure' in eventDict
:
28 text
= log
.textFromEventDict(eventDict
)
29 elif eventDict
['message']:
30 text
= ' '.join(s
.encode(encoding
, 'replace') if isinstance(s
, unicode) else s
for s
in eventDict
['message'])
31 elif 'format' in eventDict
or 'consoleformat' in eventDict
:
32 fmt
= eventDict
.get('format', eventDict
.get('consoleformat'))
33 text
= (fmt
% eventDict
).encode(encoding
, 'replace')
35 text
= log
.textFromEventDict(eventDict
)
36 timeStr
= self
.formatTime(eventDict
['time'])
37 self
.output
.write(timeStr
+ ' [' + loglevels
[eventDict
['loglevel']].ljust(7) + '] ' + text
+ '\n')
39 class FilteredConsoleLogObserver
:
40 def __init__(self
, loglevel
=INFO
):
41 self
.loglevel
= loglevel
44 log
.addObserver(self
.emit
)
47 log
.removeObserver(self
.emit
)
49 def emit(self
, eventDict
):
50 if '_noignore' not in eventDict
and not eventDict
['isError']: return
51 if eventDict
.get('loglevel', DEBUG
) > self
.loglevel
:
53 encoding
= sys
.stdout
.encoding
54 if eventDict
['isError'] and 'failure' in eventDict
:
55 text
= log
.textFromEventDict(eventDict
)
56 elif eventDict
['message']:
57 text
= ''.join(s
.encode(encoding
, 'replace') if isinstance(s
, unicode) else s
for s
in eventDict
['message'])
58 elif 'format' in eventDict
or 'consoleformat' in eventDict
:
59 fmt
= eventDict
.get('consoleformat', eventDict
.get('format'))
60 text
= (fmt
% eventDict
).encode(encoding
, 'replace')
62 text
= log
.textFromEventDict(eventDict
)
63 sys
.stdout
.write(text
+ '\n')
70 def get_level(x
, default
=ERROR
):
80 def err(*msg
, **kwargs
):
81 global collector
, logfile
82 if 'nologerror' not in kwargs
:
85 collector
= FilteredFileLogObserver(StringIO(), ERROR
)
90 logfile
= FilteredFileLogObserver(open(Config
.get('logfile', 'audiomangler-%d.log' % os
.getpid()), 'wb'), get_level(Config
['loglevel']))
93 kwargs
['loglevel'] = ERROR
94 if msg
and isinstance(msg
[0], (failure
.Failure
, Exception)):
95 log
.err(_noignore
=1, *msg
, **kwargs
)
97 log
.msg(_noignore
=1, *msg
, **kwargs
)
99 def msg(*msg
, **kwargs
):
101 kwargs
.setdefault('loglevel', DEBUG
)
102 if kwargs
['loglevel'] == ERROR
:
103 err(_noignore
=1, *msg
, **kwargs
)
105 if Config
['logfile'] and logfile
is None and kwargs
['loglevel'] <= get_level(Config
['loglevel']):
107 logfile
= FilteredFileLogObserver(open(Config
['logfile'], 'wb'), Config
['loglevel'])
110 log
.msg(_noignore
=1, *msg
, **kwargs
)
112 def fatal(*msg
, **kwargs
):
121 collector
.output
.flush()
123 text
= collector
.output
.getvalue()
125 print "The following errors occurred:"
127 print "The above errors may also have been reported during processing."
129 logfile
.output
.flush()
131 print "Errors are also recorded in the logfile '%s'." % os
.path
.abspath(logfile
.output
.name
)
135 logout
= FilteredConsoleLogObserver(Config
['consolelevel'])
137 if log
.defaultObserver
:
138 log
.defaultObserver
.stop()
139 log
.defaultObserver
= None
143 atexit
.register(cleanup
)
145 __all__
= ['err', 'msg', 'fatal', 'ERROR', 'WARNING', 'INFO', 'DEBUG']