Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame^] | 1 | /* |
| 2 | * kmod - log infrastructure |
| 3 | * |
| 4 | * Copyright (C) 2012 ProFUSION embedded systems |
| 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 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <syslog.h> |
| 23 | |
| 24 | #include "kmod.h" |
| 25 | |
| 26 | static bool log_use_syslog; |
| 27 | |
| 28 | void log_open(bool use_syslog) |
| 29 | { |
| 30 | log_use_syslog = use_syslog; |
| 31 | |
| 32 | if (log_use_syslog) |
| 33 | openlog(binname, LOG_CONS, LOG_DAEMON); |
| 34 | } |
| 35 | |
| 36 | void log_close(void) |
| 37 | { |
| 38 | if (log_use_syslog) |
| 39 | closelog(); |
| 40 | } |
| 41 | |
| 42 | void log_kmod(void *data, int priority, const char *file, int line, |
| 43 | const char *fn, const char *format, va_list args) |
| 44 | { |
| 45 | const char *prioname = prio_to_str(priority); |
| 46 | char *str; |
| 47 | |
| 48 | if (vasprintf(&str, format, args) < 0) |
| 49 | return; |
| 50 | |
| 51 | if (log_use_syslog) { |
| 52 | #ifdef ENABLE_DEBUG |
| 53 | syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, |
| 54 | fn, str); |
| 55 | #else |
| 56 | syslog(priority, "%s: %s", prioname, str); |
| 57 | #endif |
| 58 | } else { |
| 59 | #ifdef ENABLE_DEBUG |
| 60 | fprintf(stderr, "%s: %s: %s:%d %s() %s", binname, prioname, |
| 61 | file, line, fn, str); |
| 62 | #else |
| 63 | fprintf(stderr, "%s: %s: %s", binname, prioname, str); |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | free(str); |
| 68 | (void)data; |
| 69 | } |