Merge lp:~marcus-lundgren/granite/fix-1008009-1012077 into lp:~elementary-pantheon/granite/granite

Proposed by Marcus Lundgren
Status: Superseded
Proposed branch: lp:~marcus-lundgren/granite/fix-1008009-1012077
Merge into: lp:~elementary-pantheon/granite/granite
Diff against target: 372 lines (+153/-205)
1 file modified
lib/Services/Logger.vala (+153/-205)
To merge this branch: bzr merge lp:~marcus-lundgren/granite/fix-1008009-1012077
Reviewer Review Type Date Requested Status
xapantu (community) Approve
Review via email: mp+109945@code.launchpad.net

This proposal has been superseded by a proposal from 2012-06-17.

To post a comment you must log in.
Revision history for this message
Sergey "Shnatsel" Davidoff (shnatsel) wrote :

This is in fact a direct copypaste from Varka and a fix for bug #958914, the root cause of those bugs

Revision history for this message
xapantu (xapantu) wrote :

Looks good to me :)

Feel free to merge it if it works :)

review: Approve
267. By Marcus Lundgren

Improved text in the copyright notice to reflect that the code is from the Varka library.

268. By Marcus Lundgren

It should be Varka Library.

269. By Marcus Lundgren

Replaced function bodies with the ones present in plank.

270. By Marcus Lundgren

Cleaning up the diff.

271. By Marcus Lundgren

Cleaning up the diff.

272. By Marcus Lundgren

'Fixed' the problem with renaming files. Read comments in the source for information.

273. By Marcus Lundgren

Changed parameters in print_log.

274. By Marcus Lundgren

Clean up.

275. By Marcus Lundgren

