f65ab37aa9add6d59276faef7887a86a92295da6
[pypiezo.git] / pypiezo / __init__.py
1 # Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of pypiezo.
4 #
5 # pypiezo is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
9 #
10 # pypiezo is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with pypiezo.  If not, see <http://www.gnu.org/licenses/>.
17
18 import logging as _logging
19 import logging.handlers as _logging_handlers
20
21
22 __version__ = '0.4'
23
24
25 LOG = _logging.getLogger('pypiezo')
26 "Pypiezo logger"
27
28 LOG.setLevel(_logging.WARN)
29 _formatter = _logging.Formatter(
30     '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
31
32 _stream_handler = _logging.StreamHandler()
33 _stream_handler.setLevel(_logging.DEBUG)
34 _stream_handler.setFormatter(_formatter)
35 LOG.addHandler(_stream_handler)
36
37 _syslog_handler = None
38
39
40 from .config import _BaseConfig
41 from .config import find_base_config as _find_base_config
42
43
44 def setup_base_config(config):
45     global base_config, _syslog_handler
46     base_config = config
47
48     LOG.setLevel(base_config['log-level']) 
49
50     if base_config['syslog']:
51         if not _syslog_handler:
52             _syslog_handler = _logging_handlers.SysLogHandler()
53             _syslog_handler.setLevel(_logging.DEBUG)
54         LOG.handlers = [_syslog_handler]
55     else:
56         LOG.handlers = [_stream_handler]
57
58     LOG.info('setup base_config:\n%s' % config.dump())
59
60 def clear_base_config():
61     setup_base_config(_BaseConfig())
62
63 base_config = _find_base_config()
64 setup_base_config(base_config)