File: logging.py

package info (click to toggle)
fs-uae-arcade 3.1.63-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 24,456 kB
  • sloc: python: 56,011; makefile: 170
file content (102 lines) | stat: -rw-r--r-- 2,601 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
import os
import sys
import logging
import threading

import time

import fsboot

# using this lock to serialize logging from different threads
lock = threading.RLock()


class MultiplexedOutput:
    def __init__(self, *files):
        self.files = files

    def flush(self):
        with lock:
            for f in self.files:
                try:
                    f.flush()
                except Exception:
                    pass

    def isatty(self):
        return False

    def write(self, msg):
        with lock:
            for f in self.files:
                try:
                    f.write(msg)
                except Exception:
                    pass


class FileOutput(object):
    def __init__(self, file_obj):
        self.file = file_obj
        self.new_line = False

    def flush(self):
        return self.file.flush()

    def isatty(self):
        return False

    def write(self, msg):
        if isinstance(msg, str):
            self.write(msg.encode("UTF-8"))
            return
        # FIXME: legacy hack, should be removed in the future
        if b"database_password" in msg:
            return

        if self.new_line:
            elapsed = time.perf_counter() - fsboot.perf_counter_epoch
            self.file.write("{:0.3f} ".format(elapsed).encode("ASCII"))

        self.file.write(msg)
        self.new_line = msg.endswith(b"\n")


class NullOutput(object):
    def flush(self):
        pass

    def isatty(self):
        return False

    def write(self, msg):
        pass


def setup_logging(log_name):
    if sys.platform == "win32" and "FS_FORCE_STDOUT" not in os.environ:
        # noinspection PyUnresolvedReferences
        # import win32console
        if hasattr(sys, "frozen"):
            # or win32console.GetConsoleWindow() == 0:
            sys.stdout = NullOutput()
            sys.stderr = NullOutput()

    # FIXME: remove dependency on fsgs here!
    from fsgs.FSGSDirectories import FSGSDirectories

    logs_dir = FSGSDirectories.get_logs_dir()
    log_file = os.path.join(logs_dir, log_name)
    print("[LOGGING] Logging to", log_file)
    try:
        f = open(log_file, "wb")
    except Exception:
        print("[LOGGING] Could not open log file")
        # use MultiplexedOutput here too, for the mutex handling
        sys.stdout = MultiplexedOutput(sys.stdout)
        sys.stderr = MultiplexedOutput(sys.stderr)
    else:
        sys.stdout = MultiplexedOutput(FileOutput(f), sys.stdout)
        sys.stderr = MultiplexedOutput(FileOutput(f), sys.stderr)

    logging.basicConfig(stream=sys.stdout, level=logging.NOTSET)