Skip to content

Commit

Permalink
refactor the color logger code to make it easier to use, use it for m…
Browse files Browse the repository at this point in the history
…ore executable utilities, set +x bit

git-svn-id: https://xpra.org/svn/Xpra/trunk@10512 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Sep 1, 2015
1 parent 74fab16 commit 7016edb
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/xpra/client/gl/gl_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,11 @@ def main():
from xpra.platform import init,clean
from xpra.platform.gui import init as gui_init
from xpra.util import pver
from xpra.log import enable_color
try:
init("OpenGL-Check")
gui_init()
enable_color()
verbose = "-v" in sys.argv or "--verbose" in sys.argv
if verbose:
log.enable_debug()
Expand Down
3 changes: 2 additions & 1 deletion src/xpra/client/gtk_base/client_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,11 @@ def main():

from xpra.platform import init as platform_init, clean as platform_clean
from xpra.platform.gui import ready as gui_ready
from xpra.log import enable_color
try:
platform_init("Xpra-Launcher", "Xpra Connection Launcher")
enable_color()
gui_init()

try:
from xpra.scripts.main import parse_cmdline, fixup_debug_option
options, args = parse_cmdline(sys.argv)
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/codecs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ def encoding_help(encoding):

def main():
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Loader", "Encoding Info")
enable_color()
verbose = "-v" in sys.argv or "--verbose" in sys.argv
if verbose:
log.enable_debug()
Expand Down
33 changes: 21 additions & 12 deletions src/xpra/codecs/video_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,18 +427,27 @@ def getVideoHelper():

def main():
from xpra.codecs.loader import log as loader_log, load_codecs
if "-v" in sys.argv or "--verbose" in sys.argv:
loader_log.enable_debug()
log.enable_debug()
load_codecs()
vh = getVideoHelper()
vh.set_modules(ALL_VIDEO_ENCODER_OPTIONS, ALL_CSC_MODULE_OPTIONS, ALL_VIDEO_DECODER_OPTIONS)
vh.init()
log.info("VideoHelper.get_info():")
info = vh.get_info()
for k in sorted(info.keys()):
v = info.get(k)
log.info("%s=%s", k, v)
from xpra.log import enable_color
from xpra.platform import init, clean
try:
init("Video Helper")
enable_color()
if "-v" in sys.argv or "--verbose" in sys.argv:
loader_log.enable_debug()
log.enable_debug()
load_codecs()
vh = getVideoHelper()
vh.set_modules(ALL_VIDEO_ENCODER_OPTIONS, ALL_CSC_MODULE_OPTIONS, ALL_VIDEO_DECODER_OPTIONS)
vh.init()
log.info("VideoHelper.get_info():")
info = vh.get_info()
for k in sorted(info.keys()):
v = info.get(k)
if type(v) in (list, tuple):
v = csv(v)
log.info("%s=%s", k, v)
finally:
clean()


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/gtk_common/gtk_view_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@ def destroy(self, *args):

def main():
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Clipboard-Test", "Clipboard Test Tool")
enable_color()
ClipboardStateInfoWindow()
gtk.main()
finally:
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/gtk_common/gtk_view_keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ def destroy(self, *args):

def main():
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Keyboard-Test", "Keyboard Test Tool")
enable_color()
KeyboardStateInfoWindow()
gtk.main()
finally:
Expand Down
4 changes: 3 additions & 1 deletion src/xpra/gtk_common/keymap.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def get_gtk_keymap(ignore_keys=[None, "VoidSymbol", "0xffffff"]):
def main():
import sys
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Keymap-Tool", "Keymap Information Tool")
if "-v" in sys.argv:
enable_color()
if "-v" in sys.argv or "--verbose" in sys.argv:
log.enable_debug()
gtk_keymap = get_gtk_keymap()
def pkey(*entries):
Expand Down
13 changes: 13 additions & 0 deletions src/xpra/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def set_global_logging_handler(h):
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):
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
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/net/net_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ def get_info():

def main():
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Network-Info", "Network Info")
enable_color()
verbose = "-v" in sys.argv or "--verbose" in sys.argv
if verbose:
log.enable_debug()
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/platform/gui.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ def fnames(l):
def main():
from xpra.platform import init as platform_init,clean
from xpra.util import nonl
from xpra.log import enable_color
try:
platform_init("GUI-Properties")
enable_color()
init()
verbose = "-v" in sys.argv or "--verbose" in sys.argv
if verbose:
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/platform/printing.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ def print_dict(d):
v = d[k]
print("* %s : %s" % (k.ljust(32), nonl(pver(v))))
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Printing", "Printing")
enable_color()
if len(sys.argv)<3:
print_dict(get_printers())
else:
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/scripts/bug_report.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
def main():
from xpra.platform import init as platform_init
platform_init("Xpra-Bug-Report", "Xpra Bug Report")
from xpra.log import enable_color
enable_color()

