-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1761: move remote logging to a mixin
git-svn-id: https://xpra.org/svn/Xpra/trunk@18522 3bb7dfac-3a0b-4e04-842a-767bc560f471
- Loading branch information
Showing
2 changed files
with
95 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# This file is part of Xpra. | ||
# Copyright (C) 2010-2018 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 sys | ||
import traceback | ||
import logging | ||
|
||
from xpra.log import Logger, set_global_logging_handler | ||
log = Logger("client") | ||
|
||
|
||
from xpra.scripts.config import parse_bool | ||
from xpra.os_util import monotonic_time, strtobytes | ||
|
||
|
||
""" | ||
Mixin for remote logging support | ||
""" | ||
class RemoteLogging(object): | ||
|
||
def __init__(self): | ||
self.in_remote_logging = False | ||
self.local_logging = None | ||
|
||
def init(self, opts): | ||
self.log_both = (opts.remote_logging or "").lower()=="both" | ||
self.client_supports_remote_logging = self.log_both or parse_bool("remote-logging", opts.remote_logging) | ||
|
||
|
||
def cleanup(self): | ||
ll = self.local_logging | ||
if ll: | ||
set_global_logging_handler(ll) | ||
|
||
|
||
def parse_server_capabilities(self): | ||
c = self.server_capabilities | ||
if self.client_supports_remote_logging and c.boolget("remote-logging"): | ||
#check for debug: | ||
from xpra.log import is_debug_enabled | ||
for x in ("network", "crypto", "udp"): | ||
if is_debug_enabled(x): | ||
log.warn("Warning: cannot enable remote logging") | ||
log.warn(" because '%s' debug logging is enabled", x) | ||
return | ||
log.info("enabled remote logging") | ||
if not self.log_both: | ||
log.info(" see server log file for further output") | ||
self.local_logging = set_global_logging_handler(self.remote_logging_handler) | ||
|
||
def remote_logging_handler(self, log, level, msg, *args, **kwargs): | ||
#prevent loops (if our send call ends up firing another logging call): | ||
if self.in_remote_logging: | ||
return | ||
self.in_remote_logging = True | ||
try: | ||
dtime = int(1000*(monotonic_time() - self.start_time)) | ||
data = self.compressed_wrapper("text", strtobytes(msg % args), level=1) | ||
self.send("logging", level, data, dtime) | ||
exc_info = kwargs.get("exc_info") | ||
if exc_info is True: | ||
exc_info = sys.exc_info() | ||
if exc_info: | ||
for x in traceback.format_tb(exc_info[2]): | ||
self.send("logging", level, strtobytes(x), dtime) | ||
if self.log_both: | ||
self.local_logging(log, level, msg, *args, **kwargs) | ||
except Exception as e: | ||
if self.exit_code is not None: | ||
#errors can happen during exit, don't care | ||
return | ||
self.local_logging(log, logging.WARNING, "Warning: failed to send logging packet:") | ||
self.local_logging(log, logging.WARNING, " %s" % e) | ||
self.local_logging(log, logging.WARNING, " original unformatted message: %s", msg) | ||
try: | ||
self.local_logging(log, level, msg, *args, **kwargs) | ||
except: | ||
pass | ||
try: | ||
exc_info = sys.exc_info() | ||
for x in traceback.format_tb(exc_info[2]): | ||
for v in x.splitlines(): | ||
self.local_logging(log, logging.WARNING, v) | ||
except: | ||
pass | ||
finally: | ||
self.in_remote_logging = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters