-
Notifications
You must be signed in to change notification settings - Fork 13
/
pn_log.c
179 lines (151 loc) · 4.48 KB
/
pn_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright (C) 2007-2009 Felipe Contreras
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "pn_log.h"
#include "pn_printf.h"
#ifdef PN_DEBUG
/* #define PURPLE_DEBUG */
/* #define PN_DEBUG_FILE */
#include <fcntl.h>
#include <unistd.h>
#include <glib/gstdio.h>
#ifdef PURPLE_DEBUG
/* libpurple stuff. */
#include <debug.h>
#endif /* PURPLE_DEBUG */
static inline const gchar *
log_level_to_string (PecanLogLevel level)
{
switch (level)
{
case PN_LOG_LEVEL_NONE: return "NONE"; break;
case PN_LOG_LEVEL_ERROR: return "ERROR"; break;
case PN_LOG_LEVEL_WARNING: return "WARNING"; break;
case PN_LOG_LEVEL_INFO: return "INFO"; break;
case PN_LOG_LEVEL_DEBUG: return "DEBUG"; break;
case PN_LOG_LEVEL_LOG: return "LOG"; break;
case PN_LOG_LEVEL_TEST: return "TEST"; break;
default: return "Unknown"; break;
}
}
#ifdef PN_DUMP_FILE
void
pn_dump_file (const gchar *buffer,
gsize len)
{
gint fd;
static guint c;
gchar *basename;
gchar *fullname;
basename = pn_strdup_printf ("pecan-%.6u.bin", c++);
fullname = g_build_filename (g_get_tmp_dir (), basename, NULL);
g_free (basename);
fd = g_open (fullname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd)
{
write (fd, buffer, len);
close (fd);
}
}
#endif /* PN_DUMP_FILE */
void
pn_base_log_helper (guint level,
const gchar *file,
const gchar *function,
gint line,
const gchar *fmt,
...)
{
gchar *tmp;
va_list args;
gboolean console_print = FALSE;
if (level > PECAN_LOG_LEVEL && level != PN_LOG_LEVEL_TEST)
return;
va_start (args, fmt);
#if defined(PECAN_DEVEL)
console_print = TRUE;
#else
if (level == PN_LOG_LEVEL_TEST)
console_print = TRUE;
#endif
#if defined(PURPLE_DEBUG)
PurpleDebugLevel purple_level;
PurpleDebugUiOps *ops;
switch (level) {
case PN_LOG_LEVEL_ERROR:
purple_level = PURPLE_DEBUG_ERROR; break;
case PN_LOG_LEVEL_WARNING:
purple_level = PURPLE_DEBUG_WARNING; break;
case PN_LOG_LEVEL_INFO:
purple_level = PURPLE_DEBUG_INFO; break;
case PN_LOG_LEVEL_DEBUG:
case PN_LOG_LEVEL_TEST:
purple_level = PURPLE_DEBUG_MISC; break;
case PN_LOG_LEVEL_LOG:
purple_level = PURPLE_DEBUG_MISC; break;
default:
purple_level = PURPLE_DEBUG_MISC; break;
}
if (purple_debug_is_enabled())
console_print = TRUE;
ops = purple_debug_get_ui_ops();
if (!console_print) {
if (!ops || !ops->print ||
(ops->is_enabled && !ops->is_enabled(purple_level, "msn-pecan")))
{
return;
}
}
#endif
tmp = pn_strdup_vprintf (fmt, args);
#if defined(PN_DEBUG_FILE)
{
static FILE *logfile;
if (!logfile)
{
gint fd;
fd = g_file_open_tmp ("msn-pecan-XXXXXX", NULL, NULL);
if (fd)
logfile = fdopen (fd, "w");
}
if (logfile)
{
g_fprintf (logfile, "%s\t%s:%d:%s()\t%s\n",
log_level_to_string (level),
file, line, function,
tmp);
}
}
#else
#if defined(PURPLE_DEBUG)
if (level <= PN_LOG_LEVEL_INFO || level == PN_LOG_LEVEL_TEST) {
char *arg_s;
arg_s = g_strdup_printf("%s\n", tmp);
ops->print(purple_level, "msn-pecan", arg_s);
g_free(arg_s);
}
#endif
#endif /* PN_DEBUG_FILE */
if (console_print)
g_print ("%s %s:%d:%s() %s\n",
log_level_to_string (level),
file, line, function,
tmp);
g_free (tmp);
va_end (args);
}
#endif /* PN_DEBUG */