ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/slog.C
Revision: 1.6
Committed: Thu Aug 7 17:54:27 2008 UTC (15 years, 9 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_2, rel-2_21, rel-2_22
Changes since 1.5: +24 -14 lines
Log Message:
update to gplv3, finally

File Contents

# User Rev Content
1 pcg 1.1 /*
2     slog.C -- logging
3 pcg 1.6 Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de>
4 pcg 1.1
5 pcg 1.4 This file is part of GVPE.
6    
7 pcg 1.6 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 pcg 1.1 */
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 string_to_loglevel (const char *s)
47     {
48     if (!strcmp (s, "noise")) return L_NOISE;
49     if (!strcmp (s, "trace")) return L_TRACE;
50     if (!strcmp (s, "debug")) return L_DEBUG;
51     if (!strcmp (s, "info")) return L_INFO;
52     if (!strcmp (s, "notice")) return L_NOTICE;
53     if (!strcmp (s, "warn")) return L_WARN;
54     if (!strcmp (s, "error")) return L_ERR;
55     if (!strcmp (s, "critical")) return L_CRIT;
56    
57     return L_NONE;
58     }
59    
60     void log_to (int mask)
61     {
62     if (logto & LOGTO_SYSLOG)
63     closelog ();
64    
65     logto = mask;
66    
67     if (logto & LOGTO_SYSLOG)
68     openlog (log_identity, LOG_CONS | LOG_PID, LOG_DAEMON);
69     }
70    
71     void slog_ (const loglevel l, const char *m, ...)
72     {
73     if (l >= log_level)
74     {
75     va_list ap;
76     va_start (ap, m);
77     char *msg = new char [2048];
78    
79     vsnprintf (msg, 2048, m, ap);
80    
81     if (logto & LOGTO_SYSLOG)
82     {
83     int lvl = l == L_TRACE ? LOG_DEBUG
84     : l == L_DEBUG ? LOG_DEBUG
85     : l == L_INFO ? LOG_INFO
86     : l == L_NOTICE ? LOG_NOTICE
87     : l == L_ERR ? LOG_ERR
88     : l == L_CRIT ? LOG_CRIT
89     : LOG_ERR;
90    
91     syslog (lvl, "%s", msg);
92     }
93    
94     if (logto & LOGTO_STDERR)
95     {
96     write (2, msg, strlen (msg));
97     write (2, "\n", 1);
98     }
99    
100     delete msg;
101     }
102     }
103    
104     void fatal (const char *m)
105     {
106     slog (L_CRIT, m);
107 pcg 1.3 exit (EXIT_FAILURE);
108     }
109    
110     extern void require_failed (const char *file, int line, const char *info)
111     {
112     slog (L_CRIT, "FATAL: This program encountered a SHOULD NOT HAPPEN condition and will exit:");
113     slog (L_CRIT, "FATAL+ %s:%d '%s' is false", file, line, info);
114     slog (L_CRIT, "FATAL+ This might indicates a bug in this program, a bug in your libraries,");
115     slog (L_CRIT, "FATAL+ your system setup or operating system. Or it might indicate a very");
116     slog (L_CRIT, "FATAL+ unusual, unanticipated operating condition, library version mismatch");
117     slog (L_CRIT, "FATAL+ or similar problem. If it's not obvious to you what was causing it,");
118     slog (L_CRIT, "FATAL+ then please report this to the program author(s).");
119     exit (126);
120 pcg 1.1 }
121