Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 2 | |
| 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 | |
| 17 | enum { |
| 18 | MCTP_LOG_NONE, |
| 19 | MCTP_LOG_STDIO, |
| 20 | MCTP_LOG_SYSLOG, |
| 21 | MCTP_LOG_CUSTOM, |
| 22 | } log_type = MCTP_LOG_NONE; |
| 23 | |
| 24 | static int log_stdio_level; |
| 25 | static void (*log_custom_fn)(int, const char *, va_list); |
| 26 | |
| 27 | void 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 | |
| 55 | void mctp_set_log_stdio(int level) |
| 56 | { |
| 57 | log_type = MCTP_LOG_STDIO; |
| 58 | log_stdio_level = level; |
| 59 | } |
| 60 | |
| 61 | void mctp_set_log_syslog(void) |
| 62 | { |
| 63 | log_type = MCTP_LOG_SYSLOG; |
| 64 | } |
| 65 | |
| 66 | void mctp_set_log_custom(void (*fn)(int, const char *, va_list)) |
| 67 | { |
| 68 | log_type = MCTP_LOG_CUSTOM; |
| 69 | log_custom_fn = fn; |
| 70 | } |