from xpra.log import Logger, enable_debug_for
log = Logger("util")
Expand Down
4 changes: 3 additions & 1 deletion src/xpra/scripts/config.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,12 @@ def print_options(o):
v = ", ".join(str(x) for x in v)
print("* %-32s : %s" % (k, nonl(v)))
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Config-Info", "Config Info")
enable_color()
args = list(sys.argv[1:])
if "-v" in args:
if "-v" in args or "--verbose" in sys.argv:
global debug
def debug(*args):
print(args[0] % args[1:])
Expand Down
24 changes: 6 additions & 18 deletions src/xpra/scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,28 +788,21 @@ def show_sound_codec_help(is_server, speaker_codecs, microphone_codecs):
microphone_codecs += all_microphone_codecs
return info

def setloghandler(lh):
logging.root.handlers = []
logging.root.addHandler(lh)

def configure_logging(options, mode):
to = sys.stderr
if mode in ("showconfig", "info", "control", "list", "attach", "stop", "version", "print"):
to = sys.stdout
if mode in ("start", "upgrade", "attach", "shadow", "proxy", "_sound_record", "_sound_play", "stop", "version", "print"):
if mode in ("start", "upgrade", "attach", "shadow", "proxy", "_sound_record", "_sound_play", "stop", "version", "print", "showconfig"):
if "help" in options.speaker_codec or "help" in options.microphone_codec:
info = show_sound_codec_help(mode!="attach", options.speaker_codec, options.microphone_codec)
raise InitInfo("\n".join(info))
if (hasattr(to, "fileno") and os.isatty(to.fileno())) or os.environ.get("XPRA_FORCE_COLOR_LOG", "0")=="1":
from xpra.colorstreamhandler import ColorStreamHandler
from xpra.log import LOG_FORMAT
from logging import Formatter
csh = ColorStreamHandler(to)
csh.setFormatter(Formatter(LOG_FORMAT))
setloghandler(csh)
from xpra.log import LOG_FORMAT, enable_color
enable_color(to, LOG_FORMAT)
else:
#a bit naughty here, but it's easier to let xpra.log initialize
#the logging system every time, and just undo things here..
from xpra.log import setloghandler
setloghandler(logging.StreamHandler(to))

from xpra.log import add_debug_category, add_disabled_category, enable_debug_for, disable_debug_for
Expand Down Expand Up @@ -1689,15 +1682,10 @@ def run_list(error_cb, opts, extra_args):
return 0

def run_showconfig(options, args):
from xpra.colorstreamhandler import ColorStreamHandler
from xpra.log import Logger, NOPREFIX_FORMAT
from xpra.util import nonl
from logging import Formatter
csh = ColorStreamHandler(sys.stdout)
csh.setFormatter(Formatter(NOPREFIX_FORMAT))
setloghandler(csh)
from xpra.log import Logger
log = Logger("util")
from xpra.scripts.config import dict_to_validated_config, print_env
from xpra.util import nonl
d = dict_to_validated_config({})
fixup_options(d)
VIRTUAL = ["mode"] #no such option! (it's a virtual one for the launch by config files)
Expand Down
2 changes: 2 additions & 0 deletions src/xpra/sound/gstreamer_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,10 @@ def sound_option_or_all(name, options, all_values):
def main():
global pygst_version, gst_version, gst_vinfo
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("GStreamer-Info", "GStreamer Information")
enable_color()
if "-v" in sys.argv or "--verbose" in sys.argv:
log.enable_debug()
import_gst()
Expand Down
17 changes: 12 additions & 5 deletions src/xpra/sound/pulseaudio_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ def add_audio_tagging_env(env_dict=os.environ, icon_path=None):


def main():
if "-v" in sys.argv:
log.enable_debug()
i = get_info()
for k in sorted(i):
log.info("%s : %s", k.ljust(64), i[k])
from xpra.platform import init, clean
from xpra.log import enable_color
try:
init("Pulseaudio-Info")
enable_color()
if "-v" in sys.argv or "--verbose" in sys.argv:
log.enable_debug()
i = get_info()
for k in sorted(i):
log.info("%s : %s", k.ljust(64), i[k])
finally:
clean()

if __name__ == "__main__":
main()

0 comments on commit 7016edb

Please sign in to comment.