Merge lp:~rainct/zeitgeist/custom-log-handler into lp:~zeitgeist/zeitgeist/bluebird

Proposed by Siegfried Gevatter
Status: Merged
Approved by: Michal Hruby
Approved revision: 407
Merged at revision: 407
Proposed branch: lp:~rainct/zeitgeist/custom-log-handler
Merge into: lp:~zeitgeist/zeitgeist/bluebird
Prerequisite: lp:~rainct/zeitgeist/matches-template
Diff against target: 210 lines (+145/-32)
3 files modified
src/Makefile.am (+1/-0)
src/logging.vala (+140/-0)
src/zeitgeist-daemon.vala (+4/-32)
To merge this branch: bzr merge lp:~rainct/zeitgeist/custom-log-handler
Reviewer Review Type Date Requested Status
Michal Hruby (community) Approve
Review via email: mp+93289@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

73 + if ((log_levels & LogLevelFlags.LEVEL_DEBUG) != 0)
74 + output = stdout;

Only WARNING and higher levels should be logged to stderr.

review: Needs Fixing
Revision history for this message
Michal Hruby (mhr3) wrote :

77 + output.printf ("** %s: %s\n", log_level, message);

Also, one thing I dont like about glib's default log handler is that it doesn't print time, could we do that?

Revision history for this message
Siegfried Gevatter (rainct) wrote :

2012/2/17 Michal Hruby <email address hidden>:
> Only WARNING and higher levels should be logged to stderr.

Currently it's sending anything but debug to stderr. I tried :P.

(But if you want to change that, fine with me).

> Also, one thing I dont like about glib's default log handler is that
> it doesn't print time, could we do that?

