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