Merge lp://staging/~ted/indicator-applet/log-to-file into lp://staging/indicator-applet/0.4

Proposed by Ted Gould
Status: Merged
Merged at revision: not available
Proposed branch: lp://staging/~ted/indicator-applet/log-to-file
Merge into: lp://staging/indicator-applet/0.4
Diff against target: 100 lines (+69/-0)
1 file modified
src/applet-main.c (+69/-0)
To merge this branch: bzr merge lp://staging/~ted/indicator-applet/log-to-file
Reviewer Review Type Date Requested Status
Neil J. Patel (community) Approve
Review via email: mp+17695@code.staging.launchpad.net
To post a comment you must log in.
Revision history for this message
Ted Gould (ted) wrote :

Making it so the logs go out to a file in an async fashion. This should
get us some debug info as things come up.

Revision history for this message
Neil J. Patel (njpatel) wrote :

Log files are for wimps. Real men guess what errors happened.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/applet-main.c'
2--- src/applet-main.c 2010-01-19 05:29:03 +0000
3+++ src/applet-main.c 2010-01-19 22:31:11 +0000
4@@ -20,6 +20,7 @@
5 with this program. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8+#include <stdlib.h>
9 #include <config.h>
10 #include <panel-applet.h>
11
12@@ -73,6 +74,20 @@
13 #endif
14
15 /*************
16+ * log files
17+ * ***********/
18+#ifdef INDICATOR_APPLET
19+#define LOG_FILE_NAME "indicator-applet.log"
20+#endif
21+#ifdef INDICATOR_APPLET_SESSION
22+#define LOG_FILE_NAME "indicator-applet-session.log"
23+#endif
24+#ifdef INDICATOR_APPLET_COMPLETE
25+#define LOG_FILE_NAME "indicator-applet-complete.log"
26+#endif
27+GOutputStream * log_file = NULL;
28+
29+/*************
30 * init function
31 * ***********/
32
33@@ -378,6 +393,58 @@
34 #endif
35 #define N_(x) x
36
37+static void
38+log_to_file_cb (GObject * source_obj, GAsyncResult * result, gpointer user_data)
39+{
40+ g_free(user_data);
41+ return;
42+}
43+
44+static void
45+log_to_file (const gchar * domain, GLogLevelFlags level, const gchar * message, gpointer data)
46+{
47+ if (log_file == NULL) {
48+ GError * error = NULL;
49+ gchar * filename = g_build_path(g_get_user_cache_dir(), LOG_FILE_NAME, NULL);
50+ GFile * file = g_file_new_for_path(filename);
51+ g_free(filename);
52+
53+ if (!g_file_test(g_get_user_cache_dir(), G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
54+ GFile * cachedir = g_file_new_for_path(g_get_user_cache_dir());
55+ g_file_make_directory_with_parents(cachedir, NULL, &error);
56+
57+ if (error != NULL) {
58+ g_error("Unable to make directory '%s' for log file: %s", g_get_user_cache_dir(), error->message);
59+ return;
60+ }
61+ }
62+
63+ g_file_delete(file, NULL, NULL);
64+
65+ GFileIOStream * io = g_file_create_readwrite(file,
66+ G_FILE_CREATE_REPLACE_DESTINATION, /* flags */
67+ NULL, /* cancelable */
68+ &error); /* error */
69+ if (error != NULL) {
70+ g_error("Unable to replace file: %s", error->message);
71+ return;
72+ }
73+
74+ log_file = g_io_stream_get_output_stream(G_IO_STREAM(io));
75+ }
76+
77+ gchar * outputstring = g_strdup_printf("%s\n", message);
78+ g_output_stream_write_async(log_file,
79+ outputstring, /* data */
80+ strlen(outputstring), /* length */
81+ G_PRIORITY_LOW, /* priority */
82+ NULL, /* cancelable */
83+ log_to_file_cb, /* callback */
84+ outputstring); /* data */
85+
86+ return;
87+}
88+
89 static gboolean
90 applet_fill_cb (PanelApplet * applet, const gchar * iid, gpointer data)
91 {
92@@ -415,6 +482,8 @@
93 #ifdef INDICATOR_APPLET_COMPLETE
94 g_set_application_name(_("Indicator Applet Complete"));
95 #endif
96+
97+ g_log_set_default_handler(log_to_file, NULL);
98 }
99
100 /* Set panel options */

Subscribers

People subscribed via source and target branches

to status/vote changes: