Skip to content

Commit

Permalink
#1761: move remote logging to a mixin
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@18522 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Feb 21, 2018
1 parent 570eb96 commit d0056ef
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 56 deletions.
89 changes: 89 additions & 0 deletions src/xpra/client/client_remote_logging.py
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
62 changes: 6 additions & 56 deletions src/xpra/client/ui_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from xpra.client.notification_client import NotificationClient
from xpra.client.window_client import WindowClient
from xpra.client.mmap_client_mixin import MmapClient
from xpra.client.client_remote_logging import RemoteLogging


FAKE_BROKEN_CONNECTION = envint("XPRA_FAKE_BROKEN_CONNECTION")
Expand Down Expand Up @@ -84,7 +85,7 @@ def fequ(v1, v2):
Utility superclass for client classes which have a UI.
See gtk_client_base and its subclasses.
"""
class UIXpraClient(XpraClientBase, WindowClient, WebcamForwarder, AudioClient, ClipboardClient, NotificationClient, RPCClient, MmapClient):
class UIXpraClient(XpraClientBase, WindowClient, WebcamForwarder, AudioClient, ClipboardClient, NotificationClient, RPCClient, MmapClient, RemoteLogging):
#NOTE: these signals aren't registered because this class
#does not extend GObject.
__gsignals__ = {
Expand All @@ -108,6 +109,7 @@ def __init__(self):
NotificationClient.__init__(self)
RPCClient.__init__(self)
MmapClient.__init__(self)
RemoteLogging.__init__(self)
try:
pinfo = get_platform_info()
osinfo = "%s" % platform_name(sys.platform, pinfo.get("linux_distribution") or pinfo.get("sysrelease", ""))
Expand Down Expand Up @@ -212,6 +214,7 @@ def init(self, opts):
NotificationClient.init(self, opts)
RPCClient.init(self, opts)
MmapClient.init(self, opts)
RemoteLogging.init(self, opts)
self.allowed_encodings = opts.encodings
self.encoding = opts.encoding
self.video_scaling = parse_bool_or_int("video-scaling", opts.video_scaling)
Expand Down Expand Up @@ -299,7 +302,7 @@ def quit(self, exit_code=0):

def cleanup(self):
log("UIXpraClient.cleanup()")
for x in (XpraClientBase, WindowClient, WebcamForwarder, AudioClient, ClipboardClient, NotificationClient, RPCClient, MmapClient):
for x in (XpraClientBase, WindowClient, WebcamForwarder, AudioClient, ClipboardClient, NotificationClient, RPCClient, MmapClient, RemoteLogging):
x.cleanup(self)
for x in (self.keyboard_helper, self.tray, self.menu_helper, self.client_extras, getVideoHelper()):
if x is None:
Expand Down Expand Up @@ -469,6 +472,7 @@ def server_connection_established(self):
def parse_server_capabilities(self):
if not XpraClientBase.parse_server_capabilities(self):
return False
RemoteLogging.parse_server_capabilities(self)
c = self.server_capabilities
self.server_session_name = strtobytes(c.rawget("session_name", b"")).decode("utf-8")
set_name("Xpra", self.session_name or self.server_session_name or "Xpra")
Expand Down Expand Up @@ -590,21 +594,6 @@ def process_ui_capabilities(self):
if self.pings>0:
self.timeout_add(1000*self.pings, self.send_ping)

def parse_logging_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 _process_startup_complete(self, packet):
log("all the existing windows and system trays have been received: %s items", len(self._id_to_window))
Expand Down Expand Up @@ -633,45 +622,6 @@ def after_handshake(self, cb, *args):
self._on_handshake.append((cb, args))


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


######################################################################
# info:
def _process_info_response(self, packet):
Expand Down

0 comments on commit d0056ef

Please sign in to comment.