-
Notifications
You must be signed in to change notification settings - Fork 23
/
log.c
75 lines (61 loc) · 1.19 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
/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
#include <stdarg.h>
#include "libmctp.h"
#include "libmctp-log.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef MCTP_HAVE_STDIO
#include <stdio.h>
#endif
#ifdef MCTP_HAVE_SYSLOG
#include <syslog.h>
#endif
enum {
MCTP_LOG_NONE,
MCTP_LOG_STDIO,
MCTP_LOG_SYSLOG,
MCTP_LOG_CUSTOM,
} log_type = MCTP_LOG_NONE;
static int log_stdio_level;
static void (*log_custom_fn)(int, const char *, va_list);
void mctp_prlog(int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
switch (log_type) {
case MCTP_LOG_NONE:
break;
case MCTP_LOG_STDIO:
#ifdef MCTP_HAVE_STDIO
if (level <= log_stdio_level) {
vfprintf(stderr, fmt, ap);
fputs("\n", stderr);
}
#endif
break;
case MCTP_LOG_SYSLOG:
#ifdef MCTP_HAVE_SYSLOG
vsyslog(level, fmt, ap);
#endif
break;
case MCTP_LOG_CUSTOM:
log_custom_fn(level, fmt, ap);
break;
}
va_end(ap);
}
void mctp_set_log_stdio(int level)
{
log_type = MCTP_LOG_STDIO;
log_stdio_level = level;
}
void mctp_set_log_syslog(void)
{
log_type = MCTP_LOG_SYSLOG;
}
void mctp_set_log_custom(void (*fn)(int, const char *, va_list))
{
log_type = MCTP_LOG_CUSTOM;
log_custom_fn = fn;
}