8 changed files with 320 additions and 3 deletions
-
5include/Makefile.am
-
43include/tr/interface/logger.h
-
59include/tr/logger.h
-
6src/Makefile.am
-
62src/i_logger.c
-
57src/logger.c
-
39src/stderr.c
-
52src/syslog.c
@ -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: |
||||
@ -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: |
||||
@ -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: |
||||
@ -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: |
||||
@ -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: |
||||
@ -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: |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue