Browse Source

add logger

1.0.2
Georg Hopp 12 years ago
parent
commit
b7d95b1abe
  1. 5
      include/Makefile.am
  2. 43
      include/tr/interface/logger.h
  3. 59
      include/tr/logger.h
  4. 6
      src/Makefile.am
  5. 62
      src/i_logger.c
  6. 57
      src/logger.c
  7. 39
      src/stderr.c
  8. 52
      src/syslog.c

5
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

43
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 <http://www.gnu.org/licenses/>.
*/
#ifndef __TR_INTERFACE_LOGGER_H__
#define __TR_INTERFACE_LOGGER_H__
#include <stdarg.h>
#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:

59
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 <http://www.gnu.org/licenses/>.
*/
#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:

6
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

62
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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#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:

57
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 <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#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:

39
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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#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:

52
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 <http://www.gnu.org/licenses/>.
*/
#include <syslog.h>
#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:
Loading…
Cancel
Save