1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | * |
---|
19 | * Authors: William Jon McCann <mccann@jhu.edu> |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | #include <string.h> |
---|
27 | #include <stdarg.h> |
---|
28 | #include <signal.h> |
---|
29 | #include <time.h> |
---|
30 | #include <unistd.h> |
---|
31 | |
---|
32 | #include <syslog.h> |
---|
33 | |
---|
34 | #include <glib.h> |
---|
35 | #include <glib/gstdio.h> |
---|
36 | |
---|
37 | #include "gdm-log.h" |
---|
38 | |
---|
39 | static gboolean initialized = FALSE; |
---|
40 | static int syslog_levels = (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING); |
---|
41 | |
---|
42 | static void |
---|
43 | log_level_to_priority_and_prefix (GLogLevelFlags log_level, |
---|
44 | int *priorityp, |
---|
45 | const char **prefixp) |
---|
46 | { |
---|
47 | int priority; |
---|
48 | const char *prefix; |
---|
49 | |
---|
50 | /* Process the message prefix and priority */ |
---|
51 | switch (log_level & G_LOG_LEVEL_MASK) { |
---|
52 | case G_LOG_FLAG_FATAL: |
---|
53 | priority = LOG_EMERG; |
---|
54 | prefix = "FATAL"; |
---|
55 | break; |
---|
56 | case G_LOG_LEVEL_ERROR: |
---|
57 | priority = LOG_ERR; |
---|
58 | prefix = "ERROR"; |
---|
59 | break; |
---|
60 | case G_LOG_LEVEL_CRITICAL: |
---|
61 | priority = LOG_CRIT; |
---|
62 | prefix = "CRITICAL"; |
---|
63 | break; |
---|
64 | case G_LOG_LEVEL_WARNING: |
---|
65 | priority = LOG_WARNING; |
---|
66 | prefix = "WARNING"; |
---|
67 | break; |
---|
68 | case G_LOG_LEVEL_MESSAGE: |
---|
69 | priority = LOG_NOTICE; |
---|
70 | prefix = "MESSAGE"; |
---|
71 | break; |
---|
72 | case G_LOG_LEVEL_INFO: |
---|
73 | priority = LOG_INFO; |
---|
74 | prefix = "INFO"; |
---|
75 | break; |
---|
76 | case G_LOG_LEVEL_DEBUG: |
---|
77 | /* if debug was requested then bump this up to ERROR |
---|
78 | * to ensure it is seen in a log */ |
---|
79 | if (syslog_levels & G_LOG_LEVEL_DEBUG) { |
---|
80 | priority = LOG_WARNING; |
---|
81 | prefix = "DEBUG(+)"; |
---|
82 | } else { |
---|
83 | priority = LOG_DEBUG; |
---|
84 | prefix = "DEBUG"; |
---|
85 | } |
---|
86 | break; |
---|
87 | default: |
---|
88 | priority = LOG_DEBUG; |
---|
89 | prefix = "UNKNOWN"; |
---|
90 | break; |
---|
91 | } |
---|
92 | |
---|
93 | if (priorityp != NULL) { |
---|
94 | *priorityp = priority; |
---|
95 | } |
---|
96 | if (prefixp != NULL) { |
---|
97 | *prefixp = prefix; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void |
---|
102 | gdm_log_default_handler (const gchar *log_domain, |
---|
103 | GLogLevelFlags log_level, |
---|
104 | const gchar *message, |
---|
105 | gpointer unused_data) |
---|
106 | { |
---|
107 | GString *gstring; |
---|
108 | int priority; |
---|
109 | const char *level_prefix; |
---|
110 | char *string; |
---|
111 | gboolean do_log; |
---|
112 | gboolean is_fatal; |
---|
113 | |
---|
114 | is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0; |
---|
115 | |
---|
116 | do_log = (log_level & syslog_levels); |
---|
117 | if (! do_log) { |
---|
118 | return; |
---|
119 | } |
---|
120 | |
---|
121 | if (! initialized) { |
---|
122 | gdm_log_init (); |
---|
123 | } |
---|
124 | |
---|
125 | log_level_to_priority_and_prefix (log_level, |
---|
126 | &priority, |
---|
127 | &level_prefix); |
---|
128 | |
---|
129 | gstring = g_string_new (NULL); |
---|
130 | |
---|
131 | if (log_domain != NULL) { |
---|
132 | g_string_append (gstring, log_domain); |
---|
133 | g_string_append_c (gstring, '-'); |
---|
134 | } |
---|
135 | g_string_append (gstring, level_prefix); |
---|
136 | |
---|
137 | g_string_append (gstring, ": "); |
---|
138 | if (message == NULL) { |
---|
139 | g_string_append (gstring, "(NULL) message"); |
---|
140 | } else { |
---|
141 | g_string_append (gstring, message); |
---|
142 | } |
---|
143 | if (is_fatal) { |
---|
144 | g_string_append (gstring, "\naborting...\n"); |
---|
145 | } else { |
---|
146 | g_string_append (gstring, "\n"); |
---|
147 | } |
---|
148 | |
---|
149 | string = g_string_free (gstring, FALSE); |
---|
150 | |
---|
151 | syslog (priority, "%s", string); |
---|
152 | |
---|
153 | g_free (string); |
---|
154 | } |
---|
155 | |
---|
156 | void |
---|
157 | gdm_log_toggle_debug (void) |
---|
158 | { |
---|
159 | if (syslog_levels & G_LOG_LEVEL_DEBUG) { |
---|
160 | g_debug ("Debugging disabled"); |
---|
161 | syslog_levels &= ~G_LOG_LEVEL_DEBUG; |
---|
162 | } else { |
---|
163 | syslog_levels |= G_LOG_LEVEL_DEBUG; |
---|
164 | g_debug ("Debugging enabled"); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void |
---|
169 | gdm_log_set_debug (gboolean debug) |
---|
170 | { |
---|
171 | if (debug) { |
---|
172 | syslog_levels |= G_LOG_LEVEL_DEBUG; |
---|
173 | g_debug ("Enabling debugging"); |
---|
174 | } else { |
---|
175 | g_debug ("Disabling debugging"); |
---|
176 | syslog_levels &= ~G_LOG_LEVEL_DEBUG; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | void |
---|
181 | gdm_log_init (void) |
---|
182 | { |
---|
183 | const char *prg_name; |
---|
184 | int options; |
---|
185 | |
---|
186 | g_log_set_default_handler (gdm_log_default_handler, NULL); |
---|
187 | |
---|
188 | prg_name = g_get_prgname (); |
---|
189 | |
---|
190 | options = LOG_PID; |
---|
191 | #ifdef LOG_PERROR |
---|
192 | options |= LOG_PERROR; |
---|
193 | #endif |
---|
194 | |
---|
195 | openlog (prg_name, options, LOG_DAEMON); |
---|
196 | |
---|
197 | initialized = TRUE; |
---|
198 | } |
---|
199 | |
---|
200 | void |
---|
201 | gdm_log_shutdown (void) |
---|
202 | { |
---|
203 | closelog (); |
---|
204 | initialized = FALSE; |
---|
205 | } |
---|
206 | |
---|