-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlog.c
130 lines (120 loc) · 3.56 KB
/
log.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
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
/*
* Copyright (C) 2019-2024 Nicola Di Lieto <[email protected]>
*
* This file is part of uacme.
*
* uacme 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.
*
* uacme 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 this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
#include "log.h"
void log_stderr(int priority, const char *format, ...)
{
va_list ap;
const char *pri;
switch (priority) {
case LOG_DEBUG:
pri = "DEBUG";
break;
case LOG_INFO:
pri = "INFO";
break;
case LOG_NOTICE:
pri = "NOTICE";
break;
case LOG_WARNING:
pri = "WARNING";
break;
case LOG_ERR:
pri = "ERR";
break;
case LOG_CRIT:
pri = "CRIT";
break;
default:
pri = "UNKNOWN";
}
fprintf(stderr, "%ld [%s] ", (long)getpid(), pri);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fputc('\n', stderr);
}
static void (*log_func)(int priority, const char *format, ...) = log_stderr;
void set_log_func(void (*f)(int priority, const char *format, ...)) {
log_func = f;
}
static void vmsgx(int priority, const char *format, va_list ap)
{
int errno_save = errno;
char *buf = NULL;
size_t buf_size = 0;
FILE *f = open_memstream(&buf, &buf_size);
if (!f)
log_func(LOG_ERR, "vmsg(%s, ...): open_memstream: %s",
format, strerror(errno));
else if (vfprintf(f, format, ap) < 0)
log_func(LOG_ERR, "vmsg(%s, ...): vfprintf: %s",
format, strerror(errno));
else if (fflush(f) != 0)
log_func(LOG_ERR, "vmsgx(%s, ...): fflush: %s",
format, strerror(errno));
else
log_func(priority, "%s", buf);
if (f)
fclose(f);
free(buf);
errno = errno_save;
return;
}
static void vmsg(int priority, const char *format, va_list ap)
{
int errno_save = errno;
char *buf = NULL;
size_t buf_size = 0;
FILE *f = open_memstream(&buf, &buf_size);
if (!f)
log_func(LOG_ERR, "vmsg(%s, ...): open_memstream: %s",
format, strerror(errno));
else if (vfprintf(f, format, ap) < 0)
log_func(LOG_ERR, "vmsg(%s, ...): vfprintf: %s",
format, strerror(errno));
else if (fprintf(f, ": %s", strerror(errno_save)) < 0)
log_func(LOG_ERR, "vmsg(%s, ...): fprintf: %s",
format, strerror(errno));
else if (fflush(f) != 0)
log_func(LOG_ERR, "vmsgx(%s, ...): fflush: %s",
format, strerror(errno));
else
log_func(priority, "%s", buf);
if (f)
fclose(f);
free(buf);
errno = errno_save;
return;
}
DEFINE_LOG_FUNC(debug, LOG_DEBUG)
DEFINE_LOG_FUNC(info, LOG_INFO)
DEFINE_LOG_FUNC(notice, LOG_NOTICE)
DEFINE_LOG_FUNC(warn, LOG_WARNING)
DEFINE_LOG_FUNC(err, LOG_ERR)
DEFINE_LOG_FUNC(crit, LOG_CRIT)