Skip to content

Commit

Permalink
gladevcp: use logger for all console outputs instead of print()
Browse files Browse the repository at this point in the history
  • Loading branch information
hansu committed Nov 17, 2024
1 parent 8fb7cb5 commit a03b084
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/hal/user_comps/gladevcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib

import signal
# Set up the base logger
Expand Down Expand Up @@ -90,7 +91,7 @@
def dbg(string):
global gladevcp_debug
if not gladevcp_debug: return
print(string)
LOG.debug(string)

def on_window_destroy(widget, data=None):
Gtk.main_quit()
Expand Down Expand Up @@ -125,7 +126,7 @@ def add_handler(method, f):
try:
mod = __import__(basename)
except ImportError as msg:
print("module '%s' skipped - import error: %s" %(basename,msg))
LOG.error("module '%s' skipped - import error: %s" %(basename,msg))
continue
LOG.debug("module '%s' imported OK" % mod.__name__)
try:
Expand Down Expand Up @@ -154,7 +155,7 @@ def add_handler(method, f):
LOG.debug("Register callback '%s' in %s" % (method, object))
add_handler(method, f)
except Exception as e:
print("gladevcp: trouble looking for handlers in '%s': %s" %(basename, e))
LOG.warning("gladevcp: trouble looking for handlers in '%s': %s" %(basename, e))
traceback.print_exc()

# Wrap lists in Trampoline, unwrap single functions
Expand Down Expand Up @@ -194,15 +195,15 @@ def main():
if opts.debug:
# Log level defaults to INFO, so set lower if in debug mode
logger.setGlobalLevel(logger.DEBUG)
LOG.debug('DEBUGGING logging on')
LOG.info('DEBUGGING logging on')
elif opts.info:
logger.setGlobalLevel(logger.INFO)
print('INFO logging on')
LOG.info('INFO logging on')
elif opts.quiet:
logger.setGlobalLevel(logger.ERROR)
elif opts.verbose:
logger.setGlobalLevel(logger.VERBOSE)
print('VERBOSE logging on')
LOG.info('VERBOSE logging on')
else:
logger.setGlobalLevel(logger.WARNING)

Expand All @@ -228,29 +229,30 @@ def main():
try:
builder = Gtk.Builder()
builder.add_from_file(xmlname)
except Exception as e:

except GLib.Error as e:
LOG.error(e.message)
try:
# try loading as a Gtk.builder project
LOG.debug("**** GLADE VCP INFO: Not a builder project, trying to load as a lib glade project")
print(e)
LOG.info("GLADE VCP INFO: Not a builder project, trying to load as a lib glade project")
builder = Gtk.glade.XML(xmlname)
builder = GladeBuilder(builder)

except Exception as e:
print("**** GLADE VCP ERROR: With xml file: %s : %s" % (xmlname,e), file=sys.stderr)
LOG.error(f"GLADE VCP ERROR: With xml file: '{xmlname}': {e}")
sys.exit(0)

window = builder.get_object("window1")
if window is None:
print('*** GLADE VCP ERROR: No window named "window1" found. The toplevel windows has to be named "window1"!')
LOG.error('GLADE VCP ERROR: No window named "window1" found. The toplevel windows has to be named "window1"!')
sys.exit(0)

window.set_title(opts.component)

try:
halcomp = hal.component(opts.component)
except:
print("*** GLADE VCP ERROR: Asking for a HAL component using a name that already exists.", file=sys.stderr)
LOG.error("GLADE VCP ERROR: Asking for a HAL component using a name that already exists.")
sys.exit(0)

panel = gladevcp.makepins.GladePanel( halcomp, xmlname, builder, None)
Expand Down Expand Up @@ -304,7 +306,7 @@ def main():
pos = j[2].partition("+")
window.move( int(pos[0]), int(pos[2]) )
except:
print("**** GLADE VCP ERROR: With window position data", file=sys.stderr)
LOG.error("GLADE VCP ERROR: With window position data")
parser.print_usage()
sys.exit(1)
if "x" in opts.geometry:
Expand All @@ -316,17 +318,17 @@ def main():
t = window_geometry.partition("x")
window.resize( int(t[0]), int(t[2]) )
except:
print("**** GLADE VCP ERROR: With window resize data", file=sys.stderr)
LOG.error("GLADE VCP ERROR: With window resize data")
parser.print_usage()
sys.exit(1)

if opts.gtk_rc:
LOG.debug( "**** GLADE VCP INFO: %s reading gtkrc file '%s'" %(opts.component,opts.gtk_rc))
LOG.debug( "GLADE VCP INFO: %s reading gtkrc file '%s'" %(opts.component,opts.gtk_rc))
Gtk.rc_add_default_file(opts.gtk_rc)
Gtk.rc_parse(opts.gtk_rc)

if opts.theme:
LOG.debug("**** GLADE VCP INFO: Switching %s to '%s' theme" %(opts.component,opts.theme))
LOG.debug("GLADE VCP INFO: Switching %s to '%s' theme" %(opts.component,opts.theme))
settings = Gtk.Settings.get_default()
settings.set_string_property("gtk-theme-name", opts.theme, "")

Expand All @@ -353,7 +355,7 @@ def main():
# push the XWindow id number to standard out
if opts.push_XID or opts.parent:
w_id = window.get_property('window').get_xid()
print(w_id, file=sys.stdout)
LOG.debug(f"XID: {w_id}")
sys.stdout.flush()

if signal_func in handlers:
Expand All @@ -372,7 +374,7 @@ def main():
Gdk.flush()
error = Gdk.error_trap_pop()
if error and opts.debug:
print("**** GLADE VCP ERROR: X Protocol Error: %s" % str(error), file=sys.stderr)
LOG.error("GLADE VCP ERROR: X Protocol Error: %s" % str(error))


if __name__ == '__main__':
Expand Down

0 comments on commit a03b084

Please sign in to comment.