blob: cbdbbcaad1cda806f310749d4186f1f41029866a [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>
Jeremy Kerrc7e764a2019-05-28 16:49:03 +08004
5#include "libmctp.h"
6#include "libmctp-log.h"
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
Andrew Jeffery682232e2020-01-13 16:17:45 +103012#ifdef MCTP_HAVE_STDIO
13#include <stdio.h>
14#endif
15
Jeremy Kerrc7e764a2019-05-28 16:49:03 +080016#ifdef MCTP_HAVE_SYSLOG
17#include <syslog.h>
18#endif
19
20enum {
21 MCTP_LOG_NONE,
22 MCTP_LOG_STDIO,
23 MCTP_LOG_SYSLOG,
24 MCTP_LOG_CUSTOM,
25} log_type = MCTP_LOG_NONE;
26
27static int log_stdio_level;
28static void (*log_custom_fn)(int, const char *, va_list);
29
30void mctp_prlog(int level, const char *fmt, ...)
31{
32 va_list ap;
33
34 va_start(ap, fmt);
35
36 switch (log_type) {
37 case MCTP_LOG_NONE:
38 break;
39 case MCTP_LOG_STDIO:
Andrew Jeffery682232e2020-01-13 16:17:45 +103040#ifdef MCTP_HAVE_STDIO
Jeremy Kerrc7e764a2019-05-28 16:49:03 +080041 if (level <= log_stdio_level) {
42 vfprintf(stderr, fmt, ap);
43 fputs("\n", stderr);
44 }
Andrew Jeffery682232e2020-01-13 16:17:45 +103045#endif
Jeremy Kerrc7e764a2019-05-28 16:49:03 +080046 break;
47 case MCTP_LOG_SYSLOG:
48#ifdef MCTP_HAVE_SYSLOG
49 vsyslog(level, fmt, ap);
50#endif
51 break;
52 case MCTP_LOG_CUSTOM:
53 log_custom_fn(level, fmt, ap);
54 break;
55 }
56
57 va_end(ap);
58}
59
60void mctp_set_log_stdio(int level)
61{
62 log_type = MCTP_LOG_STDIO;
63 log_stdio_level = level;
64}
65
66void mctp_set_log_syslog(void)
67{
68 log_type = MCTP_LOG_SYSLOG;
69}
70
71void mctp_set_log_custom(void (*fn)(int, const char *, va_list))
72{
73 log_type = MCTP_LOG_CUSTOM;
74 log_custom_fn = fn;
75}