summaryrefslogtreecommitdiff
path: root/taler/util/gnunet_log.py
blob: 9debe4e5f196002552e06ab1f0f20243405b8fe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# This file is part of TALER
# (C) 2014, 2015, 2016, 2019 Taler Systems SA
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later
#  version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free
#  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA  02110-1301  USA
#
# @author Marcello Stanisci
# @brief Implementation of the GNUnet logging logic.
#
#!/usr/bin/env python3

# GNUNET_FORCE_LOG format [component];[file];[function];[from line [to line]];loglevel

import os
import re
import logging
import datetime
import inspect


##
# Represent a definition for one logging action.
class LogDefinition:

    ##
    # Init constructor.
    #
    # @param self the object itself.
    # @param component which component this definition is going to affect.
    # @param filename which filename this definition is going to affect.
    # @param function which function this definition is going to affect.
    # @param line_interval which line_interval this definition is going to affect.
    # @param loglevel which loglevel is accepted.
    # @param forced does this definition come from GNUNET_FORCE_LOG?
    def __init__(self, component, filename, function, line_interval, loglevel, forced):
        self.forced = forced
        self.component = ".*" if "" == component else component
        self.filename = ".*" if "" == filename else filename
        self.function = ".*" if "" == function else function
        self.line_interval = self.__parse_line_interval(line_interval)

        # string here
        self.loglevel = loglevel

    ##
    # Parse the @a line_interval from a logging definition.
    #
    # @param self the object itself.
    # @param line_interval the line interval value as it comes
    #        from the user definition.  The format is X[-Y].
    # @return a dict with min/max fields; if max is not given,
    #         then min == max.  If the input is wrong, then just
    #         match every line.
    def __parse_line_interval(self, line_interval):
        interval_re = "^([0-9]+)(-([0-9]+))?$"
        match = re.match(interval_re, line_interval)
        if match:
            return dict(
                min=int(match.group(1)),
                max=int(match.group(3) if match.group(3) else match.group(1)),
            )

        # just match every line if bad interval was provided.
        return dict(min=0, max=float("inf"))


##
# Represent a loglevel.
#
# @param self the object itself.
# @param string the loglevel given as a string (DEBUG/ERROR/WARNING/...)
# @param level the loglevel given as for the 'logging' module API.
# @param function the native function from 'logging' module that
#        _actually_ outputs the log line.
class GnunetLoglevel:
    def __init__(self, string, level, function):
        self.string = string
        self.level = level
        self.function = function

    def __str__(self):
        return self.string

    def getLevel(self):
        return self.level

    def getFunction(self):
        return self.function


