blob: 4df529b86385f3d8a52b93c1b52a7416ccf2328e [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerrc7e764a2019-05-28 16:49:03 +08002
3#include <stdarg.h>
4#include <stdio.h>
5
6#include "libmctp.h"
7#include "libmctp-log.h"
8
9#ifdef HAVE_CONFIG_H
10#include "config.h"
11#endif
12
13#ifdef MCTP_HAVE_SYSLOG
14#include <syslog.h>
15#endif
16
17enum {
18 MCTP_LOG_NONE,
19 MCTP_LOG_STDIO,
20 MCTP_LOG_SYSLOG,
21 MCTP_LOG_CUSTOM,
22} log_type = MCTP_LOG_NONE;
23
24static int log_stdio_level;
25static void (*log_custom_fn)(int, const char *, va_list);
26
27void mctp_prlog(int level, const char *fmt, ...)
28{
29 va_list ap;
30
31 va_start(ap, fmt);
32
33 switch (log_type) {
34 case MCTP_LOG_NONE:
35 break;
36 case MCTP_LOG_STDIO:
37 if (level <= log_stdio_level) {
38 vfprintf(stderr, fmt, ap);
39 fputs("\n", stderr);
40 }
41 break;
42 case MCTP_LOG_SYSLOG:
43#ifdef MCTP_HAVE_SYSLOG
44 vsyslog(level, fmt, ap);
45#endif
46 break;
47 case MCTP_LOG_CUSTOM:
48 log_custom_fn(level, fmt, ap);
49 break;
50 }
51
52 va_end(ap);
53}
54
55void mctp_set_log_stdio(int level)
56{
57 log_type = MCTP_LOG_STDIO;
58 log_stdio_level = level;
59}
60
61void mctp_set_log_syslog(void)
62{
63 log_type = MCTP_LOG_SYSLOG;
64}
65
66void mctp_set_log_custom(void (*fn)(int, const char *, va_list))
67{
68 log_type = MCTP_LOG_CUSTOM;
69 log_custom_fn = fn;
70}