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> |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 4 | |
| 5 | #include "libmctp.h" |
| 6 | #include "libmctp-log.h" |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
Andrew Jeffery | 682232e | 2020-01-13 16:17:45 +1030 | [diff] [blame] | 12 | #ifdef MCTP_HAVE_STDIO |
| 13 | #include <stdio.h> |
| 14 | #endif |
| 15 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 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 | |
| 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: |
Andrew Jeffery | 682232e | 2020-01-13 16:17:45 +1030 | [diff] [blame] | 40 | #ifdef MCTP_HAVE_STDIO |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 41 | if (level <= log_stdio_level) { |
| 42 | vfprintf(stderr, fmt, ap); |
| 43 | fputs("\n", stderr); |
| 44 | } |
Andrew Jeffery | 682232e | 2020-01-13 16:17:45 +1030 | [diff] [blame] | 45 | #endif |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 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 | |
| 60 | void mctp_set_log_stdio(int level) |
| 61 | { |
| 62 | log_type = MCTP_LOG_STDIO; |
| 63 | log_stdio_level = level; |
| 64 | } |
| 65 | |
| 66 | void mctp_set_log_syslog(void) |
| 67 | { |
| 68 | log_type = MCTP_LOG_SYSLOG; |
| 69 | } |
| 70 | |
| 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 | } |