From b7d95b1abe3c317508ed7465d658b53ef3ce6b87 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sat, 5 Jul 2014 22:07:40 +0100 Subject: [PATCH] add logger --- include/Makefile.am | 5 +-- include/tr/interface/logger.h | 43 ++++++++++++++++++++++++ include/tr/logger.h | 59 +++++++++++++++++++++++++++++++++ src/Makefile.am | 6 +++- src/i_logger.c | 62 +++++++++++++++++++++++++++++++++++ src/logger.c | 57 ++++++++++++++++++++++++++++++++ src/stderr.c | 39 ++++++++++++++++++++++ src/syslog.c | 52 +++++++++++++++++++++++++++++ 8 files changed, 320 insertions(+), 3 deletions(-) create mode 100644 include/tr/interface/logger.h create mode 100644 include/tr/logger.h create mode 100644 src/i_logger.c create mode 100644 src/logger.c create mode 100644 src/stderr.c create mode 100644 src/syslog.c diff --git a/include/Makefile.am b/include/Makefile.am index e7b410d..54d71c1 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -3,10 +3,11 @@ nobase_include_HEADERS = trbase.h \ tr/commons.h \ tr/interface.h \ tr/memory.h \ + tr/logger.h \ tr/tree_macros.h \ tr/interface/class.h \ tr/interface/indexable.h \ tr/interface/observer.h \ tr/interface/serializable.h \ - tr/interface/subject.h - + tr/interface/subject.h \ + tr/interface/logger.h diff --git a/include/tr/interface/logger.h b/include/tr/interface/logger.h new file mode 100644 index 0000000..950cb28 --- /dev/null +++ b/include/tr/interface/logger.h @@ -0,0 +1,43 @@ +/** + * \file + * The logger interface. + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __TR_INTERFACE_LOGGER_H__ +#define __TR_INTERFACE_LOGGER_H__ + +#include + +#include "trbase.h" +#include "tr/logger.h" + +typedef void (* fptr_TR_log)(void *, TR_logger_level, const char * const); + +TR_INTERFACE(TR_Logger) { + TR_IFID; + fptr_TR_log log; +}; + +extern void TR_loggerLog(void *, TR_logger_level, const char * const, ...); + +#endif // __TR_INTERFACE_LOGGER_H__ + +// vim: set ts=4 sw=4: diff --git a/include/tr/logger.h b/include/tr/logger.h new file mode 100644 index 0000000..a21cf56 --- /dev/null +++ b/include/tr/logger.h @@ -0,0 +1,59 @@ +/** + * \file + * A generic logger class and two extended classes, One that logs to + * stderr and one that logs to the system syslog. + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __TR_LOGGER_H__ +#define __TR_LOGGER_H__ + +#include "trbase.h" + +typedef enum { + TR_LOGGER_DEBUG=0, + TR_LOGGER_INFO, + TR_LOGGER_NOTICE, + TR_LOGGER_WARNING, + TR_LOGGER_ERR, + TR_LOGGER_CRIT, + TR_LOGGER_ALERT, + TR_LOGGER_EMERG +} TR_logger_level; + +#include "tr/interface/logger.h" + +extern const char * const TR_logger_level_str[]; + +TR_CLASS(TR_Logger) { + TR_logger_level min_level; +}; + +TR_CLASS(TR_LoggerStderr) { + TR_EXTENDS(TR_Logger); +}; + +TR_CLASS(TR_LoggerSyslog) { + TR_EXTENDS(TR_Logger); +}; + +#endif // __TR_LOGGER_H__ + +// vim: set ts=4 sw=4: diff --git a/src/Makefile.am b/src/Makefile.am index bf2823d..2cf64ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,11 +5,15 @@ AM_CFLAGS += -I../include/ TR_CLASS = memory.c \ interface.c \ + logger.c \ + stderr.c \ + syslog.c \ i_class.c \ i_subject.c \ i_observer.c \ i_indexable.c \ - i_serializable.c + i_serializable.c \ + i_logger.c lib_LTLIBRARIES = libtrbase.la diff --git a/src/i_logger.c b/src/i_logger.c new file mode 100644 index 0000000..31046b0 --- /dev/null +++ b/src/i_logger.c @@ -0,0 +1,62 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include "tr/logger.h" +#include "tr/interface/logger.h" +#include "trbase.h" + +TR_CREATE_INTERFACE(TR_Logger, 1); + +void +TR_loggerLog( + void * _object, + TR_logger_level level, + const char * const fmt, + ...) { + TR_Logger object = _object; + + if (level >= object->min_level) { + char * msg = NULL; + size_t msg_size = 0; + va_list params; + + va_start(params, fmt); + msg_size = vsnprintf(NULL, msg_size, fmt, params); + va_end(params); + + msg = TR_malloc(msg_size + 1); + + va_start(params, fmt); + vsnprintf(msg, msg_size + 1, fmt, params); + va_end(params); + + TR_CALL(_object, TR_Logger, log, level, msg); + + TR_MEM_FREE(msg); + } +} + +// vim: set ts=4 sw=4: diff --git a/src/logger.c b/src/logger.c new file mode 100644 index 0000000..eb24930 --- /dev/null +++ b/src/logger.c @@ -0,0 +1,57 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "tr/logger.h" +#include "tr/interface/logger.h" + +const +char * const +TR_logger_level_str[] = { + "DEBUG", + "INFO", + "NOTICE", + "WARNING", + "ERR", + "CRIT", + "ALERT", + "EMERG" +}; + +static +int +loggerCtor(void * _this, va_list * params) +{ + TR_Logger this = _this; + this->min_level = va_arg(*params, int); + + return 0; +} + +static void loggerDtor(void * _this) {} + +TR_INIT_IFACE(TR_Class, loggerCtor, loggerDtor, NULL); +TR_CREATE_CLASS(TR_Logger, NULL, TR_IF(TR_Class)); + +// vim: set ts=4 sw=4: diff --git a/src/stderr.c b/src/stderr.c new file mode 100644 index 0000000..a0e7a05 --- /dev/null +++ b/src/stderr.c @@ -0,0 +1,39 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "tr/logger.h" +#include "tr/interface/logger.h" + +static +void +logStderr(void * this, TR_logger_level level, const char * const msg) +{ + fprintf(stderr, "[%s] %s\n", TR_logger_level_str[level], msg); +} + +TR_INIT_IFACE(TR_Logger, logStderr); +TR_CREATE_CLASS(TR_LoggerStderr, TR_Logger, TR_IF(TR_Logger)); + +// vim: set ts=4 sw=4: diff --git a/src/syslog.c b/src/syslog.c new file mode 100644 index 0000000..8e226e7 --- /dev/null +++ b/src/syslog.c @@ -0,0 +1,52 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2012 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "trbase.h" +#include "tr/logger.h" +#include "tr/interface/logger.h" + +static +const +int syslog_priority[] = { + LOG_USER | LOG_DEBUG, + LOG_USER | LOG_INFO, + LOG_USER | LOG_NOTICE, + LOG_USER | LOG_WARNING, + LOG_USER | LOG_ERR, + LOG_USER | LOG_CRIT, + LOG_USER | LOG_ALERT, + LOG_USER | LOG_EMERG +}; + +static +void +logSyslog(void * this, TR_logger_level level, const char * const msg) +{ + syslog(syslog_priority[level], "[%s] %s", TR_logger_level_str[level], msg); +} + +TR_INIT_IFACE(TR_Logger, logSyslog); +TR_CREATE_CLASS(TR_LoggerSyslog, TR_Logger, TR_IF(TR_Logger)); + +// vim: set ts=4 sw=4: