Merge lp:~kissiel/checkbox/touch-logging into lp:checkbox

Proposed by Maciej Kisielewski
Status: Merged
Approved by: Zygmunt Krynicki
Approved revision: 3571
Merged at revision: 3572
Proposed branch: lp:~kissiel/checkbox/touch-logging
Merge into: lp:checkbox
Diff against target: 141 lines (+116/-0)
3 files modified
checkbox-touch/components/PythonLogger.qml (+98/-0)
checkbox-touch/main.qml (+14/-0)
checkbox-touch/py/checkbox_touch.py (+4/-0)
To merge this branch: bzr merge lp:~kissiel/checkbox/touch-logging
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Review via email: mp+249036@code.launchpad.net

Description of the change

This MR brings new logging to Checkbox-Touch. This makes logging more robust and easier to manage.

536d2ef checkbox-touch: add python logger
PythonLogger component is defined that uses pyotherside to forward logging events to python logging module.

fb7207c checkbox-touch: export checkbox.touch.qml logger to qml

9ce6636 checkbox-touch: use PythonLogger in checkbox-touch
Second, PythonLogger is used in Checkbox-Touch, overriding console.log and console.error, making them send events to python logging.

To post a comment you must log in.
Revision history for this message
Zygmunt Krynicki (zyga) wrote :

This looks good, +1, some typos below

review: Needs Fixing
lp:~kissiel/checkbox/touch-logging updated
3569. By Maciej Kisielewski

checkbox-touch: add python logger

This patch introduces PythonLogger component that handles logging by sending it
through pyotherside to python, which in turn uses logging module to properly
record events. When this logger is created it overrides default behaviour of
console.log and console.error with python counterparts.

Signed-off-by: Maciej Kisielewski <email address hidden>

3570. By Maciej Kisielewski

checkbox-touch: export checkbox.touch.qml logger to qml

This patch makes checkbox.touch.qml logger available to QML as object compliant
to PythonObjectHandle component.

Signed-off-by: Maciej Kisielewski <email address hidden>

3571. By Maciej Kisielewski

checkbox-touch: use PythonLogger in checkbox-touch

This patch adds PythonLogger object to checkbox-touch. As console.log are now
pushed as logging.DEBUG and the logging level used is INFO, many messages
previously seen when using checkbox-touch are now silenced.

Signed-off-by: Maciej Kisielewski <email address hidden>

Revision history for this message
Maciej Kisielewski (kissiel) wrote :

> This looks good, +1, some typos below

Typos fixed. Repushed.

Revision history for this message
Zygmunt Krynicki (zyga) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'checkbox-touch/components/PythonLogger.qml'
--- checkbox-touch/components/PythonLogger.qml 1970-01-01 00:00:00 +0000
+++ checkbox-touch/components/PythonLogger.qml 2015-02-09 14:20:04 +0000
@@ -0,0 +1,98 @@
1/*
2 * This file is part of Checkbox
3 *
4 * Copyright 2015 Canonical Ltd.
5 *
6 * Authors:
7 * - Maciej Kisielewski <maciej.kisielewski@canonical.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 3.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22/*! \brief Python-driven logger
23 \inherits PythonObjectHandle
24
25 This component uses pyotherside to forward logging events to python.
26 It monkey-patches console.log and console.error to capture their calls
27 and forward those calls to python logger.
28*/
29import QtQuick 2.0
30
31PythonObjectHandle {
32
33 function debug(msg) {
34 invoke('debug', [msg], function() {});
35 }
36
37 function info(msg) {
38 invoke('info', [msg], function() {});
39 }
40
41 function warning(msg) {
42 invoke('warning', [msg], function() {});
43 }
44
45 function error(msg) {
46 invoke('error', [msg + "\nStacktrace:\n" + getStackTrace()], function() {});
47 }
48
49 function critical(msg) {
50 invoke('critical', [msg + "\nStacktrace:\n" + getStackTrace()], function() {});
51 }
52 /** get string containing pretty-formatted stack trace */
53 function getStackTrace() {
54 var stackTrace = "";
55 var callers = ((new Error).stack).split("\n");
56 callers.shift(); // remove current frame from ST (getStackTrace)
57 callers.shift(); // remove logging method frame from ST
58 for (var lvl in callers) {
59 stackTrace += "#" + lvl + " " + callers[lvl] + "\n";
60 }
61 return stackTrace;
62 }
63
64 /** Overridden invoke that doesn't log invoke calls */
65 function invoke(func, args, callback) {
66 if (py !== null && handle > 0) {
67 py.call("py_invoke", [handle, func, args], function(response) {
68 callback(response);
69 });
70 } else {
71 _original_console_error("unable to py_invoke: " + handle + ", " + func + ", " + JSON.stringify(args));
72 throw "py_invoke called without ready py and handle";
73 }
74 }
75
76 Component.onCompleted: {
77 /* save original logging facilities */
78 _original_console_log = console.log;
79 _original_console_error = console.error;
80 }
81
82 onHandleReady: {
83 /* monkey-patch console.log and console.error */
84 console.log = function() { debug(_argsToString(arguments)); };
85 console.error = function() { error(_argsToString(arguments)); };
86 debug("Python logger ready");
87 }
88
89 /** get string containing unpacked values from arguments object separated by spaces*/
90 function _argsToString(argsObj) {
91 var args = [];
92 for(var i = 0; i < argsObj.length; i++) args.push(argsObj[i]);
93 return args.join(" ");
94 }
95
96 property var _original_console_log
97 property var _original_console_error
98}
099
=== modified file 'checkbox-touch/main.qml'
--- checkbox-touch/main.qml 2015-01-29 14:58:11 +0000
+++ checkbox-touch/main.qml 2015-02-09 14:20:04 +0000
@@ -122,6 +122,20 @@
122 }122 }
123 }123 }
124124
125 PythonLogger {
126 id: logger
127 py: py
128 Component.onCompleted: {
129 py.Component.onCompleted.connect(function() {
130 py.importModule("checkbox_touch", function() {
131 py.call("checkbox_touch.get_qml_logger", [], function(handle) {
132 logger.handle = handle;
133 });
134 });
135 });
136 }
137 }
138
125 PageStack {139 PageStack {
126 id: pageStack140 id: pageStack
127 Component.onCompleted: push(welcomePage)141 Component.onCompleted: push(welcomePage)
128142
=== modified file 'checkbox-touch/py/checkbox_touch.py'
--- checkbox-touch/py/checkbox_touch.py 2015-02-06 15:51:40 +0000
+++ checkbox-touch/py/checkbox_touch.py 2015-02-09 14:20:04 +0000
@@ -799,5 +799,9 @@
799 return manager799 return manager
800800
801801
802def get_qml_logger():
803 return _manager.ref(logging.getLogger('checkbox.touch.qml'))
804
805
802create_app_object = CheckboxTouchApplication.create_and_get_handle806create_app_object = CheckboxTouchApplication.create_and_get_handle
803_manager = bootstrap()807_manager = bootstrap()

Subscribers

People subscribed via source and target branches