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