Commit b7d95b1abe3c317508ed7465d658b53ef3ce6b87

Authored by Georg Hopp
1 parent e32f54b2

add logger

@@ -3,10 +3,11 @@ nobase_include_HEADERS = trbase.h \ @@ -3,10 +3,11 @@ nobase_include_HEADERS = trbase.h \
3 tr/commons.h \ 3 tr/commons.h \
4 tr/interface.h \ 4 tr/interface.h \
5 tr/memory.h \ 5 tr/memory.h \
  6 + tr/logger.h \
6 tr/tree_macros.h \ 7 tr/tree_macros.h \
7 tr/interface/class.h \ 8 tr/interface/class.h \
8 tr/interface/indexable.h \ 9 tr/interface/indexable.h \
9 tr/interface/observer.h \ 10 tr/interface/observer.h \
10 tr/interface/serializable.h \ 11 tr/interface/serializable.h \
11 - tr/interface/subject.h  
12 - 12 + tr/interface/subject.h \
  13 + tr/interface/logger.h
  1 +/**
  2 + * \file
  3 + * The logger interface.
  4 + *
  5 + * \author Georg Hopp
  6 + *
  7 + * \copyright
  8 + * Copyright © 2012 Georg Hopp
  9 + *
  10 + * This program is free software: you can redistribute it and/or modify
  11 + * it under the terms of the GNU General Public License as published by
  12 + * the Free Software Foundation, either version 3 of the License, or
  13 + * (at your option) any later version.
  14 + *
  15 + * This program is distributed in the hope that it will be useful,
  16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18 + * GNU General Public License for more details.
  19 + *
  20 + * You should have received a copy of the GNU General Public License
  21 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22 + */
  23 +
  24 +#ifndef __TR_INTERFACE_LOGGER_H__
  25 +#define __TR_INTERFACE_LOGGER_H__
  26 +
  27 +#include <stdarg.h>
  28 +
  29 +#include "trbase.h"
  30 +#include "tr/logger.h"
  31 +
  32 +typedef void (* fptr_TR_log)(void *, TR_logger_level, const char * const);
  33 +
  34 +TR_INTERFACE(TR_Logger) {
  35 + TR_IFID;
  36 + fptr_TR_log log;
  37 +};
  38 +
  39 +extern void TR_loggerLog(void *, TR_logger_level, const char * const, ...);
  40 +
  41 +#endif // __TR_INTERFACE_LOGGER_H__
  42 +
  43 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + * A generic logger class and two extended classes, One that logs to
  4 + * stderr and one that logs to the system syslog.
  5 + *
  6 + * \author Georg Hopp
  7 + *
  8 + * \copyright
  9 + * Copyright © 2012 Georg Hopp
  10 + *
  11 + * This program is free software: you can redistribute it and/or modify
  12 + * it under the terms of the GNU General Public License as published by
  13 + * the Free Software Foundation, either version 3 of the License, or
  14 + * (at your option) any later version.
  15 + *
  16 + * This program is distributed in the hope that it will be useful,
  17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 + * GNU General Public License for more details.
  20 + *
  21 + * You should have received a copy of the GNU General Public License
  22 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23 + */
  24 +
  25 +#ifndef __TR_LOGGER_H__
  26 +#define __TR_LOGGER_H__
  27 +
  28 +#include "trbase.h"
  29 +
  30 +typedef enum {
  31 + TR_LOGGER_DEBUG=0,
  32 + TR_LOGGER_INFO,
  33 + TR_LOGGER_NOTICE,
  34 + TR_LOGGER_WARNING,
  35 + TR_LOGGER_ERR,
  36 + TR_LOGGER_CRIT,
  37 + TR_LOGGER_ALERT,
  38 + TR_LOGGER_EMERG
  39 +} TR_logger_level;
  40 +
  41 +#include "tr/interface/logger.h"
  42 +
  43 +extern const char * const TR_logger_level_str[];
  44 +
  45 +TR_CLASS(TR_Logger) {
  46 + TR_logger_level min_level;
  47 +};
  48 +
  49 +TR_CLASS(TR_LoggerStderr) {
  50 + TR_EXTENDS(TR_Logger);
  51 +};
  52 +
  53 +TR_CLASS(TR_LoggerSyslog) {
  54 + TR_EXTENDS(TR_Logger);
  55 +};
  56 +
  57 +#endif // __TR_LOGGER_H__
  58 +
  59 +// vim: set ts=4 sw=4:
@@ -5,11 +5,15 @@ AM_CFLAGS += -I../include/ @@ -5,11 +5,15 @@ AM_CFLAGS += -I../include/
5 5
6 TR_CLASS = memory.c \ 6 TR_CLASS = memory.c \
7 interface.c \ 7 interface.c \
  8 + logger.c \
  9 + stderr.c \
  10 + syslog.c \
