/usr/lib/python2.7/dist-packages/xpra/log.py is in xpra 0.17.6+dfsg-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 | # This file is part of Xpra.
# Copyright (C) 2008, 2009 Nathaniel Smith <njs@pobox.com>
# Copyright (C) 2012-2015 Antoine Martin <antoine@devloop.org.uk>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.
import os
import sys
import logging
import weakref
# This module is used by non-GUI programs and thus must not import gtk.
LOG_PREFIX = os.environ.get("XPRA_LOG_PREFIX", "")
LOG_FORMAT = os.environ.get("XPRA_LOG_FORMAT", "%(asctime)s %(message)s")
NOPREFIX_FORMAT = "%(message)s"
logging.basicConfig(format=LOG_FORMAT)
logging.root.setLevel(logging.INFO)
#so we can keep a reference to all the loggers in use
#we may have multiple loggers for the same key, so use a dict
#but we don't want to prevent garbage collection so use a list of weakrefs
all_loggers = dict()
def add_logger(categories, logger):
global all_loggers
categories = list(categories)
categories.append("all")
l = weakref.ref(logger)
for cat in categories:
all_loggers.setdefault(cat, set()).add(l)
def get_all_loggers():
global all_loggers
a = set()
for loggers in all_loggers.values():
for l in list(loggers):
#weakref:
v = l()
if v:
a.add(v)
return a
debug_enabled_categories = set()
debug_disabled_categories = set()
def add_debug_category(*cat):
remove_disabled_category(*cat)
for c in cat:
debug_enabled_categories.add(c)
def remove_debug_category(*cat):
for c in cat:
if c in debug_enabled_categories:
debug_enabled_categories.remove(c)
def is_debug_enabled(category):
if "all" in debug_enabled_categories:
return True
if category in debug_enabled_categories:
return True
return isenvdebug(category) or isenvdebug("ALL")
def add_disabled_category(*cat):
remove_debug_category(*cat)
for c in cat:
debug_disabled_categories.add(c)
def remove_disabled_category(*cat):
for c in cat:
if c in debug_disabled_categories:
debug_disabled_categories.remove(c)
def get_loggers_for_categories(*cat):
if not cat:
return []
cset = set(cat)
matches = set()
for l in get_all_loggers():
if set(l.categories).issuperset(cset):
matches.add(l)
return list(matches or [])
def enable_debug_for(*cat):
loggers = get_loggers_for_categories(*cat)
for l in loggers:
if not l.is_debug_enabled():
l.enable_debug()
return loggers
def disable_debug_for(*cat):
loggers = get_loggers_for_categories(*cat)
for l in loggers:
if l.is_debug_enabled():
l.disable_debug()
return loggers
default_level = logging.DEBUG
def set_default_level(level):
global default_level
default_level = level
def standard_logging(log, level, msg, *args, **kwargs):
#this is just the regular logging:
log(level, msg, *args, **kwargs)
#this allows us to capture all logging and redirect it:
#the default 'standard_logging' uses the logger,
#but the client may inject its own handler here
global_logging_handler = standard_logging
def set_global_logging_handler(h):
global global_logging_handler
saved = global_logging_handler
global_logging_handler = h
return saved
def setloghandler(lh):
logging.root.handlers = []
logging.root.addHandler(lh)
def enable_color(to=sys.stdout, formatter=NOPREFIX_FORMAT):
if not hasattr(to, "fileno"):
#on win32 sys.stdout can be a "Blackhole",
#which does not have a fileno
return
from xpra.colorstreamhandler import ColorStreamHandler
from logging import Formatter
csh = ColorStreamHandler(to)
csh.setFormatter(Formatter(formatter))
setloghandler(csh)
#the order will be preserved with Python 2.7 onwards:
try:
from collections import OrderedDict
except:
OrderedDict = dict
STRUCT_KNOWN_FILTERS = OrderedDict([
("Client", OrderedDict([
("client" , "All client code"),
("paint" , "Client window paint code"),
("cairo" , "Cairo paint code used with the GTK3 client"),
("opengl" , "Client OpenGL rendering"),
("info" , "About and Session info dialogs"),
("launcher" , "The client launcher program"),
])),
("General", OrderedDict([
("clipboard" , "All clipboard operations"),
("notify" , "Notification forwarding"),
("tray" , "System Tray forwarding"),
("printing" , "Printing"),
("file" , "File transfers"),
("keyboard" , "Keyboard mapping and key event handling"),
("screen" , "Screen and workarea dimension"),
("fps" , "Frames per second"),
("xsettings" , "XSettings synchronization"),
("dbus" , "DBUS calls"),
("rpc" , "Remote Procedure Calls"),
("menu" , "Menus"),
("events" , "System and window events"),
])),
("Window", OrderedDict([
("window" , "All window code"),
("geometry" , "Window geometry"),
("shape" , "Window shape forwarding (XShape)"),
("focus" , "Window focus"),
("workspace" , "Window workspace synchronization"),
("metadata" , "Window metadata"),
("state" , "Window state"),
("icon" , "Window icons"),
("frame" , "Window frame")
])),
("Encoding", OrderedDict([
("codec" , "Codec loader and video helper"),
("loader" , "Pixel compression codec loader"),
("video" , "Video encoding"),
("score" , "Video pipeline scoring and selection"),
("encoding" , "Server side encoding selection and compression"),
("scaling" , "Picture scaling"),
("delta" , "Delta pre-compression"),
("xor" , "XOR delta pre-compression"),
("subregion" , "Video subregion processing"),
("regiondetect" , "Video region detection"),
("regionrefresh", "Video region refresh"),
("refresh" , "Refresh of lossy screen updates"),
("compress" , "Pixel compression (non video)"),
])),
("Codec", OrderedDict([
#codecs:
("csc" , "Colourspace conversion codecs"),
("cuda" , "CUDA device access (nvenc)"),
("cython" , "Cython CSC module"),
("opencl" , "OpenCL CSC module"),
("opencv" , "OpenCV CSC module"),
("swscale" , "swscale CSC module"),
("libyuv" , "libyuv CSC module"),
("decoder" , "All decoders"),
("encoder" , "All encoders"),
("avcodec" , "avcodec decoder"),
("libav" , "libav common code (swscale and avcodec)"),
("pillow" , "Pillow encoder and decoder"),
("vpx" , "libvpx encoder and decoder"),
("nvenc" , "nvenc encoder (all versions)"),
("x264" , "libx264 encoder"),
("x265" , "libx265 encoder"),
("xvid" , "Xvid encoder"),
("webp" , "libwebp encoder and decoder"),
("webcam" , "webcam access"),
])),
("Pointer", OrderedDict([
("mouse" , "Mouse motion"),
("cursor" , "Mouse cursor shape"),
])),
("Misc", OrderedDict([
#libraries
("gtk" , "All GTK code: bindings, client, etc"),
("util" , "All utility functions"),
("gobject" , "Command line clients"),
#server bits:
("test" , "Test code"),
("verbose" , "Very verbose flag"),
#specific applications:
])),
("Network", OrderedDict([
#internal / network:
("network" , "All network code"),
("mmap" , "mmap transfers"),
("protocol" , "Packet input and output (formatting, parsing, sending and receiving)"),
("crypto" , "Encryption"),
("auth" , "Authentication"),
])),
("Server", OrderedDict([
#Server:
("server" , "All server code"),
("proxy" , "Proxy server"),
("shadow" , "Shadow server"),
("command" , "Server control channel"),
("timeout" , "Server timeouts"),
("exec" , "Executing commands"),
#server features:
("mdns" , "mDNS session publishing"),
#server internals:
("stats" , "Server statistics"),
("grab" , "Window grabs"),
("xshm" , "XShm pixel capture"),
])),
("Sound", OrderedDict([
("sound" , "All sound"),
("gstreamer" , "GStreamer internal messages"),
("av-sync" , "Audio-video sync"),
])),
("X11", OrderedDict([
("x11" , "All X11 code"),
("bindings" , "X11 Cython bindings"),
("core" , "X11 core bindings"),
("randr" , "X11 RandR bindings"),
("ximage" , "X11 XImage bindings"),
("error" , "X11 errors"),
])),
("Platform", OrderedDict([
("platform" , "All platform support code"),
("import" , "Platform support import code"),
("osx" , "Mac OS X platform support code"),
("win32" , "Microsoft Windows platform support code"),
("posix" , "Posix platform code"),
])),
])
#flatten it:
KNOWN_FILTERS = OrderedDict()
for category, d in STRUCT_KNOWN_FILTERS.items():
for k,v in d.items():
KNOWN_FILTERS[k] = v
def isenvdebug(category):
return os.environ.get("XPRA_%s_DEBUG" % category.upper(), "0")=="1"
# A wrapper around 'logging' with some convenience stuff. In particular:
# -- You initialize it with a list of categories
# If unset, the default logging target is set to the name of the module where
# Logger() was called.
# -- Any of the categories can enable debug logging if the environment
# variable 'XPRA_${CATEGORY}_DEBUG' is set to "1"
# -- We also keep a list of debug_categories, so these can get enabled
# programatically too
# -- We keep track of which loggers are associated with each category,
# so we can enable/disable debug logging by category
# -- You can pass exc_info=True to any method, and sys.exc_info() will be
# substituted.
# -- __call__ is an alias for debug
# -- we bypass the logging system unless debugging is enabled for the logger,
# which is much faster than relying on the python logging code
class Logger(object):
def __init__(self, *categories):
self.categories = list(categories)
caller = sys._getframe(1).f_globals["__name__"]
if caller!="__main__":
self.categories.insert(0, caller)
self.logger = logging.getLogger(".".join(self.categories))
self.logger.setLevel(default_level)
disabled = False
enabled = False
for cat in self.categories:
if cat in debug_disabled_categories:
disabled = True
if is_debug_enabled(cat):
enabled = True
self.debug_enabled = enabled and not disabled
#ready, keep track of it:
add_logger(self.categories, self)
for x in categories:
if x not in KNOWN_FILTERS:
self.warn("unknown logging category: %s", x)
def __repr__(self):
return "Logger(%s)" % ", ".join(self.categories)
def is_debug_enabled(self):
return self.debug_enabled
def enable_debug(self):
self.debug_enabled = True
def disable_debug(self):
self.debug_enabled = False
def log(self, level, msg, *args, **kwargs):
if kwargs.get("exc_info") is True:
kwargs["exc_info"] = sys.exc_info()
global global_logging_handler
if LOG_PREFIX:
msg = LOG_PREFIX+msg
global_logging_handler(self.logger.log, level, msg, *args, **kwargs)
def __call__(self, msg, *args, **kwargs):
if self.debug_enabled:
self.log(logging.DEBUG, msg, *args, **kwargs)
def debug(self, msg, *args, **kwargs):
if self.debug_enabled:
self.log(logging.DEBUG, msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
self.log(logging.INFO, msg, *args, **kwargs)
def warn(self, msg, *args, **kwargs):
self.log(logging.WARN, msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
self.log(logging.ERROR, msg, *args, **kwargs)
class CaptureHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self, logging.DEBUG)
self.records = []
def handle(self, record):
self.records.append(record)
def emit(self, record):
self.records.append(record)
def createLock(self):
self.lock = None
|