Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 1 | /* |
| 2 | * kmod - log infrastructure |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 20 | #include <errno.h> |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <syslog.h> |
| 24 | |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame^] | 25 | #include <libkmod.h> |
| 26 | |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 27 | #include "kmod.h" |
| 28 | |
| 29 | static bool log_use_syslog; |
Lucas De Marchi | 52a50fe | 2012-11-06 18:26:34 -0200 | [diff] [blame] | 30 | static int log_priority = LOG_ERR; |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 31 | |
Lucas De Marchi | c841273 | 2012-11-06 19:06:11 -0200 | [diff] [blame] | 32 | static _always_inline_ const char *prio_to_str(int prio) |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 33 | { |
Lucas De Marchi | c841273 | 2012-11-06 19:06:11 -0200 | [diff] [blame] | 34 | const char *prioname; |
| 35 | char buf[32]; |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 36 | |
Lucas De Marchi | c841273 | 2012-11-06 19:06:11 -0200 | [diff] [blame] | 37 | switch (prio) { |
| 38 | case LOG_CRIT: |
| 39 | prioname = "FATAL"; |
| 40 | break; |
| 41 | case LOG_ERR: |
| 42 | prioname = "ERROR"; |
| 43 | break; |
| 44 | case LOG_WARNING: |
| 45 | prioname = "WARNING"; |
| 46 | break; |
| 47 | case LOG_NOTICE: |
| 48 | prioname = "NOTICE"; |
| 49 | break; |
| 50 | case LOG_INFO: |
| 51 | prioname = "INFO"; |
| 52 | break; |
| 53 | case LOG_DEBUG: |
| 54 | prioname = "DEBUG"; |
| 55 | break; |
| 56 | default: |
| 57 | snprintf(buf, sizeof(buf), "LOG-%03d", prio); |
| 58 | prioname = buf; |
| 59 | } |
| 60 | |
| 61 | return prioname; |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 62 | } |
| 63 | |
Lucas De Marchi | 1958af8 | 2013-04-21 16:16:18 -0300 | [diff] [blame] | 64 | _printf_format_(6, 0) |
Lucas De Marchi | c841273 | 2012-11-06 19:06:11 -0200 | [diff] [blame] | 65 | static void log_kmod(void *data, int priority, const char *file, int line, |
| 66 | const char *fn, const char *format, va_list args) |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 67 | { |
| 68 | const char *prioname = prio_to_str(priority); |
| 69 | char *str; |
| 70 | |
| 71 | if (vasprintf(&str, format, args) < 0) |
| 72 | return; |
| 73 | |
| 74 | if (log_use_syslog) { |
| 75 | #ifdef ENABLE_DEBUG |
| 76 | syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, |
| 77 | fn, str); |
| 78 | #else |
| 79 | syslog(priority, "%s: %s", prioname, str); |
| 80 | #endif |
| 81 | } else { |
| 82 | #ifdef ENABLE_DEBUG |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 83 | fprintf(stderr, "%s: %s: %s:%d %s() %s", |
| 84 | program_invocation_short_name, prioname, file, line, |
| 85 | fn, str); |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 86 | #else |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 87 | fprintf(stderr, "%s: %s: %s", program_invocation_short_name, |
| 88 | prioname, str); |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 89 | #endif |
| 90 | } |
| 91 | |
| 92 | free(str); |
| 93 | (void)data; |
| 94 | } |
Lucas De Marchi | c841273 | 2012-11-06 19:06:11 -0200 | [diff] [blame] | 95 | void log_open(bool use_syslog) |
| 96 | { |
| 97 | log_use_syslog = use_syslog; |
| 98 | |
| 99 | if (log_use_syslog) |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 100 | openlog(program_invocation_short_name, LOG_CONS, LOG_DAEMON); |
Lucas De Marchi | c841273 | 2012-11-06 19:06:11 -0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void log_close(void) |
| 104 | { |
| 105 | if (log_use_syslog) |
| 106 | closelog(); |
| 107 | } |
Lucas De Marchi | 52a50fe | 2012-11-06 18:26:34 -0200 | [diff] [blame] | 108 | |
Lucas De Marchi | fcb0ce9 | 2012-11-06 19:01:59 -0200 | [diff] [blame] | 109 | void log_printf(int prio, const char *fmt, ...) |
| 110 | { |
| 111 | const char *prioname; |
| 112 | char *msg; |
| 113 | va_list args; |
| 114 | |
| 115 | if (prio > log_priority) |
| 116 | return; |
| 117 | |
| 118 | va_start(args, fmt); |
| 119 | if (vasprintf(&msg, fmt, args) < 0) |
| 120 | msg = NULL; |
| 121 | va_end(args); |
| 122 | if (msg == NULL) |
| 123 | return; |
| 124 | |
| 125 | prioname = prio_to_str(prio); |
| 126 | |
| 127 | if (log_use_syslog) |
| 128 | syslog(prio, "%s: %s", prioname, msg); |
| 129 | else |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 130 | fprintf(stderr, "%s: %s: %s", program_invocation_short_name, |
| 131 | prioname, msg); |
Lucas De Marchi | fcb0ce9 | 2012-11-06 19:01:59 -0200 | [diff] [blame] | 132 | free(msg); |
| 133 | |
| 134 | if (prio <= LOG_CRIT) |
| 135 | exit(EXIT_FAILURE); |
| 136 | } |
| 137 | |
Lucas De Marchi | 52a50fe | 2012-11-06 18:26:34 -0200 | [diff] [blame] | 138 | void log_setup_kmod_log(struct kmod_ctx *ctx, int priority) |
| 139 | { |
| 140 | log_priority = priority; |
| 141 | |
| 142 | kmod_set_log_priority(ctx, log_priority); |
| 143 | kmod_set_log_fn(ctx, log_kmod, NULL); |
| 144 | } |