8 i_class.c \ 11 i_class.c \
9 i_subject.c \ 12 i_subject.c \
10 i_observer.c \ 13 i_observer.c \
11 i_indexable.c \ 14 i_indexable.c \
12 - i_serializable.c 15 + i_serializable.c \
  16 + i_logger.c
13 17
14 lib_LTLIBRARIES = libtrbase.la 18 lib_LTLIBRARIES = libtrbase.la
15 19
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdlib.h>
  24 +#include <stdio.h>
  25 +#include <stdarg.h>
  26 +
  27 +#include "tr/logger.h"
  28 +#include "tr/interface/logger.h"
  29 +#include "trbase.h"
  30 +
  31 +TR_CREATE_INTERFACE(TR_Logger, 1);
  32 +
  33 +void
  34 +TR_loggerLog(
  35 + void * _object,
  36 + TR_logger_level level,
  37 + const char * const fmt,
  38 + ...) {
  39 + TR_Logger object = _object;
  40 +
  41 + if (level >= object->min_level) {
  42 + char * msg = NULL;
  43 + size_t msg_size = 0;
  44 + va_list params;
  45 +
  46 + va_start(params, fmt);
  47 + msg_size = vsnprintf(NULL, msg_size, fmt, params);
  48 + va_end(params);
  49 +
  50 + msg = TR_malloc(msg_size + 1);
  51 +
  52 + va_start(params, fmt);
  53 + vsnprintf(msg, msg_size + 1, fmt, params);
  54 + va_end(params);
  55 +
  56 + TR_CALL(_object, TR_Logger, log, level, msg);
  57 +
  58 + TR_MEM_FREE(msg);
  59 + }
  60 +}
  61 +
  62 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdarg.h>
  24 +
  25 +#include "trbase.h"
  26 +#include "tr/logger.h"
  27 +#include "tr/interface/logger.h"
  28 +
  29 +const
  30 +char * const
  31 +TR_logger_level_str[] = {
  32 + "DEBUG",
  33 + "INFO",
  34 + "NOTICE",
  35 + "WARNING",
  36 + "ERR",
  37 + "CRIT",
  38 + "ALERT",
  39 + "EMERG"
  40 +};
  41 +
  42 +static
  43 +int
  44 +loggerCtor(void * _this, va_list * params)
  45 +{
  46 + TR_Logger this = _this;
  47 + this->min_level = va_arg(*params, int);
  48 +
  49 + return 0;
  50 +}
  51 +
  52 +static void loggerDtor(void * _this) {}
  53 +
  54 +TR_INIT_IFACE(TR_Class, loggerCtor, loggerDtor, NULL);
  55 +TR_CREATE_CLASS(TR_Logger, NULL, TR_IF(TR_Class));
  56 +
  57 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdio.h>
  24 +
  25 +#include "trbase.h"
  26 +#include "tr/logger.h"
  27 +#include "tr/interface/logger.h"
  28 +
  29 +static
  30 +void
  31 +logStderr(void * this, TR_logger_level level, const char * const msg)
  32 +{
  33 + fprintf(stderr, "[%s] %s\n", TR_logger_level_str[level], msg);
  34 +}
  35 +
  36 +TR_INIT_IFACE(TR_Logger, logStderr);
  37 +TR_CREATE_CLASS(TR_LoggerStderr, TR_Logger, TR_IF(TR_Logger));
  38 +
  39 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <syslog.h>
  24 +
  25 +#include "trbase.h"
  26 +#include "tr/logger.h"
  27 +#include "tr/interface/logger.h"
  28 +
  29 +static
  30 +const
  31 +int syslog_priority[] = {
  32 + LOG_USER | LOG_DEBUG,
  33 + LOG_USER | LOG_INFO,
  34 + LOG_USER | LOG_NOTICE,
  35 + LOG_USER | LOG_WARNING,
  36 + LOG_USER | LOG_ERR,
  37 + LOG_USER | LOG_CRIT,
  38 + LOG_USER | LOG_ALERT,
  39 + LOG_USER | LOG_EMERG
  40 +};
  41 +
  42 +static
  43 +void
  44 +logSyslog(void * this, TR_logger_level level, const char * const msg)
  45 +{
  46 + syslog(syslog_priority[level], "[%s] %s", TR_logger_level_str[level], msg);
  47 +}
  48 +
  49 +TR_INIT_IFACE(TR_Logger, logSyslog);
  50 +TR_CREATE_CLASS(TR_LoggerSyslog, TR_Logger, TR_IF(TR_Logger));
  51 +
  52 +// vim: set ts=4 sw=4:
Please register or login to post a comment