Add the PYSAWSIM_LOG_LEVEL environmental variable.
[sawsim.git] / pysawsim / __init__.py
1 # Copyright (C) 2010  W. Trevor King <wking@drexel.edu>
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 #
16 # The author may be contacted at <wking@drexel.edu> on the Internet, or
17 # write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
18 # Philadelphia PA 19104, USA.
19
20 """Wrap `sawsim` to facilitate more complicated analysis.
21
22 Testing pysawsim
23 ================
24
25 pysawsim's test framework is build using doctest_, unittest_, and nose_.
26 `nosetests` (from the nose_ package) scans through the source tree,
27 searching out the various tests and running them.  If you aren't
28 familiar with nose_, there is excellent documentation on its home
29 page.  We use nose_ because its auto-discovery allows us to avoid
30 collecting all of our assorted tests into
31 :class:`unittest.TestSuite`\s and running them by hand.
32
33 To run the test suite from the sawsim installation directory, just use::
34
35     nosetests --with-doctest --doctest-tests pysawsim/
36
37 .. _doctest: http://docs.python.org/library/doctest.html
38 .. _unittest: http://docs.python.org/library/unittest.html
39 .. _nose: http://somethingaboutorange.com/mrl/projects/nose/0.11.3/
40
41
42 Adding tests to modules
43 -----------------------
44
45 Just go crazy with doctests and unittests; nose_ will find them.
46 """
47
48 import logging
49 import logging.handlers
50 import sys as _sys
51 import os as _os
52
53
54 _multiprocess_shared_ = True
55 """Allow nosetests to share this module between test processes.
56
57 This module cannot be split because _log setup is not re-entrant.
58 """
59
60 __version__ = '0.10'  # match sawsim version
61
62
63 PYSAWSIM_LOG_LEVEL_MSG = """
64 You can control the log verbosity with the `PYSAWSIM_LOG_LEVEL`
65 environmental variable.  Set it to one of the level names in Python's
66 `logging` module (e.g. `DEBUG`).
67 """
68
69 def log():
70     return logging.getLogger('pysawsim')
71
72 def add_stderr_log_handler(level=None):
73     if level == None:
74         level_string = _os.environ.get('PYSAWSIM_LOG_LEVEL', 'WARNING')
75         try:
76             level = getattr(logging, level_string)
77         except AttributeError:
78             _sys.stderr.write(
79                 'unrecognized PYSAWSIM_LOG_LEVEL: %s\n' % level_string)
80             raise
81     _log = log()
82     _log.setLevel(level)
83     console = logging.StreamHandler()
84     formatter = logging.Formatter('%(name)-8s: %(levelname)-6s %(message)s')
85     console.setFormatter(formatter)
86     _log.addHandler(console)
87
88 add_stderr_log_handler()