Return to sys_logger.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libcharon / bus / listeners |
1.1 misho 1: /* 1.1.1.2 ! misho 2: * Copyright (C) 2012-2020 Tobias Brunner 1.1 misho 3: * Copyright (C) 2006 Martin Willi 4: * HSR Hochschule fuer Technik Rapperswil 5: * 6: * This program is free software; you can redistribute it and/or modify it 7: * under the terms of the GNU General Public License as published by the 8: * Free Software Foundation; either version 2 of the License, or (at your 9: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. 10: * 11: * This program is distributed in the hope that it will be useful, but 12: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14: * for more details. 15: */ 16: 17: #include <stdio.h> 18: #include <string.h> 19: #include <syslog.h> 20: 21: #include "sys_logger.h" 22: 23: #include <threading/mutex.h> 24: #include <threading/rwlock.h> 25: 26: typedef struct private_sys_logger_t private_sys_logger_t; 27: 28: /** 29: * Private data of a sys_logger_t object 30: */ 31: struct private_sys_logger_t { 32: 33: /** 34: * Public data. 35: */ 36: sys_logger_t public; 37: 38: /** 39: * syslog facility to use 40: */ 41: int facility; 42: 43: /** 44: * Maximum level to log, for each group 45: */ 46: level_t levels[DBG_MAX]; 47: 48: /** 49: * Print the name/# of the IKE_SA? 50: */ 51: bool ike_name; 52: 53: /** 1.1.1.2 ! misho 54: * Print the log level ! 55: */ ! 56: bool log_level; ! 57: ! 58: /** 1.1 misho 59: * Mutex to ensure multi-line log messages are not torn apart 60: */ 61: mutex_t *mutex; 62: 63: /** 64: * Lock to read/write options (levels, ike_name) 65: */ 66: rwlock_t *lock; 67: }; 68: 69: METHOD(logger_t, log_, void, 70: private_sys_logger_t *this, debug_t group, level_t level, int thread, 71: ike_sa_t* ike_sa, const char *message) 72: { 1.1.1.2 ! misho 73: char groupstr[5], namestr[128] = ""; 1.1 misho 74: const char *current = message, *next; 75: 76: /* cache group name and optional name string */ 77: this->lock->read_lock(this->lock); 1.1.1.2 ! misho 78: if (this->log_level) ! 79: { ! 80: snprintf(groupstr, sizeof(groupstr), "%N%d", debug_names, group, ! 81: level); ! 82: } ! 83: else ! 84: { ! 85: snprintf(groupstr, sizeof(groupstr), "%N", debug_names, group); ! 86: } ! 87: 1.1 misho 88: if (this->ike_name && ike_sa) 89: { 90: if (ike_sa->get_peer_cfg(ike_sa)) 91: { 92: snprintf(namestr, sizeof(namestr), " <%s|%d>", 93: ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa)); 94: } 95: else 96: { 97: snprintf(namestr, sizeof(namestr), " <%d>", 98: ike_sa->get_unique_id(ike_sa)); 99: } 100: } 101: this->lock->unlock(this->lock); 102: 103: /* do a syslog for every line */ 104: this->mutex->lock(this->mutex); 105: while (TRUE) 106: { 107: next = strchr(current, '\n'); 108: if (next == NULL) 109: { 110: syslog(this->facility | LOG_INFO, "%.2d[%s]%s %s\n", 111: thread, groupstr, namestr, current); 112: break; 113: } 114: syslog(this->facility | LOG_INFO, "%.2d[%s]%s %.*s\n", 115: thread, groupstr, namestr, (int)(next - current), current); 116: current = next + 1; 117: } 118: this->mutex->unlock(this->mutex); 119: } 120: 121: METHOD(logger_t, get_level, level_t, 122: private_sys_logger_t *this, debug_t group) 123: { 124: level_t level; 125: 126: this->lock->read_lock(this->lock); 127: level = this->levels[group]; 128: this->lock->unlock(this->lock); 129: return level; 130: } 131: 132: METHOD(sys_logger_t, set_level, void, 133: private_sys_logger_t *this, debug_t group, level_t level) 134: { 135: this->lock->write_lock(this->lock); 136: if (group < DBG_ANY) 137: { 138: this->levels[group] = level; 139: } 140: else 141: { 142: for (group = 0; group < DBG_MAX; group++) 143: { 144: this->levels[group] = level; 145: } 146: } 147: this->lock->unlock(this->lock); 148: } 149: 150: METHOD(sys_logger_t, set_options, void, 1.1.1.2 ! misho 151: private_sys_logger_t *this, bool ike_name, bool log_level) 1.1 misho 152: { 153: this->lock->write_lock(this->lock); 154: this->ike_name = ike_name; 1.1.1.2 ! misho 155: this->log_level = log_level; 1.1 misho 156: this->lock->unlock(this->lock); 157: } 158: 159: METHOD(sys_logger_t, destroy, void, 160: private_sys_logger_t *this) 161: { 162: this->lock->destroy(this->lock); 163: this->mutex->destroy(this->mutex); 164: free(this); 165: } 166: 167: /* 168: * Described in header. 169: */ 170: sys_logger_t *sys_logger_create(int facility) 171: { 172: private_sys_logger_t *this; 173: 174: INIT(this, 175: .public = { 176: .logger = { 177: .log = _log_, 178: .get_level = _get_level, 179: }, 180: .set_level = _set_level, 181: .set_options = _set_options, 182: .destroy = _destroy, 183: }, 184: .facility = facility, 185: .mutex = mutex_create(MUTEX_TYPE_DEFAULT), 186: .lock = rwlock_create(RWLOCK_TYPE_DEFAULT), 187: ); 188: 189: set_level(this, DBG_ANY, LEVEL_SILENT); 190: setlogmask(LOG_UPTO(LOG_INFO)); 191: 192: return &this->public; 193: }