Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 1 | /* Syslog logging support code */ |
| 2 | |
| 3 | /* (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 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 along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "../config.h" |
| 23 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 24 | #ifdef HAVE_SYSLOG_H |
| 25 | |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 26 | #include <stdarg.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <syslog.h> |
| 31 | |
| 32 | #ifdef HAVE_STRINGS_H |
| 33 | #include <strings.h> |
| 34 | #endif |
| 35 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 36 | #include <osmocom/core/talloc.h> |
| 37 | #include <osmocom/core/utils.h> |
| 38 | #include <osmocom/core/logging.h> |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 39 | |
| 40 | static const int logp2syslog_level(unsigned int level) |
| 41 | { |
| 42 | if (level >= LOGL_FATAL) |
| 43 | return LOG_CRIT; |
| 44 | else if (level >= LOGL_ERROR) |
| 45 | return LOG_ERR; |
| 46 | else if (level >= LOGL_NOTICE) |
| 47 | return LOG_NOTICE; |
| 48 | else if (level >= LOGL_INFO) |
| 49 | return LOG_INFO; |
| 50 | else |
| 51 | return LOG_DEBUG; |
| 52 | } |
| 53 | |
| 54 | static void _syslog_output(struct log_target *target, |
| 55 | unsigned int level, const char *log) |
| 56 | { |
| 57 | syslog(logp2syslog_level(level), "%s", log); |
| 58 | } |
| 59 | |
| 60 | struct log_target *log_target_create_syslog(const char *ident, int option, |
| 61 | int facility) |
| 62 | { |
| 63 | struct log_target *target; |
| 64 | |
| 65 | target = log_target_create(); |
| 66 | if (!target) |
| 67 | return NULL; |
| 68 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 69 | target->tgt_syslog.facility = facility; |
| 70 | target->type = LOG_TGT_TYPE_SYSLOG; |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 71 | target->output = _syslog_output; |
| 72 | |
| 73 | openlog(ident, option, facility); |
| 74 | |
| 75 | return target; |
| 76 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 77 | |
| 78 | #endif /* HAVE_SYSLOG_H */ |