xref: /openbmc/libmctp/log.c (revision 682232ea)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 
3 #include <stdarg.h>
4 
5 #include "libmctp.h"
6 #include "libmctp-log.h"
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #ifdef MCTP_HAVE_STDIO
13 #include <stdio.h>
14 #endif
15 
16 #ifdef MCTP_HAVE_SYSLOG
17 #include <syslog.h>
18 #endif
19 
20 enum {
21 	MCTP_LOG_NONE,
22 	MCTP_LOG_STDIO,
23 	MCTP_LOG_SYSLOG,
24 	MCTP_LOG_CUSTOM,
25 } log_type = MCTP_LOG_NONE;
26 
27 static int log_stdio_level;
28 static void (*log_custom_fn)(int, const char *, va_list);
29 
mctp_prlog(int level,const char * fmt,...)30 void 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:
40 #ifdef MCTP_HAVE_STDIO
41 		if (level <= log_stdio_level) {
42 			vfprintf(stderr, fmt, ap);
43 			fputs("\n", stderr);
44 		}
45 #endif
46 		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 
mctp_set_log_stdio(int level)60 void mctp_set_log_stdio(int level)
61 {
62 	log_type = MCTP_LOG_STDIO;
63 	log_stdio_level = level;
64 }
65 
mctp_set_log_syslog(void)66 void mctp_set_log_syslog(void)
67 {
68 	log_type = MCTP_LOG_SYSLOG;
69 }
70 
mctp_set_log_custom(void (* fn)(int,const char *,va_list))71 void mctp_set_log_custom(void (*fn)(int, const char *, va_list))
72 {
73 	log_type = MCTP_LOG_CUSTOM;
74 	log_custom_fn = fn;
75 }
76