##
# The "mother" class of this module.  This class is in charge of
# parsing the definitions given by the user, from all the streams:
# being it programmatical, or the environment.  It is also in charge
# of giving the right precedence to the streams: e.g. GNUNET_FORCE_LOG
# takes precedence over the "setup()" method.
class GnunetLogger:

    COMPONENT_IDX = 0
    FILENAME_IDX = 1
    FUNCTION_IDX = 2
    LINE_INTERVAL = 3
    LEVEL_IDX = 4

    ##
    # Init constructor.
    #
    # @param self the object itself.
    # @param component the component name, that will be fed
    #        to the native 'logging' API.
    def __init__(self, component):
        self.logger = logging.getLogger(component)
        self.ERROR = GnunetLoglevel("ERROR", logging.ERROR, self.logger.error)
        self.WARNING = GnunetLoglevel("WARNING", logging.WARNING, self.logger.warning)
        self.INFO = GnunetLoglevel("INFO", logging.INFO, self.logger.info)
        self.DEBUG = GnunetLoglevel("DEBUG", logging.DEBUG, self.logger.debug)

        self.component = component
        self.loglevel = None

        # Setting the *logging* loglevel in order to have the
        # chance of changing the *logger* (object) loglevel along the
        # execution.  So this particular loglevel has no relevance
        # (might have been any other loglevel).
        logging.basicConfig(level=logging.INFO)

        self.no_forced_definitions = True
        self.definitions = list()

        # Give priority to the forced env.
        if os.environ.get("GNUNET_FORCE_LOG"):
            self.no_forced_definitions = False
            self.__parse_definitions(os.environ.get("GNUNET_FORCE_LOG"), True)

        if os.environ.get("GNUNET_LOG"):
            self.__parse_definitions(os.environ.get("GNUNET_LOG"), False)

        if os.environ.get("GNUNET_FORCE_LOGFILE"):
            filename = self.parse_filename(os.environ.get("GNUNET_FORCE_LOGFILE"))
            fh = logging.FileHandler(filename)
            self.logger.addHandler(fh)

    ##
    # Parse the filename where to write log lines.
    #
    # @param self the object itself.
    # @param filename the filename to parse (usually given
    #        to the '-l' option).
    # @return the parse filename string (with all the dates
    #         placeholders interpreted.)
    def parse_filename(self, filename):
        # implement {} and [] substitution.
        f = filename.replace("{}", self.component)
        f = f.replace("[]", str(os.getpid()))
        now = datetime.datetime.now()
        f = f.replace("%Y", now.strftime("%Y"))
        f = f.replace("%m", now.strftime("%m"))
        f = f.replace("%d", now.strftime("%d"))
        return f

    ##
    # Maps loglevels as strings, to loglevels as defined
    # in _this_ object.
    #
    # @param self the object itself.
    # @param level the string to map.
    # @return the loglevel native of _this_ object; defaults
    #         to INFO, if not found in the map.
    def string_to_loglevel(self, level):
        level_map = {
            "ERROR": self.ERROR,
            "WARNING": self.WARNING,
            "INFO": self.INFO,
            "DEBUG": self.DEBUG,
        }

        # Defaults to INFO.
        return level_map.get(level, self.INFO)

    ##
    # Set the loglevel for this module.
    def setup(self, loglevel):
        self.loglevel = loglevel

    ##
    # Log API for users to produce logs.
    #
    # @param self the object itself.
    # @param message the message to log.
    # @param message_loglevel the loglevel associated with the message.
    def log(self, message, message_loglevel):
        caller_frame = inspect.stack()[1]

        filename = os.path.basename(inspect.getfile(caller_frame[0]))
        lineno = caller_frame.lineno
        function = caller_frame.function

        # Ordinary case (level setup + nothing forced).
        if self.loglevel and self.no_forced_definitions:
            self.logger.setLevel(level=self.loglevel.getLevel())
            message_loglevel.getFunction()(message)
            return

        # We crawl through GNUNET_FORCE_LOG definitions,
        # or GNUNET_LOG (in case of non-forced definition
        # and non-given loglevel at object creation time)
        for defi in self.definitions:
            if defi.forced or not self.loglevel:
                if (
                    re.match(defi.component, self.component)
                    and re.match(defi.filename, filename)
                    and re.match(defi.function, function)
                    and defi.line_interval["min"] <= lineno <= defi.line_interval["max"]
                ):
                    self.logger.setLevel(
                        level=self.string_to_loglevel(defi.loglevel).getLevel()
                    )
                    message_loglevel.getFunction()(message)
                    return

        # If the flow got here, then one of the following
        # may hold.
        #
        # (1) GNUNET_FORCE_LOG was given and no definition was
        #     found for this component (neither forced nor unforced).
        #     Possibly, there also exists a default loglevel.

        if self.loglevel:
            self.logger.setLevel(level=self.loglevel.getLevel())

        # (2) GNUNET_FORCE_LOG was NOT given and neither was
        #     a default loglevel, and also a unforced definition
        #     wasn't found.  Must assign a made-up loglevel.

        else:
            self.logger.setLevel(level=logging.INFO)

        message_loglevel.getFunction()(message)

    ##
    # Helper function that parses definitions coming from the environment.
    #
    # @param self the object itself.
    # @param line the definition coming from the environment.
    # @param forced whether the definition comes from GNUNET_FORCE_LOG or not.
    def __parse_definitions(self, line, forced):
        gfl_split = line.split("/")
        for component in gfl_split:
            gfl_split_split = component.split(";")

            if 5 != len(gfl_split_split):
                print("warning: GNUNET_(FORCE_)LOG is malformed")
                return

            definition = LogDefinition(
                gfl_split_split[GnunetLogger.COMPONENT_IDX],
                gfl_split_split[GnunetLogger.FILENAME_IDX],
                gfl_split_split[GnunetLogger.FUNCTION_IDX],
                gfl_split_split[GnunetLogger.LINE_INTERVAL],
                gfl_split_split[GnunetLogger.LEVEL_IDX],
                forced,
            )

            self.definitions.append(definition)