1 |
pcg |
1.1 |
/* |
2 |
|
|
slog.C -- logging |
3 |
pcg |
1.4 |
Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> |
4 |
pcg |
1.1 |
|
5 |
pcg |
1.4 |
This file is part of GVPE. |
6 |
|
|
|
7 |
|
|
GVPE is free software; you can redistribute it and/or modify |
8 |
pcg |
1.1 |
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
pcg |
1.4 |
along with gvpe; if not, write to the Free Software |
19 |
pcg |
1.5 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
pcg |
1.1 |
*/ |
21 |
|
|
|
22 |
|
|
#include <cstdarg> |
23 |
|
|
#include <cstdio> |
24 |
|
|
#include <cstring> |
25 |
|
|
#include <cstdlib> |
26 |
|
|
|
27 |
|
|
#include <unistd.h> |
28 |
|
|
#include <syslog.h> |
29 |
|
|
|
30 |
|
|
#include "slog.h" |
31 |
|
|
|
32 |
|
|
loglevel log_level = L_INFO; |
33 |
|
|
const char *log_identity = ""; |
34 |
|
|
static int logto = LOGTO_STDERR; |
35 |
|
|
|
36 |
|
|
loglevel string_to_loglevel (const char *s) |
37 |
|
|
{ |
38 |
|
|
if (!strcmp (s, "noise")) return L_NOISE; |
39 |
|
|
if (!strcmp (s, "trace")) return L_TRACE; |
40 |
|
|
if (!strcmp (s, "debug")) return L_DEBUG; |
41 |
|
|
if (!strcmp (s, "info")) return L_INFO; |
42 |
|
|
if (!strcmp (s, "notice")) return L_NOTICE; |
43 |
|
|
if (!strcmp (s, "warn")) return L_WARN; |
44 |
|
|
if (!strcmp (s, "error")) return L_ERR; |
45 |
|
|
if (!strcmp (s, "critical")) return L_CRIT; |
46 |
|
|
|
47 |
|
|
return L_NONE; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
void log_to (int mask) |
51 |
|
|
{ |
52 |
|
|
if (logto & LOGTO_SYSLOG) |
53 |
|
|
closelog (); |
54 |
|
|
|
55 |
|
|
logto = mask; |
56 |
|
|
|
57 |
|
|
if (logto & LOGTO_SYSLOG) |
58 |
|
|
openlog (log_identity, LOG_CONS | LOG_PID, LOG_DAEMON); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
void slog_ (const loglevel l, const char *m, ...) |
62 |
|
|
{ |
63 |
|
|
if (l >= log_level) |
64 |
|
|
{ |
65 |
|
|
va_list ap; |
66 |
|
|
va_start (ap, m); |
67 |
|
|
char *msg = new char [2048]; |
68 |
|
|
|
69 |
|
|
vsnprintf (msg, 2048, m, ap); |
70 |
|
|
|
71 |
|
|
if (logto & LOGTO_SYSLOG) |
72 |
|
|
{ |
73 |
|
|
int lvl = l == L_TRACE ? LOG_DEBUG |
74 |
|
|
: l == L_DEBUG ? LOG_DEBUG |
75 |
|
|
: l == L_INFO ? LOG_INFO |
76 |
|
|
: l == L_NOTICE ? LOG_NOTICE |
77 |
|
|
: l == L_ERR ? LOG_ERR |
78 |
|
|
: l == L_CRIT ? LOG_CRIT |
79 |
|
|
: LOG_ERR; |
80 |
|
|
|
81 |
|
|
syslog (lvl, "%s", msg); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
if (logto & LOGTO_STDERR) |
85 |
|
|
{ |
86 |
|
|
write (2, msg, strlen (msg)); |
87 |
|
|
write (2, "\n", 1); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
delete msg; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
void fatal (const char *m) |
95 |
|
|
{ |
96 |
|
|
slog (L_CRIT, m); |
97 |
pcg |
1.3 |
exit (EXIT_FAILURE); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
extern void require_failed (const char *file, int line, const char *info) |
101 |
|
|
{ |
102 |
|
|
slog (L_CRIT, "FATAL: This program encountered a SHOULD NOT HAPPEN condition and will exit:"); |
103 |
|
|
slog (L_CRIT, "FATAL+ %s:%d '%s' is false", file, line, info); |
104 |
|
|
slog (L_CRIT, "FATAL+ This might indicates a bug in this program, a bug in your libraries,"); |
105 |
|
|
slog (L_CRIT, "FATAL+ your system setup or operating system. Or it might indicate a very"); |
106 |
|
|
slog (L_CRIT, "FATAL+ unusual, unanticipated operating condition, library version mismatch"); |
107 |
|
|
slog (L_CRIT, "FATAL+ or similar problem. If it's not obvious to you what was causing it,"); |
108 |
|
|
slog (L_CRIT, "FATAL+ then please report this to the program author(s)."); |
109 |
|
|
exit (126); |
110 |
pcg |
1.1 |
} |
111 |
|
|
|