Clean up.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/Services/Logger.vala'
2--- lib/Services/Logger.vala 2011-11-08 21:45:07 +0000
3+++ lib/Services/Logger.vala 2012-06-17 15:54:17 +0000
4@@ -1,5 +1,8 @@
5 //
6 // Copyright (C) 2011 Robert Dyer
7+//
8+// Originally Written by Robert Dyer for Varka Library
9+// Varka Library: http://www.launchpad.net/varka
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13@@ -17,210 +20,155 @@
14
15 namespace Granite.Services {
16
17- public enum LogLevel {
18- DEBUG,
19- INFO,
20- NOTIFY,
21- WARN,
22- ERROR,
23- FATAL,
24- }
25-
26- enum ConsoleColor {
27- BLACK,
28- RED,
29- GREEN,
30- YELLOW,
31- BLUE,
32- MAGENTA,
33- CYAN,
34- WHITE,
35- }
36-
37- /**
38- * An enhanced GLib logger which all {@link Granite.Application}s use.
39- */
40- public class Logger : GLib.Object {
41-
42- class LogMessage : GLib.Object {
43-
44- public LogLevel Level { get; private set; }
45- public string Message { get; private set; }
46-
47- public LogMessage (LogLevel level, string message) {
48- Level = level;
49- Message = message;
50- }
51-
52- }
53-
54- public static LogLevel DisplayLevel { get; set; default = LogLevel.WARN; }
55-
56- static string AppName { get; set; }
57-
58- static Object queue_lock = null;
59-
60- static List<LogMessage> log_queue;
61- static bool is_writing;
62-
63- static Regex re;
64-
65- /**
66- * Initializes the logger with the supplied application name.
67- *
68- * @param app_name the name of the app to display in the logs
69- */
70- public static void initialize (string app_name) {
71-
72- AppName = app_name;
73- is_writing = false;
74- log_queue = new List<LogMessage> ();
75- try {
76- re = new Regex ("""(.*)\.vala(:\d+): (.*)""");
77- } catch { }
78-
79- Log.set_default_handler (glib_log_func);
80- }
81-
82- static string format_message (string msg) {
83-
84- if (re != null && re.match (msg)) {
85- var parts = re.split (msg);
86- return "[%s%s] %s".printf (parts[1], parts[2], parts[3]);
87- }
88- return msg;
89- }
90-
91- /**
92- * Convenience method to write a message with LogLevel.NOTIFY.
93- *
94- * @param msg the message to write to the log
95- */
96- public static void notification (string msg) {
97- write (LogLevel.NOTIFY, format_message (msg));
98- }
99-
100- static string get_time () {
101-
102- var now = new DateTime.now_local ();
103- return "%.2d:%.2d:%.2d.%.6d".printf (now.get_hour (), now.get_minute (), now.get_second (), now.get_microsecond ());
104- }
105-
106- static void write (LogLevel level, string msg) {
107-
108- if (level < DisplayLevel)
109- return;
110-
111- if (is_writing) {
112- lock (queue_lock)
113- log_queue.append (new LogMessage (level, msg));
114- } else {
115- is_writing = true;
116-
117- if (log_queue.length () > 0) {
118- var logs = log_queue.copy ();
119- lock (queue_lock)
120- log_queue = new List<LogMessage> ();
121-
122- foreach (var log in logs)
123- print_log (log);
124- }
125-
126- print_log (new LogMessage (level, msg));
127-
128- is_writing = false;
129- }
130- }
131-
132- static void print_log (LogMessage log) {
133-
134- set_color_for_level (log.Level);
135- stdout.printf ("[%s %s]", log.Level.to_string ().substring (27), get_time ());
136-
137- reset_color ();
138- stdout.printf (" %s\n", log.Message);
139- }
140-
141- static void set_color_for_level (LogLevel level) {
142-
143- switch (level) {
144- case LogLevel.DEBUG:
145- set_foreground (ConsoleColor.GREEN);
146- break;
147- case LogLevel.INFO:
148- set_foreground (ConsoleColor.BLUE);
149- break;
150- case LogLevel.NOTIFY:
151- set_foreground (ConsoleColor.MAGENTA);
152- break;
153- case LogLevel.WARN:
154- set_foreground (ConsoleColor.YELLOW);
155- break;
156- case LogLevel.ERROR:
157- set_foreground (ConsoleColor.RED);
158- break;
159- case LogLevel.FATAL:
160- set_background (ConsoleColor.RED);
161- set_foreground (ConsoleColor.WHITE);
162- break;
163- }
164- }
165-
166- static void reset_color () {
167- stdout.printf ("\x001b[0m");
168- }
169-
170- static void set_foreground (ConsoleColor color) {
171- set_color (color, true);
172- }
173-
174- static void set_background (ConsoleColor color) {
175- set_color (color, false);
176- }
177-
178- static void set_color (ConsoleColor color, bool isForeground) {
179-
180- var color_code = color + 30 + 60;
181- if (!isForeground)
182- color_code += 10;
183- stdout.printf ("\x001b[%dm", color_code);
184- }
185-
186- static void glib_log_func (string? d, LogLevelFlags flags, string msg) {
187- var domain = "";
188- if (d != null)
189- domain = "[%s] ".printf (d);
190-
191- var message = msg.replace ("\n", "").replace ("\r", "");
192- message = "%s%s".printf (domain, message);
193-
194- switch (flags) {
195- case LogLevelFlags.LEVEL_CRITICAL:
196- write (LogLevel.FATAL, format_message (message));
197- write (LogLevel.FATAL, format_message (AppName + " will not function properly."));
198- break;
199-
200- case LogLevelFlags.LEVEL_ERROR:
201- write (LogLevel.ERROR, format_message (message));
202- break;
203-
204- case LogLevelFlags.LEVEL_INFO:
205- case LogLevelFlags.LEVEL_MESSAGE:
206- write (LogLevel.INFO, format_message (message));
207- break;
208-
209- case LogLevelFlags.LEVEL_DEBUG:
210- write (LogLevel.DEBUG, format_message (message));
211- break;
212-
213- case LogLevelFlags.LEVEL_WARNING:
214- default:
215- write (LogLevel.WARN, format_message (message));
216- break;
217- }
218- }
219-
220- }
221-
222+ public enum LogLevel {
223+ DEBUG,
224+ INFO,
225+ NOTIFY,
226+ WARN,
227+ ERROR,
228+ FATAL,
229+ }
230+
231+ enum ConsoleColor {
232+ BLACK,
233+ RED,
234+ GREEN,
235+ YELLOW,
236+ BLUE,
237+ MAGENTA,
238+ CYAN,
239+ WHITE,
240+ }
241+
242+ public class Logger : GLib.Object {
243+
244+ public static LogLevel DisplayLevel { get; set; default = LogLevel.WARN; }
245+
246+ static string AppName { get; set; }
247+
248+ static Regex re;
249+
250+ public static void initialize (string app_name) {
251+
252+ AppName = app_name;
253+ /*try {
254+ re = new Regex ("""(.*)\.vala(:\d+): (.*)""");
255+ } catch { }*/
256+
257+ Log.set_default_handler (glib_log_func);
258+ }
259+
260+ static string format_message (string msg) {
261+
262+ if (re != null && re.match (msg)) {
263+ var parts = re.split (msg);
264+ return "[%s%s] %s".printf (parts[1], parts[2], parts[3]);
265+ }
266+ return msg;
267+ }
268+
269+ public static void notification (string msg) {
270+ write (LogLevel.NOTIFY, format_message (msg));
271+ }
272+
273+ static string get_time () {
274+
275+ var now = new DateTime.now_local ();
276+ return "%.2d:%.2d:%.2d.%.6d".printf (now.get_hour (), now.get_minute (), now.get_second (), now.get_microsecond ());
277+ }
278+
279+ static void write (LogLevel level, string msg) {
280+
281+ if (level < DisplayLevel)
282+ return;
283+
284+ set_color_for_level (level);
285+ stdout.printf ("[%s %s]", level.to_string ().substring (16), get_time ());
286+
287+ reset_color ();
288+ stdout.printf (" %s\n", msg);
289+ }
290+
291+ static void set_color_for_level (LogLevel level) {
292+
293+ switch (level) {
294+ case LogLevel.DEBUG:
295+ set_foreground (ConsoleColor.GREEN);
296+ break;
297+ case LogLevel.INFO:
298+ set_foreground (ConsoleColor.BLUE);
299+ break;
300+ case LogLevel.NOTIFY:
301+ set_foreground (ConsoleColor.MAGENTA);
302+ break;
303+ case LogLevel.WARN:
304+ set_foreground (ConsoleColor.YELLOW);
305+ break;
306+ case LogLevel.ERROR:
307+ set_foreground (ConsoleColor.RED);
308+ break;
309+ case LogLevel.FATAL:
310+ set_background (ConsoleColor.RED);
311+ set_foreground (ConsoleColor.WHITE);
312+ break;
313+ }
314+ }
315+
316+ static void reset_color () {
317+ stdout.printf ("\x001b[0m");
318+ }
319+
320+ static void set_foreground (ConsoleColor color) {
321+ set_color (color, true);
322+ }
323+
324+ static void set_background (ConsoleColor color) {
325+ set_color (color, false);
326+ }
327+
328+ static void set_color (ConsoleColor color, bool isForeground) {
329+
330+ var color_code = color + 30 + 60;
331+ if (!isForeground)
332+ color_code += 10;
333+ stdout.printf ("\x001b[%dm", color_code);
334+ }
335+
336+ static void glib_log_func (string? d, LogLevelFlags flags, string msg) {
337+ var domain = "";
338+ if (d != null)
339+ domain = "[%s] ".printf (d);
340+
341+ var message = msg.replace ("\n", "").replace ("\r", "");
342+ message = "%s%s".printf (domain, message);
343+
344+ switch (flags) {
345+ case LogLevelFlags.LEVEL_CRITICAL:
346+ write (LogLevel.FATAL, format_message (message));
347+ write (LogLevel.FATAL, format_message (AppName + " will not function properly."));
348+ break;
349+
350+ case LogLevelFlags.LEVEL_ERROR:
351+ write (LogLevel.ERROR, format_message (message));
352+ break;
353+
354+ case LogLevelFlags.LEVEL_INFO:
355+ case LogLevelFlags.LEVEL_MESSAGE:
356+ write (LogLevel.INFO, format_message (message));
357+ break;
358+
359+ case LogLevelFlags.LEVEL_DEBUG:
360+ write (LogLevel.DEBUG, format_message (message));
361+ break;
362+
363+ case LogLevelFlags.LEVEL_WARNING:
364+ default:
365+ write (LogLevel.WARN, format_message (message));
366+ break;
367+ }
368+ }
369+
370+ }
371+
372 }
373

Subscribers

People subscribed via source and target branches