File: log.py

package info (click to toggle)
displaycal-py3 3.9.16-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,120 kB
  • sloc: python: 115,777; javascript: 11,540; xml: 598; sh: 257; makefile: 173
file content (432 lines) | stat: -rw-r--r-- 15,160 bytes parent folder | download
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# -*- coding: utf-8 -*-


from codecs import EncodedFile
from hashlib import md5
import atexit
import logging
import logging.handlers
import os
import re
import sys
import warnings
from io import BytesIO
from time import localtime, strftime, time

from DisplayCAL.meta import name as appname, script2pywname
from DisplayCAL.multiprocess import mp
from DisplayCAL.options import debug
from DisplayCAL.safe_print import SafePrinter, safe_print as _safe_print
from DisplayCAL.util_os import safe_glob

logging.raiseExceptions = 0
logging._warnings_showwarning = warnings.showwarning


if debug:
    loglevel = logging.DEBUG
else:
    loglevel = logging.INFO


logger = None
_logdir = None


def showwarning(message, category, filename, lineno, file=None, line=""):
    # Adapted from _showwarning in Python2.7/lib/logging/__init__.py
    """
    Implementation of showwarnings which redirects to logging, which will first
    check to see if the file parameter is None. If a file is specified, it will
    delegate to the original warnings implementation of showwarning. Otherwise,
    it will call warnings.formatwarning and will log the resulting string to a
    warnings logger named "py.warnings" with level logging.WARNING.

    UNlike the default implementation, the line is omitted from the warning,
    and the warning does not end with a newline.
    """
    if file is not None:
        if logging._warnings_showwarning is not None:
            logging._warnings_showwarning(
                message, category, filename, lineno, file, line
            )
    else:
        s = warnings.formatwarning(message, category, filename, lineno, line)
        logger = logging.getLogger("py.warnings")
        if not logger.handlers:
            if hasattr(sys.stderr, "isatty") and sys.stderr.isatty():
                handler = logging.StreamHandler()  # Logs to stderr by default
            else:
                handler = logging.NullHandler()
            logger.addHandler(handler)
        log(s.strip(), fn=logger.warning)


warnings.showwarning = showwarning

logbuffer = EncodedFile(BytesIO(), "UTF-8", errors="replace")


def wx_log(logwindow, msg):
    if logwindow.IsShownOnScreen():
        # Check if log buffer has been emptied or not.
        # If it has, our log message is already included.
        if logbuffer.tell():
            logwindow.Log(msg)


class DummyLogger:
    def critical(self, msg, *args, **kwargs):
        pass

    def debug(self, msg, *args, **kwargs):
        pass

    def error(self, msg, *args, **kwargs):
        pass

    def exception(self, msg, *args, **kwargs):
        pass

    def info(self, msg, *args, **kwargs):
        pass

    def log(self, level, msg, *args, **kwargs):
        pass

    def warning(self, msg, *args, **kwargs):
        pass


class Log:
    def __call__(self, msg, fn=None):
        """Log a message.

        Optionally use function 'fn' instead of logging.info.

        """
        global logger
        if isinstance(msg, bytes):
            msg = msg.decode("utf-8", "replace")

        msg = msg.replace("\r\n", "\n").replace("\r", "")
        if fn is None and logger and logger.handlers:
            fn = logger.info
        if fn:
            for line in msg.split("\n"):
                fn(line)
        # If wxPython itself calls warnings.warn on import, it is not yet fully
        # imported at the point our showwarning() function calls log().
        # Check for presence of our wxfixes module and if it has an attribute
        # "wx", in which case wxPython has finished importing.
        wxfixes = sys.modules.get("%s.wxfixes" % appname)
        # wxfixes = sys.modules.get("wxfixes")
        if (
            wxfixes
            and hasattr(wxfixes, "wx")
            and mp.current_process().name == "MainProcess"
        ):
            wx = wxfixes.wx
            if (
                wx.GetApp() is not None
                and hasattr(wx.GetApp(), "frame")
                and hasattr(wx.GetApp().frame, "infoframe")
            ):
                wx.CallAfter(wx_log, wx.GetApp().frame.infoframe, msg)

    def flush(self):
        pass

    def write(self, msg):
        self(msg.rstrip())


log = Log()


