File: log.c

package info (click to toggle)
conman 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,600 kB
  • sloc: ansic: 11,863; sh: 2,174; exp: 2,132; makefile: 270; perl: 120
file content (283 lines) | stat: -rw-r--r-- 7,213 bytes parent folder | download
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*****************************************************************************
 *  Written by Chris Dunlap <cdunlap@llnl.gov>.
 *  Copyright (C) 2007-2022 Lawrence Livermore National Security, LLC.
 *  Copyright (C) 2001-2007 The Regents of the University of California.
 *  UCRL-CODE-2002-009.
 *
 *  This file is part of ConMan: The Console Manager.
 *  For details, see <https://dun.github.io/conman/>.
 *
 *  ConMan 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 3 of the License, or (at your option)
 *  any later version.
 *
 *  ConMan 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 ConMan.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************
 *  Refer to "log.h" for documentation on public functions.
 *****************************************************************************/


#if HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "log.h"
#include "util-str.h"


#ifndef MAX_LINE
#  define MAX_LINE 1024
#endif /* !MAX_LINE */


static FILE * log_file_fp = NULL;
static int    log_file_priority = -1;
static int    log_file_timestamp = 0;
static int    log_syslog = 0;
static int    log_fd_daemonize = -1;


static void log_aux(int errnum, int priority, char *msgbug, int msgbuflen,
    const char *format, va_list vargs);

static const char * log_prefix(int priority);


void debug_printf(int level, const char *format, ...)
{
    static int debug_level = -1;
    va_list vargs;
    char *p;
    int i = 0;

    if (debug_level < 0) {
        if ((p = getenv("DEBUG"))) {
            i = atoi(p);
        }
        debug_level = (i > 0) ? i : 0;
    }
    if ((level > 0) && (level <= debug_level)) {
        va_start(vargs, format);
        vfprintf(stderr, format, vargs);
        va_end(vargs);
    }
    return;
}


void log_set_file(FILE *fp, int priority, int timestamp)
{
    if (fp && !ferror(fp)) {
        log_file_fp = fp;
        log_file_priority = (priority > 0) ? priority : 0;
        log_file_timestamp = !!timestamp;
        setvbuf(fp, NULL, _IONBF, 0);   /* set stream unbuffered */
    }
    else {
        log_file_fp = NULL;
        log_file_priority = -1;
        log_file_timestamp = 0;
    }
    return;
}


void log_set_syslog(char *ident, int facility)
{
    char *p;

    if (ident) {
        if ((p = strrchr(ident, '/'))) {
            ident = p + 1;
        }
        openlog(ident, LOG_NDELAY | LOG_PID, facility);
        log_syslog = 1;
    }
    else {
        closelog();
        log_syslog = 0;
    }
    return;
}


void log_set_err_pipe(int fd)
{
    log_fd_daemonize = (fd >= 0) ? fd : -1;
    return;
}


void log_err(int errnum, const char *format, ...)
{
    int priority = LOG_ERR;
    va_list vargs;
    char msg[MAX_LINE];
    signed char c;
    int n;
    char *p;

    va_start(vargs, format);
    log_aux(errnum, priority, msg, sizeof(msg), format, vargs);
    va_end(vargs);

    /*  Return error priority and message across "daemonize" pipe.
     */
    if (log_fd_daemonize >= 0) {
        c = (signed char) priority;
        n = write(log_fd_daemonize, &c, sizeof(c));
        if ((n > 0) && (msg[0] != '\0') && (log_file_fp != stderr)) {
            if ((p = strchr(msg, '\n'))) {
                *p = '\0';
            }
            /*  Ignore return value from write() instead of logging an error
             *    about failing to log an error.  Replaced void cast with
             *    useless assignment since compilation under rhel5 complained
             *    about ignoring return value of 'write'.
             */
            n = write(log_fd_daemonize, msg, strlen(msg) + 1);
        }
    }
#ifndef NDEBUG
    /*  Generate core for debugging.
     */
    if (getenv("DEBUG")) {
        abort();
    }
#endif /* !NDEBUG */

    exit(1);
}


void log_msg(int priority, const char *format, ...)
{
    va_list vargs;

    va_start(vargs, format);
    log_aux(0, priority, NULL, 0, format, vargs);
    va_end(vargs);

    return;
}


static void log_aux(int errnum, int priority, char *msgbuf, int msgbuflen,
    const char *format, va_list vargs)
{
    time_t t;
    struct tm tm;
    const char *prefix;
    char buf[MAX_LINE];                 /* buf starting with timestamp       */
    char *pbuf;                         /* buf starting with priority string */
    char *sbuf;                         /* buf starting with message         */
    char *p;
    int len;
    int n;

    p = sbuf = pbuf = buf;
    len = sizeof(buf) - 1;              /* reserve char for trailing newline */

    t = 0;
    get_localtime(&t, &tm);
    n = strftime(p, len, "%Y-%m-%d %H:%M:%S ", &tm);
    if (n == 0) {
        *p = '\0';
        len = 0;
    }
    p = sbuf = pbuf += n;
    len -= n;

    if ((len > 0) && (prefix = log_prefix(priority))) {
        int m = 10 - strlen(prefix);
        if (m <= 0) {
            m = 1;
        }
        assert(strlen(prefix) < 10);
        n = snprintf(p, len, "%s:%*c", prefix, m, 0x20);
        if ((n < 0) || (n >= len)) {
            n = len - 1;
        }
        p = sbuf += n;
        len -= n;
    }

    if (len > 0) {
        n = vsnprintf(p, len, format, vargs);
        if ((n < 0) || (n >= len)) {
            n = len - 1;
        }
        p += n;
        len -= n;
    }

    if (format[strlen(format) - 1] != '\n') {
        if ((len > 0) && (errnum > 0)) {
            n = snprintf(p, len, ": %s", strerror(errnum));
            if ((n < 0) || (n >= len)) {
                n = len - 1;
            }
            p += n;
            len -= n;
        }
        strcat(p, "\n");        /* space was reserved above for this newline */
    }

    if ((msgbuf != NULL) && (msgbuflen > 0)) {
        strncpy(msgbuf, sbuf, msgbuflen);
        msgbuf[msgbuflen - 1] = '\0';
    }

    if (log_syslog) {
        syslog(priority, "%s", sbuf);
    }
    if (log_file_fp && (priority <= log_file_priority)) {
        n = fprintf(log_file_fp, "%s", log_file_timestamp ? buf : pbuf);
        if (n == EOF) {
            syslog(LOG_CRIT, "Logging stopped due to error");
            log_file_fp = NULL;
        }
    }
    return;
}


static const char * log_prefix(int priority)
{
    switch (priority) {
    case LOG_EMERG:
        return("EMERGENCY");
    case LOG_ALERT:
        return("ALERT");
    case LOG_CRIT:
        return("CRITICAL");
    case LOG_ERR:
        return("ERROR");
    case LOG_WARNING:
        return("WARNING");
    case LOG_NOTICE:
        return("NOTICE");
    case LOG_INFO:
        return("INFO");
    case LOG_DEBUG:
        return("DEBUG");
    default:
        return("UNKNOWN");
    }
}