/usr/lib/python2.7/dist-packages/DisplayCAL/log.py is in dispcalgui 3.5.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
from __future__ import with_statement
from codecs import EncodedFile
from hashlib import md5
import atexit
import glob
import logging
import logging.handlers
import os
import re
import sys
import warnings
from time import localtime, strftime, time
from meta import name as appname, script2pywname
from multiprocess import mp
from options import debug
from safe_print import SafePrinter, safe_print as _safe_print
from util_io import StringIOu as StringIO
from util_str import safe_str, safe_unicode
logging.raiseExceptions = 0
logging._warnings_showwarning = warnings.showwarning
if debug:
loglevel = logging.DEBUG
else:
loglevel = logging.INFO
logger = None
def showwarning(message, category, filename, lineno, file=None, line=""):
"""
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()
else:
handler = logging.NullHandler()
logger.addHandler(handler)
logger.warning("%s", s.strip())
warnings.showwarning = showwarning
logbuffer = EncodedFile(StringIO(), "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
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 "wx" in sys.modules and mp.current_process().name == "MainProcess":
from wxaddons import 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(safe_str(filename,
"UTF-8")).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_=hasattr(sys.stdout, "isatty") and
sys.stdout.isatty()):
SafePrinter.__init__(self)
self.log = log
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()
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
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(filter(lambda s: s.strip(),
lockfile.readlines()))
except:
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):
continue
try:
logstat = os.stat(logfile)
except Exception, exception:
safe_print(u"Warning - os.stat('%s') failed: %s" %
tuple(safe_unicode(s) for s in (logfile,
exception)))
else:
mtimes[logstat.st_mtime] = filename
if mtimes:
filename = mtimes[sorted(mtimes.keys())[0]]
if is_main_process:
for lockfilepath in glob.glob(os.path.join(confighome,
lockbasename +
".mp-worker-*.lock")):
try:
os.remove(lockfilepath)
except:
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:
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, exception:
safe_print(u"Warning - log directory '%s' could not be created: %s"
% tuple(safe_unicode(s) for s in (logdir, exception)))
elif when != "never" and os.path.exists(logfile):
try:
logstat = os.stat(logfile)
except Exception, exception:
safe_print(u"Warning - os.stat('%s') failed: %s" %
tuple(safe_unicode(s) for s in (logfile, exception)))
else:
# rollover needed?
t = logstat.st_mtime
try:
mtime = localtime(t)
except ValueError, exception:
# 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, exception:
safe_print(u"Warning - logfile backup '%s' could not "
u"be removed during rollover: %s" %
tuple(safe_unicode(s) for s in (logbackup,
exception)))
try:
os.rename(logfile, logbackup)
except Exception, exception:
safe_print(u"Warning - logfile '%s' could not be renamed "
u"to '%s' during rollover: %s" %
tuple(safe_unicode(s) for s in
(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, exception:
safe_print(u"Warning - log directory '%s' listing failed "
u"during rollover: %s" %
tuple(safe_unicode(s) for s in (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, exception:
safe_print(u"Warning - logfile backup '%s' "
u"could not be removed during "
u"rollover: %s" %
tuple(safe_unicode(s) for s in
(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, exception:
safe_print(u"Warning - logging to file '%s' not possible: %s" %
tuple(safe_unicode(s) for s in (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)
|