1###############################################################################
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###############################################################################
17
18import os
19import sys
20import logging
21import ntpath
22
23try:
24    from StringIO import StringIO
25except ImportError:
26    from io import StringIO
27
28
29def logger_setup(log_dir, log_file, program_name, version, silent):
30    """Setup loggers.
31
32    Two logger are setup which both print to the stdout and a
33    log file when the log_dir is not None. The first logger is
34    named 'timestamp' and provides a timestamp with each call,
35    while the other is named 'no_timestamp' and does not prepend
36    any information. The attribution 'is_silent' is also added
37    to each logger to indicate if the silent flag is thrown.
38
39    Parameters
40    ----------
41    log_dir : str
42        Output directory for log file.
43    log_file : str
44        Desired name of log file.
45    program_name : str
46        Name of program.
47    version : str
48        Program version number.
49    silent : boolean
50        Flag indicating if output to stdout should be suppressed.
51    """
52
53    # setup general properties of loggers
54    timestamp_logger = logging.getLogger('timestamp')
55    timestamp_logger.setLevel(logging.DEBUG)
56    log_format = logging.Formatter(fmt="[%(asctime)s] %(levelname)s: %(message)s",
57                                   datefmt="%Y-%m-%d %H:%M:%S")
58
59    no_timestamp_logger = logging.getLogger('no_timestamp')
60    no_timestamp_logger.setLevel(logging.DEBUG)
61
62    # setup logging to console
63    timestamp_stream_logger = logging.StreamHandler(sys.stdout)
64    timestamp_stream_logger.setFormatter(log_format)
65    timestamp_logger.addHandler(timestamp_stream_logger)
66
67    no_timestamp_stream_logger = logging.StreamHandler(sys.stdout)
68    no_timestamp_stream_logger.setFormatter(None)
69    no_timestamp_logger.addHandler(no_timestamp_stream_logger)
70
71    timestamp_logger.is_silent = False
72    no_timestamp_stream_logger.is_silent = False
73    if silent:
74        timestamp_logger.is_silent = True
75        timestamp_stream_logger.setLevel(logging.ERROR)
76        no_timestamp_stream_logger.is_silent = True
77
78    if log_dir:
79        if not os.path.exists(log_dir):
80            os.makedirs(log_dir)
81        timestamp_file_logger = logging.FileHandler(os.path.join(log_dir, log_file), 'a')
82        timestamp_file_logger.setFormatter(log_format)
83        timestamp_logger.addHandler(timestamp_file_logger)
84
85        no_timestamp_file_logger = logging.FileHandler(os.path.join(log_dir, log_file), 'a')
86        no_timestamp_file_logger.setFormatter(None)
87        no_timestamp_logger.addHandler(no_timestamp_file_logger)
88
89    timestamp_logger.info('%s v%s' % (program_name, version))
90    timestamp_logger.info(ntpath.basename(sys.argv[0]) + ' ' + ' '.join(sys.argv[1:]))
91