|
17 | 17 | # GNU General Public License for more details. |
18 | 18 |
|
19 | 19 |
|
20 | | -import os |
21 | | -import logging |
22 | | -from linuxcnc import ini |
| 20 | +# this has been moved to python/lib/common |
| 21 | +# in the future we might be able to remove this file |
| 22 | +# but it could break customizations out in the user world |
| 23 | +# might be better just just leave this 'springboard' here |
23 | 24 |
|
24 | | -# For convenience import log levels so we don't need to import |
25 | | -# logging to set the log level within other modules. |
26 | | -from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL |
| 25 | +from common.logger import * |
27 | 26 |
|
28 | | -# add a new verbose level |
29 | | -VERBOSE = 5 |
30 | | -logging.VERBOSE = 5 |
31 | | -logging.addLevelName(logging.VERBOSE, "VERBOSE") |
32 | | - |
33 | | - |
34 | | -# add a custom log level function |
35 | | -def verbose(self, message, *args, **kws): |
36 | | - if self.isEnabledFor(VERBOSE): |
37 | | - # Yes, logger takes its '*args' as 'args'. |
38 | | - self._log(logging.VERBOSE, message, args, **kws) |
39 | | - |
40 | | -# add the custom log level to the library (class patch) |
41 | | -logging.Logger.verbose = verbose |
42 | | -logging.Logger.VERBOSE = VERBOSE |
43 | | -# Our custom colorizing formatter for the terminal handler |
44 | | -from .lib.colored_formatter import ColoredFormatter |
45 | | - |
46 | | - |
47 | | -# Global name of the base logger |
48 | | -BASE_LOGGER_NAME = None |
49 | | -BASE_LOGGER_FILE = None |
50 | | - |
51 | | -# Define the log message formats |
52 | | -TERM_FORMAT = '[%(name)s][%(levelname)s] %(message)s (%(filename)s:%(lineno)d)' |
53 | | -FILE_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
54 | | - |
55 | | - |
56 | | -# Get logger for module based on module.__name__ |
57 | | -def getLogger(name): |
58 | | - if BASE_LOGGER_NAME is None: |
59 | | - initBaseLogger('DEFAULT') |
60 | | - name = '{0}.{1}'.format(BASE_LOGGER_NAME, name.upper()) |
61 | | - return logging.getLogger(name) |
62 | | - |
63 | | - |
64 | | -# Set global logging level |
65 | | -def setGlobalLevel(level): |
66 | | - base_log = logging.getLogger(BASE_LOGGER_NAME) |
67 | | - base_log.setLevel(level) |
68 | | - |
69 | | - |
70 | | -# Initialize the base logger |
71 | | -def initBaseLogger(name, log_file=None, log_level=DEBUG, logToFile=False): |
72 | | - |
73 | | - global BASE_LOGGER_NAME |
74 | | - BASE_LOGGER_NAME = name |
75 | | - |
76 | | - if not log_file: |
77 | | - log_file = getLogFile(name) |
78 | | - |
79 | | - global BASE_LOGGER_FILE |
80 | | - BASE_LOGGER_FILE = log_file |
81 | | - |
82 | | - # Clear the previous sessions log file |
83 | | - with open(log_file, 'w') as fh: |
84 | | - pass |
85 | | - |
86 | | - # Create base logger |
87 | | - base_log = logging.getLogger(BASE_LOGGER_NAME) |
88 | | - base_log.setLevel(log_level) |
89 | | - |
90 | | - # Add console handler |
91 | | - ch = logging.StreamHandler() |
92 | | - ch.setLevel(logging.VERBOSE) |
93 | | - cf = ColoredFormatter(TERM_FORMAT) |
94 | | - ch.setFormatter(cf) |
95 | | - base_log.addHandler(ch) |
96 | | - |
97 | | - # Add file handler |
98 | | - if logToFile: |
99 | | - fh = logging.FileHandler(log_file) |
100 | | - fh.setLevel(logging.VERBOSE) |
101 | | - ff = logging.Formatter(FILE_FORMAT) |
102 | | - fh.setFormatter(ff) |
103 | | - base_log.addHandler(fh) |
104 | | - |
105 | | - # Get logger for logger |
106 | | - log = getLogger(__name__) |
107 | | - if logToFile: |
108 | | - base_log.info('Logging to: yellow<{}>'.format(log_file)) |
109 | | - |
110 | | - return base_log |
111 | | - |
112 | | - |
113 | | -# Attempt to find the log file specified INI [DISPLAY] LOG_FILE, |
114 | | -# failing that log to $HOME/<base_log_name>.log |
115 | | -def getLogFile(name): |
116 | | - |
117 | | - # Default log file to use if not specified in INI |
118 | | - log_file = os.path.expanduser('~/{}.log').format(name.lower()) |
119 | | - |
120 | | - # LinuxCNC may not be running, so use get() to avoid a KeyError |
121 | | - ini_file = os.environ.get('INI_FILE_NAME') |
122 | | - config_dir = os.environ.get('CONFIG_DIR') |
123 | | - |
124 | | - if ini_file: |
125 | | - lcnc_ini = ini(ini_file) |
126 | | - path = lcnc_ini.find('DISPLAY', 'LOG_FILE') |
127 | | - if path: |
128 | | - if path.startswith('~'): |
129 | | - # Path is relative to $HOME |
130 | | - log_file = os.path.expanduser(path) |
131 | | - elif not os.path.isabs(path): |
132 | | - # Assume intended path is relative to the INI file |
133 | | - log_file = os.path.join(config_dir, path) |
134 | | - else: |
135 | | - # It must be an absolute path then |
136 | | - log_file = os.path.realpath(path) |
137 | | - |
138 | | - return log_file |
0 commit comments