1 /*******************************************************************************
2 **3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
3 ** 10 20 30 40 50 60 70 80
4 **
5 ** notify-osd
6 **
7 ** log.h - log utils
8 **
9 ** Copyright 2009 Canonical Ltd.
10 **
11 ** Authors:
12 ** Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
13 ** David Barth <david.barth@canonical.com>
14 **
15 ** Contributor(s):
16 ** Sense "qense" Hofstede <qense@ubuntu.com> (fix LP: #465801, rev. 415)
17 **
18 ** This program is free software: you can redistribute it and/or modify it
19 ** under the terms of the GNU General Public License version 3, as published
20 ** by the Free Software Foundation.
21 **
22 ** This program is distributed in the hope that it will be useful, but
23 ** WITHOUT ANY WARRANTY; without even the implied warranties of
24 ** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
25 ** PURPOSE. See the GNU General Public License for more details.
26 **
27 ** You should have received a copy of the GNU General Public License along
28 ** with this program. If not, see <http://www.gnu.org/licenses/>.
29 **
30 *******************************************************************************/
31
32 #include "config.h"
33 #include <sys/time.h>
34 #include <time.h>
35 #include <stdio.h>
36 #include <glib.h>
37
38 #include "bubble.h"
39
40 static FILE *logfile = NULL;
41
42 static void
log_logger_null(const char * domain,GLogLevelFlags log_level,const char * message,gpointer user_data)43 log_logger_null(const char *domain,
44 GLogLevelFlags log_level,
45 const char *message,
46 gpointer user_data)
47 {
48 return;
49 }
50
51 void
log_init(void)52 log_init (void)
53 {
54 if (! g_getenv ("LOG"))
55 return;
56
57 const char *homedir = g_getenv ("HOME");
58 if (!homedir)
59 homedir = g_get_home_dir ();
60 g_return_if_fail (homedir != NULL);
61
62 // Make sure the cache directory is there, if at all possible
63 const gchar *dirname = g_get_user_cache_dir ();
64 g_mkdir_with_parents (dirname, 0700);
65
66 char *filename = g_build_filename (dirname, "notify-osd.log", NULL);
67
68 logfile = fopen (filename, "w");
69 if (logfile == NULL)
70 g_warning ("could not open/append to %s; logging disabled",
71 filename);
72
73 g_free (filename);
74
75 /* discard all debug messages unless DEBUG is set */
76 if (! g_getenv ("DEBUG"))
77 g_log_set_handler (NULL,
78 G_LOG_LEVEL_MESSAGE
79 | G_LOG_FLAG_FATAL
80 | G_LOG_FLAG_RECURSION,
81 log_logger_null, NULL);
82
83 g_message ("DEBUG mode enabled");
84 }
85
86
87 char*
log_create_timestamp(void)88 log_create_timestamp (void)
89 {
90 struct timeval tv;
91 struct tm *tm;
92
93 /* FIXME: deal with tz offsets */
94 gettimeofday (&tv, NULL);
95 tm = localtime (&tv.tv_sec);
96
97 return g_strdup_printf ("%.4d-%.2d-%.2dT%.2d:%.2d:%.2d%.1s%.2d:%.2d",
98 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
99 tm->tm_hour, tm->tm_min, tm->tm_sec,
100 "-", 0, 0);
101 }
102
103 void
log_bubble(Bubble * bubble,const char * app_name,const char * option)104 log_bubble (Bubble *bubble, const char *app_name, const char *option)
105 {
106 g_return_if_fail (IS_BUBBLE (bubble));
107
108 if (logfile == NULL)
109 return;
110
111 char *ts = log_create_timestamp ();
112
113 if (option)
114 fprintf (logfile, "[%s, %s %s] %s\n",
115 ts, app_name, option,
116 bubble_get_title (bubble));
117 else
118 fprintf (logfile, "[%s, %s] %s\n",
119 ts, app_name,
120 bubble_get_title (bubble));
121
122 fprintf (logfile, "%s\n\n",
123 bubble_get_message_body (bubble));
124
125 fflush (logfile);
126
127 g_free (ts);
128 }
129
130 void
log_bubble_debug(Bubble * bubble,const char * app_name,const char * icon)131 log_bubble_debug (Bubble *bubble, const char *app_name, const char *icon)
132 {
133 g_return_if_fail (bubble != NULL && IS_BUBBLE (bubble));
134
135 char *ts = log_create_timestamp ();
136
137 g_debug ("[%s, %s, id:%d, icon:%s%s] %s\n%s\n",
138 ts, app_name, bubble_get_id (bubble), icon,
139 bubble_is_synchronous (bubble) ? " (synchronous)" : "",
140 bubble_get_title (bubble),
141 bubble_get_message_body (bubble));
142
143 g_free (ts);
144 }
145