-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogging.c
95 lines (77 loc) · 2.42 KB
/
logging.c
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
/*
* Copyright (C) 2016 Ingemar Ådahl
*
* This file is part of remote-input.
*
* remote-input 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.
*
* remote-input 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 remote-input. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logging.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#define CLAMP(val, low, high) (val > high ? high : (val < low ? low : val))
static int log_level = LOG_NOTICE;
static enum log_target selected_target = STDIO;
void log_set_level(int level) {
log_level = CLAMP(level, LOG_ALERT, LOG_DEBUG);
if (selected_target == SYSLOG) {
setlogmask(LOG_UPTO(log_level));
}
}
PRINTF_TYPE(2,3)
static void stdio_log(int priority, const char* format, ...) {
if (priority > log_level) {
return;
}
va_list args;
va_start(args, format);
FILE* stream = priority < LOG_WARNING ? stderr : stdout;
fflush(stdout);
fflush(stderr);
vfprintf(stream, format, args);
fprintf(stream, "\n");
va_end(args);
}
PRINTF_TYPE(2,3)
void(*__log_function)(int priority, const char* format, ...) = &stdio_log;
void log_set_target(enum log_target target) {
if (selected_target == target) {
return;
}
selected_target = target;
if (selected_target == STDIO) {
closelog();
__log_function = &stdio_log;
return;
}
if (selected_target == SYSLOG) {
openlog(NULL, LOG_NDELAY | LOG_PID, LOG_USER);
setlogmask(LOG_UPTO(log_level));
__log_function = &syslog;
}
}
void __log_errno(const char* file, int line, const char* format, ...) {
char msg_buf[128];
va_list args;
va_start(args, format);
vsnprintf(msg_buf, sizeof(msg_buf), format, args);
va_end(args);
if (file != NULL && line >= 0) {
LOG(ERROR, "%s:%d: %s: %s", file, line, msg_buf, strerror(errno));
return;
}
LOG(ERROR, "%s: %s", msg_buf, strerror(errno));
}