ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/slog.C
Revision: 1.3
Committed: Thu Jan 29 18:55:10 2004 UTC (20 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_1_6, rel-1_7, VPE-1_6_1
Changes since 1.2: +13 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     slog.C -- logging
3 pcg 1.2 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
4 pcg 1.1
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    
20     #include <cstdarg>
21     #include <cstdio>
22     #include <cstring>
23     #include <cstdlib>
24    
25     #include <unistd.h>
26     #include <syslog.h>
27    
28     #include "slog.h"
29    
30     loglevel log_level = L_INFO;
31     const char *log_identity = "";
32     static int logto = LOGTO_STDERR;
33    
34     loglevel string_to_loglevel (const char *s)
35     {
36     if (!strcmp (s, "noise")) return L_NOISE;
37     if (!strcmp (s, "trace")) return L_TRACE;
38     if (!strcmp (s, "debug")) return L_DEBUG;
39     if (!strcmp (s, "info")) return L_INFO;
40     if (!strcmp (s, "notice")) return L_NOTICE;
41     if (!strcmp (s, "warn")) return L_WARN;
42     if (!strcmp (s, "error")) return L_ERR;
43     if (!strcmp (s, "critical")) return L_CRIT;
44    
45     return L_NONE;
46     }
47    
48     void log_to (int mask)
49     {
50     if (logto & LOGTO_SYSLOG)
51     closelog ();
52    
53     logto = mask;
54    
55     if (logto & LOGTO_SYSLOG)
56     openlog (log_identity, LOG_CONS | LOG_PID, LOG_DAEMON);
57     }
58    
59     void slog_ (const loglevel l, const char *m, ...)
60     {
61     if (l >= log_level)
62     {
63     va_list ap;
64     va_start (ap, m);
65     char *msg = new char [2048];
66    
67     vsnprintf (msg, 2048, m, ap);
68    
69     if (logto & LOGTO_SYSLOG)
70     {
71     int lvl = l == L_TRACE ? LOG_DEBUG
72     : l == L_DEBUG ? LOG_DEBUG
73     : l == L_INFO ? LOG_INFO
74     : l == L_NOTICE ? LOG_NOTICE
75     : l == L_ERR ? LOG_ERR
76     : l == L_CRIT ? LOG_CRIT
77     : LOG_ERR;
78    
79     syslog (lvl, "%s", msg);
80     }
81    
82     if (logto & LOGTO_STDERR)
83     {
84     write (2, msg, strlen (msg));
85     write (2, "\n", 1);
86     }
87    
88     delete msg;
89     }
90     }
91    
92     void fatal (const char *m)
93     {
94     slog (L_CRIT, m);
95 pcg 1.3 exit (EXIT_FAILURE);
96     }
97    
98     extern void require_failed (const char *file, int line, const char *info)
99     {
100     slog (L_CRIT, "FATAL: This program encountered a SHOULD NOT HAPPEN condition and will exit:");
101     slog (L_CRIT, "FATAL+ %s:%d '%s' is false", file, line, info);
102     slog (L_CRIT, "FATAL+ This might indicates a bug in this program, a bug in your libraries,");
103     slog (L_CRIT, "FATAL+ your system setup or operating system. Or it might indicate a very");
104     slog (L_CRIT, "FATAL+ unusual, unanticipated operating condition, library version mismatch");
105     slog (L_CRIT, "FATAL+ or similar problem. If it's not obvious to you what was causing it,");
106     slog (L_CRIT, "FATAL+ then please report this to the program author(s).");
107     exit (126);
108 pcg 1.1 }
109