class LogFile:
    """Logfile class. Default is to not rotate."""

    def __init__(self, filename, logdir, when="never", backupCount=0):
        self.filename = filename
        self._logger = get_file_logger(
            md5(filename.encode()).hexdigest(),
            when=when,
            backupCount=backupCount,
            logdir=logdir,
            filename=filename,
        )

    def close(self):
        for handler in reversed(self._logger.handlers):
            handler.close()
            self._logger.removeHandler(handler)

    def flush(self):
        for handler in self._logger.handlers:
            handler.flush()

    def write(self, msg):
        for line in msg.rstrip().replace("\r\n", "\n").replace("\r", "").split("\n"):
            self._logger.info(line)


class SafeLogger(SafePrinter):
    """Print and log safely, avoiding any UnicodeDe-/EncodingErrors on strings
    and converting all other objects to safe string representations.
    """

    def __init__(self, log=True, print_=None):
        SafePrinter.__init__(self)
        self.log = log
        if print_ is None:
            print_ = (
                sys.stdout and hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
            )
        self.print_ = print_

    def write(self, *args, **kwargs):
        if kwargs.get("print_", self.print_):
            _safe_print(*args, **kwargs)
        if kwargs.get("log", self.log):
            kwargs.update(fn=log, encoding=None)
            _safe_print(*args, **kwargs)


safe_log = SafeLogger(print_=False)
safe_print = SafeLogger()


safe_log = SafeLogger(print_=False)
safe_print = SafeLogger()


