Merge lp:~victored/granite/logger-cleanup into lp:~elementary-pantheon/granite/granite

Proposed by Victor Martinez
Status: Merged
Approved by: xapantu
Approved revision: 872
Merged at revision: 872
Proposed branch: lp:~victored/granite/logger-cleanup
Merge into: lp:~elementary-pantheon/granite/granite
Diff against target: 184 lines (+47/-50)
1 file modified
lib/Services/Logger.vala (+47/-50)
To merge this branch: bzr merge lp:~victored/granite/logger-cleanup
Reviewer Review Type Date Requested Status
xapantu (community) Approve
Review via email: mp+269597@code.launchpad.net

Commit message

[Services.Logger] Fix formatting issues in log messages. Fixes lp:1487199

- Add performance and multi-threading optimizations courtesy of ricotz.
- Only show last part of log level. e.g. _LOG_LEVEL_FATAL => FATAL.
- Removed 'Application will not function properly' messages that trigger after a fatal error occurs.
- Removed unused regex and format_message function.

To post a comment you must log in.
Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Removing the microseconds has only disadvantages imho.

FYI, https://git.launchpad.net/plank/commit/?id=b62c1c76b31208b066a169be58ccd4af6caf4ecc

Revision history for this message
Victor Martinez (victored) wrote :

Hey nice. I'm definitely merging your changes.

I can see how microseconds can be useful for simple profiling. I'll add them back.

Revision history for this message
Victor Martinez (victored) wrote :

Rico,

I've merged your improvements to the best of my ability. I've only made minor changes due to small enum differences in the API. I also added microseconds back. Is there anything else I should change here?

Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Log.set_default_handler ((GLib.LogFunc) glib_log_func);

Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Squash your branch and use a decent commit message.

Revision history for this message
Victor Martinez (victored) wrote :

Thanks, I've added the explicit cast. I hope the commit message is decent enough now :P

lp:~victored/granite/logger-cleanup updated
872. By Victor Martinez

Fix formatting issues in log messages from Granite.Logger. Fixes lp:1487199

- Add performance and multithreading optimizations courtesy of ricotz.
- Only show last part of log level. e.g. _LOG_LEVEL_FATAL => FATAL.
- Removed 'Application will not function properly' messages that trigger after a fatal error occurs. They don't provide useful information and only create noise.
- Removed unused regex and format_message function.

Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Dropping "trailing" commas in enum-declarations is not needed, iirc valac can handle this since 0.16

Revision history for this message
Victor Martinez (victored) wrote :

That explains why it still compiled. That change is kinda non negotiable for me. I know you personally wouldn't leave those trailing commas in your code either.

Revision history for this message
Rico Tzschichholz (ricotz) wrote :

OK, your choice, and yeah I tend to omit it too.

Revision history for this message
xapantu (xapantu) wrote :

For the record I am not convinced at all that using micro seconds is a good way to do (even simple) profiling, that only works if the app is run app bare metal (without any threads and kernel) -.-

