Annotation of embedaddon/strongswan/src/libcharon/bus/listeners/sys_logger.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2012 Tobias Brunner
        !             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:        /**
        !            54:         * Mutex to ensure multi-line log messages are not torn apart
        !            55:         */
        !            56:        mutex_t *mutex;
        !            57: 
        !            58:        /**
        !            59:         * Lock to read/write options (levels, ike_name)
        !            60:         */
        !            61:        rwlock_t *lock;
        !            62: };
        !            63: 
        !            64: METHOD(logger_t, log_, void,
        !            65:        private_sys_logger_t *this, debug_t group, level_t level, int thread,
        !            66:        ike_sa_t* ike_sa, const char *message)
        !            67: {
        !            68:        char groupstr[4], namestr[128] = "";
        !            69:        const char *current = message, *next;
        !            70: 
        !            71:        /* cache group name and optional name string */
        !            72:        snprintf(groupstr, sizeof(groupstr), "%N", debug_names, group);
        !            73: 
        !            74:        this->lock->read_lock(this->lock);
        !            75:        if (this->ike_name && ike_sa)
        !            76:        {
        !            77:                if (ike_sa->get_peer_cfg(ike_sa))
        !            78:                {
        !            79:                        snprintf(namestr, sizeof(namestr), " <%s|%d>",
        !            80:                                ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
        !            81:                }
        !            82:                else
        !            83:                {
        !            84:                        snprintf(namestr, sizeof(namestr), " <%d>",
        !            85:                                ike_sa->get_unique_id(ike_sa));
        !            86:                }
        !            87:        }
        !            88:        this->lock->unlock(this->lock);
        !            89: 
        !            90:        /* do a syslog for every line */
        !            91:        this->mutex->lock(this->mutex);
        !            92:        while (TRUE)
        !            93:        {
        !            94:                next = strchr(current, '\n');
        !            95:                if (next == NULL)
        !            96:                {
        !            97:                        syslog(this->facility | LOG_INFO, "%.2d[%s]%s %s\n",
        !            98:                                   thread, groupstr, namestr, current);
        !            99:                        break;
        !           100:                }
        !           101:                syslog(this->facility | LOG_INFO, "%.2d[%s]%s %.*s\n",
        !           102:                           thread, groupstr, namestr, (int)(next - current), current);
        !           103:                current = next + 1;
        !           104:        }
        !           105:        this->mutex->unlock(this->mutex);
        !           106: }
        !           107: 
        !           108: METHOD(logger_t, get_level, level_t,
        !           109:        private_sys_logger_t *this, debug_t group)
        !           110: {
        !           111:        level_t level;
        !           112: 
        !           113:        this->lock->read_lock(this->lock);
        !           114:        level = this->levels[group];
        !           115:        this->lock->unlock(this->lock);
        !           116:        return level;
        !           117: }
        !           118: 
        !           119: METHOD(sys_logger_t, set_level, void,
        !           120:        private_sys_logger_t *this, debug_t group, level_t level)
        !           121: {
        !           122:        this->lock->write_lock(this->lock);
        !           123:        if (group < DBG_ANY)
        !           124:        {
        !           125:                this->levels[group] = level;
        !           126:        }
        !           127:        else
        !           128:        {
        !           129:                for (group = 0; group < DBG_MAX; group++)
        !           130:                {
        !           131:                        this->levels[group] = level;
        !           132:                }
        !           133:        }
        !           134:        this->lock->unlock(this->lock);
        !           135: }
        !           136: 
        !           137: METHOD(sys_logger_t, set_options, void,
        !           138:        private_sys_logger_t *this, bool ike_name)
        !           139: {
        !           140:        this->lock->write_lock(this->lock);
        !           141:        this->ike_name = ike_name;
        !           142:        this->lock->unlock(this->lock);
        !           143: }
        !           144: 
        !           145: METHOD(sys_logger_t, destroy, void,
        !           146:        private_sys_logger_t *this)
        !           147: {
        !           148:        this->lock->destroy(this->lock);
        !           149:        this->mutex->destroy(this->mutex);
        !           150:        free(this);
        !           151: }
        !           152: 
        !           153: /*
        !           154:  * Described in header.
        !           155:  */
        !           156: sys_logger_t *sys_logger_create(int facility)
        !           157: {
        !           158:        private_sys_logger_t *this;
        !           159: 
        !           160:        INIT(this,
        !           161:                .public = {
        !           162:                        .logger = {
        !           163:                                .log = _log_,
        !           164:                                .get_level = _get_level,
        !           165:                        },
        !           166:                        .set_level = _set_level,
        !           167:                        .set_options = _set_options,
        !           168:                        .destroy = _destroy,
        !           169:                },
        !           170:                .facility = facility,
        !           171:                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
        !           172:                .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
        !           173:        );
        !           174: 
        !           175:        set_level(this, DBG_ANY, LEVEL_SILENT);
        !           176:        setlogmask(LOG_UPTO(LOG_INFO));
        !           177: 
        !           178:        return &this->public;
        !           179: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>