To the file, sure (it's in the commented code).

To std{out,err}, wth would you do that, it's just cluttering stuff up.

Revision history for this message
Michal Hruby (mhr3) wrote :

> 2012/2/17 Michal Hruby <email address hidden>:
> > Only WARNING and higher levels should be logged to stderr.
>
> Currently it's sending anything but debug to stderr. I tried :P.
>

Yes, I wanted INFO and MESSAGE to also go to stdout.

> (But if you want to change that, fine with me).
>
> > Also, one thing I dont like about glib's default log handler is that
> > it doesn't print time, could we do that?
>
> To the file, sure (it's in the commented code).
>
> To std{out,err}, wth would you do that, it's just cluttering stuff up.

It's pretty valuable when you're just reading the log and wondering if something happened in the same millisecond or a couple of hours apart.

406. By Siegfried Gevatter

Leave va_list there but don't use it.

407. By Siegfried Gevatter

Move logging code from zeitgeist-daemon to a new logging.vala and install
a custom log handler (adding color and timestamps).

Revision history for this message
Michal Hruby (mhr3) wrote :

Wooo, that will be nice! :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Makefile.am'
2--- src/Makefile.am 2012-02-02 18:57:35 +0000
3+++ src/Makefile.am 2012-02-19 20:51:17 +0000
4@@ -37,6 +37,7 @@
5 extension.vala \
6 extension-collection.vala \
7 extension-store.vala \
8+ logging.vala \
9 notify.vala \
10 sql.vala \
11 utils.vala \
12
13=== added file 'src/logging.vala'
14--- src/logging.vala 1970-01-01 00:00:00 +0000
15+++ src/logging.vala 2012-02-19 20:51:17 +0000
16@@ -0,0 +1,140 @@
17+/* logging.vala
18+ *
19+ * Copyright © 2012 Canonical Ltd.
20+ * By Siegfried-A. Gevatter <siegfried.gevatter@collabora.co.uk>
21+ *
22+ * This program is free software: you can redistribute it and/or modify
23+ * it under the terms of the GNU Lesser General Public License as published by
24+ * the Free Software Foundation, either version 2.1 of the License, or
25+ * (at your option) any later version.
26+ *
27+ * This program is distributed in the hope that it will be useful,
28+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30+ * GNU General Public License for more details.
31+ *
32+ * You should have received a copy of the GNU Lesser General Public License
33+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
34+ *
35+ */
36+
37+namespace Zeitgeist
38+{
39+ namespace Logging
40+ {
41+
42+ private const string BLUE = "\x1b[34m";
43+ private const string GREEN = "\x1b[32m";
44+ private const string RED = "\x1b[31m";
45+ private const string YELLOW = "\x1b[33m";
46+ private const string RESET = "\x1b[0m";
47+
48+ private string get_log_level_string (LogLevelFlags log_levels,
49+ out string color)
50+ {
51+ string log_level;
52+
53+ if ((log_levels & LogLevelFlags.LEVEL_ERROR) != 0)
54+ {
55+ log_level = "ERROR";
56+ color = RED;
57+ }
58+ else if ((log_levels & LogLevelFlags.LEVEL_CRITICAL) != 0)
59+ {
60+ log_level = "CRITICAL";
61+ color = RED;
62+ }
63+ else if ((log_levels & LogLevelFlags.LEVEL_WARNING) != 0)
64+ {
65+ log_level = "WARNING";
66+ color = RED;
67+ }
68+ else if ((log_levels & LogLevelFlags.LEVEL_MESSAGE) != 0)
69+ {
70+ log_level = "MESSAGE";
71+ color = BLUE;
72+ }
73+ else if ((log_levels & LogLevelFlags.LEVEL_INFO) != 0)
74+ {
75+ log_level = "INFO";
76+ color = BLUE;
77+ }
78+ else if ((log_levels & LogLevelFlags.LEVEL_DEBUG) != 0)
79+ {
80+ log_level = "DEBUG";
81+ color = GREEN;
82+ }
83+ else
84+ {
85+ log_level = "UNKNOWN";
86+ color = BLUE;
87+ }
88+
89+ return log_level;
90+ }
91+
92+ private void log_handler (string? log_domain, LogLevelFlags log_levels,
93+ string message)
94+ {
95+ string color;
96+ string log_level = get_log_level_string (log_levels, out color);
97+ string timestamp = TimeVal ().to_iso8601 ().substring (11, 15);
98+ //string datestamp = new DateTime.now_local ().format (
99+ // "%Y-%m-%d %H:%M:%S");
100+ // FIXME: get PID
101+
102+ unowned FileStream output;
103+ if (log_levels >= LogLevelFlags.LEVEL_MESSAGE)
104+ output = stdout; // MESSAGE, INFO or DEBUG
105+ else
106+ output = stderr;
107+
108+ // Print to console
109+ output.printf ("%s[%s %s]%s %s\n", color, timestamp,
110+ log_level, RESET, message);
111+
112+ // Log to file
113+ // FIXME:
114+ //printf ("[%s] - %s - %s\n", datestamp, log_level, message);
115+ }
116+
117+ public void setup_logging (string? log_level)
118+ {
119+ LogLevelFlags discarded = LogLevelFlags.LEVEL_DEBUG;
120+ if (log_level != null)
121+ {
122+ var ld = LogLevelFlags.LEVEL_DEBUG;
123+ var li = LogLevelFlags.LEVEL_INFO;
124+ var lm = LogLevelFlags.LEVEL_MESSAGE;
125+ var lw = LogLevelFlags.LEVEL_WARNING;
126+ var lc = LogLevelFlags.LEVEL_CRITICAL;
127+ switch (log_level.up ())
128+ {
129+ case "DEBUG":
130+ discarded = 0;
131+ break;
132+ case "INFO":
133+ discarded = ld;
134+ break;
135+ case "WARNING":
136+ discarded = ld | li | lm;
137+ break;
138+ case "CRITICAL":
139+ discarded = ld | li | lm | lw;
140+ break;
141+ case "ERROR":
142+ discarded = ld | li | lm | lw | lc;
143+ break;
144+ }
145+ }
146+ if (discarded != 0)
147+ Log.set_handler (null, discarded, () => {});
148+
149+ LogLevelFlags logged = ~discarded & ~LogLevelFlags.FLAG_RECURSION;
150+ Log.set_handler (null, logged, log_handler);
151+ }
152+
153+ }
154+}
155+
156+// vim:expandtab:ts=4:sw=4
157
158=== modified file 'src/zeitgeist-daemon.vala'
159--- src/zeitgeist-daemon.vala 2012-02-07 12:46:46 +0000
160+++ src/zeitgeist-daemon.vala 2012-02-19 20:51:17 +0000
161@@ -5,6 +5,8 @@
162 * Copyright © 2011 Collabora Ltd.
163 * By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
164 * By Seif Lotfy <seif@lotfy.com>
165+ * Copyright © 2012 Canonical Ltd.
166+ * By Siegfried-A. Gevatter <siegfried.gevatter@collabora.co.uk>
167 *
168 * This program is free software: you can redistribute it and/or modify
169 * it under the terms of the GNU Lesser General Public License as published by
170@@ -449,38 +451,8 @@
171
172 return 0;
173 }
174-
175- LogLevelFlags discarded = LogLevelFlags.LEVEL_DEBUG;
176- if (log_level != null)
177- {
178- var ld = LogLevelFlags.LEVEL_DEBUG;
179- var li = LogLevelFlags.LEVEL_INFO;
180- var lm = LogLevelFlags.LEVEL_MESSAGE;
181- var lw = LogLevelFlags.LEVEL_WARNING;
182- var lc = LogLevelFlags.LEVEL_CRITICAL;
183- switch (log_level.up ())
184- {
185- case "DEBUG":
186- discarded = 0;
187- break;
188- case "INFO":
189- discarded = ld;
190- break;
191- case "WARNING":
192- discarded = ld | li | lm;
193- break;
194- case "CRITICAL":
195- discarded = ld | li | lm | lw;
196- break;
197- case "ERROR":
198- discarded = ld | li | lm | lw | lc;
199- break;
200- }
201- }
202- if (discarded != 0)
203- {
204- Log.set_handler ("", discarded, () => {});
205- }
206+
207+ Logging.setup_logging (log_level);
208
209 run ();
210 }

Subscribers

People subscribed via source and target branches