Revision history for this message
xapantu (xapantu) :
review: Approve

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 2014-09-04 12:39:00 +0000
3+++ lib/Services/Logger.vala 2015-08-31 07:31:08 +0000
4@@ -1,5 +1,6 @@
5 /*
6 * Copyright (C) 2011-2013 Robert Dyer
7+ * 2015 elementary LLC, Rico Tzschichholz
8 *
9 * This program or library is free software; you can redistribute it
10 * and/or modify it under the terms of the GNU Lesser General Public
11@@ -51,7 +52,7 @@
12 /**
13 * This level should be used only in cases of unrecoverable errors.
14 */
15- FATAL,
16+ FATAL
17 }
18
19 enum ConsoleColor {
20@@ -62,7 +63,7 @@
21 BLUE,
22 MAGENTA,
23 CYAN,
24- WHITE,
25+ WHITE
26 }
27
28 /**
29@@ -70,73 +71,58 @@
30 *
31 */
32 public class Logger : GLib.Object {
33-
34+ const string[] LOG_LEVEL_TO_STRING = {
35+ "DEBUG",
36+ "INFO",
37+ "NOTIFY",
38+ "WARNING",
39+ "ERROR",
40+ "FATAL"
41+ };
42+
43 /**
44 * This is used to determine which level of LogLevelling should be used.
45 */
46 public static LogLevel DisplayLevel { get; set; default = LogLevel.WARN; }
47-
48- /**
49- * The name of the app that is logging.
50- */
51- static string AppName { get; set; }
52-
53- static Regex re;
54-
55+
56+ static Mutex write_mutex;
57+
58 /**
59 * This method initializes the Logger
60 *
61 * @param app_name name of app that is logging
62 */
63 public static void initialize (string app_name) {
64-
65- AppName = app_name;
66- /*try {
67- re = new Regex ("""(.*)\.vala(:\d+): (.*)""");
68- } catch { }*/
69-
70- Log.set_default_handler (glib_log_func);
71- }
72-
73- /**
74- * Formats a message to be logged
75- *
76- * @param msg message to be formatted
77- */
78- static string format_message (string msg) {
79-
80- if (re != null && re.match (msg)) {
81- var parts = re.split (msg);
82- return "[%s%s] %s".printf (parts[1], parts[2], parts[3]);
83- }
84- return msg;
85- }
86-
87+ Log.set_default_handler ((GLib.LogFunc) glib_log_func);
88+ }
89+
90 /**
91 * Logs message using Notify level formatting
92 *
93 * @param msg message to be logged
94 */
95 public static void notification (string msg) {
96- write (LogLevel.NOTIFY, format_message (msg));
97+ write (LogLevel.NOTIFY, msg);
98 }
99
100 static string get_time () {
101-
102 var now = new GLib.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+ static void write (LogLevel level, owned string msg) {
108
109 if (level < DisplayLevel)
110 return;
111-
112+
113+ write_mutex.lock ();
114 set_color_for_level (level);
115- stdout.printf ("[%s %s]", level.to_string ().substring (16), get_time ());
116+ stdout.printf ("[%s %s]", LOG_LEVEL_TO_STRING[level], get_time ());
117
118 reset_color ();
119 stdout.printf (" %s\n", msg);
120+
121+ write_mutex.unlock ();
122 }
123
124 static void set_color_for_level (LogLevel level) {
125@@ -185,37 +171,48 @@
126 }
127
128 static void glib_log_func (string? d, LogLevelFlags flags, string msg) {
129- var domain = "";
130+ string domain;
131 if (d != null)
132 domain = "[%s] ".printf (d);
133-
134- var message = msg.replace ("\n", "").replace ("\r", "");
135- message = "%s%s".printf (domain, message);
136-
137+ else
138+ domain = "";
139+
140+ string message;
141+ if (msg.contains ("\n") || msg.contains ("\r"))
142+ message = "%s%s".printf (domain, msg.replace ("\n", "").replace ("\r", ""));
143+ else
144+ message = "%s%s".printf (domain, msg);
145+
146+ LogLevel level;
147+
148+ // Strip internal flags to make it possible to use a switch statement
149+ flags = (flags & LogLevelFlags.LEVEL_MASK);
150+
151 switch (flags) {
152 case LogLevelFlags.LEVEL_CRITICAL:
153- write (LogLevel.FATAL, format_message (message));
154- write (LogLevel.FATAL, format_message (AppName + " will not function properly."));
155+ level = LogLevel.FATAL;
156 break;
157
158 case LogLevelFlags.LEVEL_ERROR:
159- write (LogLevel.ERROR, format_message (message));
160+ level = LogLevel.ERROR;
161 break;
162
163 case LogLevelFlags.LEVEL_INFO:
164 case LogLevelFlags.LEVEL_MESSAGE:
165- write (LogLevel.INFO, format_message (message));
166+ level = LogLevel.INFO;
167 break;
168
169 case LogLevelFlags.LEVEL_DEBUG:
170- write (LogLevel.DEBUG, format_message (message));
171+ level = LogLevel.DEBUG;
172 break;
173
174 case LogLevelFlags.LEVEL_WARNING:
175 default:
176- write (LogLevel.WARN, format_message (message));
177+ level = LogLevel.WARN;
178 break;
179 }
180+
181+ write (level, message);
182 }
183
184 }

Subscribers

People subscribed via source and target branches