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