def get_file_logger(
    name,
    level=loglevel,
    when="midnight",
    backupCount=5,
    logdir=None,
    filename=None,
    confighome=None,
):
    """Return logger object.

    A TimedRotatingFileHandler or FileHandler (if when == "never") will be used.

    """
    global _logdir
    global logger
    if logdir is None:
        logdir = _logdir
    logger = logging.getLogger(name)
    if not filename:
        filename = name
    mode = "a"
    if confighome:
        # Use different logfile name (append number) for each additional instance
        is_main_process = mp.current_process().name == "MainProcess"
        if os.path.basename(confighome).lower() == "dispcalgui":
            lockbasename = filename.replace(appname, "dispcalGUI")
        else:
            lockbasename = filename
        lockfilepath = os.path.join(confighome, lockbasename + ".lock")
        if os.path.isfile(lockfilepath):
            try:
                with open(lockfilepath, "r") as lockfile:
                    instances = len(lockfile.read().splitlines())
            except Exception:
                pass
            else:
                if not is_main_process:
                    # Running as child from multiprocessing under Windows
                    instances -= 1
                if instances:
                    filenames = [filename]
                    filename += ".%i" % instances
                    filenames.append(filename)
                    if filenames[0].endswith("-apply-profiles"):
                        # Running the profile loader always sends a close
                        # request to an already running instance, so there
                        # will be at most two logfiles, and we want to use
                        # the one not currently in use.
                        mtimes = {}
                        for filename in filenames:
                            logfile = os.path.join(logdir, filename + ".log")
                            if not os.path.isfile(logfile):
                                mtimes[0] = filename
                                continue
                            try:
                                logstat = os.stat(logfile)
                            except Exception as exception:
                                print(
                                    "Warning - os.stat('%s') failed: %s"
                                    % (logfile, exception)
                                )
                            else:
                                mtimes[logstat.st_mtime] = filename
                        if mtimes:
                            filename = mtimes[sorted(mtimes.keys())[0]]
        if is_main_process:
            for lockfilepath in safe_glob(
                os.path.join(confighome, lockbasename + ".mp-worker-*.lock")
            ):
                try:
                    os.remove(lockfilepath)
                except Exception:
                    pass
        else:
            # Running as child from multiprocessing under Windows
            lockbasename += ".mp-worker-"
            process_num = 1
            while os.path.isfile(
                os.path.join(confighome, lockbasename + "%i.lock" % process_num)
            ):
                process_num += 1
            lockfilepath = os.path.join(
                confighome, lockbasename + "%i.lock" % process_num
            )
            try:
                with open(lockfilepath, "w") as lockfile:
                    pass
            except Exception:
                pass
            else:
                atexit.register(os.remove, lockfilepath)
            when = "never"
            filename += ".mp-worker-%i" % process_num
            mode = "w"
    logfile = os.path.join(logdir, filename + ".log")
    for handler in logger.handlers:
        if isinstance(
            handler, logging.FileHandler
        ) and handler.baseFilename == os.path.abspath(logfile):
            return logger
    logger.propagate = 0
    logger.setLevel(level)
    if not os.path.exists(logdir):
        try:
            os.makedirs(logdir)
        except Exception as exception:
            print(
                "Warning - log directory '%s' could not be created: %s"
                % (logdir, exception)
            )
    elif when != "never" and os.path.exists(logfile):
        try:
            logstat = os.stat(logfile)
        except Exception as exception:
            print("Warning - os.stat('%s') failed: %s" % (logfile, exception))
        else:
            # rollover needed?
            t = logstat.st_mtime
            try:
                mtime = localtime(t)
            except ValueError:
                # This can happen on Windows because localtime() is buggy on
                # that platform. See:
                # http://stackoverflow.com/questions/4434629/zipfile-module-in-python-runtime-problems
                # http://bugs.python.org/issue1760357
                # To overcome this problem, we ignore the real modification
                # date and force a rollover
                t = time() - 60 * 60 * 24
                mtime = localtime(t)
            # Deal with DST
            now = localtime()
            dstNow = now[-1]
            dstThen = mtime[-1]
            if dstNow != dstThen:
                if dstNow:
                    addend = 3600
                else:
                    addend = -3600
                mtime = localtime(t + addend)
            if now[:3] > mtime[:3]:
                # do rollover
                logbackup = logfile + strftime(".%Y-%m-%d", mtime)
                if os.path.exists(logbackup):
                    try:
                        os.remove(logbackup)
                    except Exception as exception:
                        print(
                            "Warning - logfile backup '%s' could not be removed during rollover: %s"
                            % (logbackup, exception)
                        )
                try:
                    os.rename(logfile, logbackup)
                except Exception as exception:
                    print(
                        "Warning - logfile '%s' could not be renamed to '%s' during rollover: %s"
                        % (logfile, os.path.basename(logbackup), exception)
                    )
                # Adapted from Python 2.6's
                # logging.handlers.TimedRotatingFileHandler.getFilesToDelete
                extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}$")
                baseName = os.path.basename(logfile)
                try:
                    fileNames = os.listdir(logdir)
                except Exception as exception:
                    print(
                        "Warning - log directory '%s' listing failed during rollover: %s"
                        % (logdir, exception)
                    )
                else:
                    result = []
                    prefix = baseName + "."
                    plen = len(prefix)
                    for fileName in fileNames:
                        if fileName[:plen] == prefix:
                            suffix = fileName[plen:]
                            if extMatch.match(suffix):
                                result.append(os.path.join(logdir, fileName))
                    result.sort()
                    if len(result) > backupCount:
                        for logbackup in result[: len(result) - backupCount]:
                            try:
                                os.remove(logbackup)
                            except Exception as exception:
                                print(
                                    "Warning - logfile backup '%s' could not be removed during rollover: %s"
                                    % (logbackup, exception)
                                )
    if os.path.exists(logdir):
        try:
            if when != "never":
                filehandler = logging.handlers.TimedRotatingFileHandler(
                    logfile, when=when, backupCount=backupCount
                )
            else:
                filehandler = logging.FileHandler(logfile, mode)
            fileformatter = logging.Formatter("%(asctime)s %(message)s")
            filehandler.setFormatter(fileformatter)
            logger.addHandler(filehandler)
        except Exception as exception:
            print(
                "Warning - logging to file '%s' not possible: %s" % (logfile, exception)
            )
    return logger


def setup_logging(logdir, name=appname, ext=".py", backupCount=5, confighome=None):
    """Setup the logging facility."""
    global _logdir, logger
    _logdir = logdir
    name = script2pywname(name)
    if (
        name.startswith(appname)
        or name.startswith("dispcalGUI")
        or ext in (".app", ".exe", ".pyw")
    ):
        logger = get_file_logger(
            None,
            loglevel,
            "midnight",
            backupCount,
            filename=name,
            confighome=confighome,
        )
        if name == appname or name == "dispcalGUI":
            streamhandler = logging.StreamHandler(logbuffer)
            streamformatter = logging.Formatter("%(asctime)s %(message)s")
            streamhandler.setFormatter(streamformatter)
            logger.addHandler(streamhandler)