- added log_bytes and log_chunk
- fixed logger - uses now syslog only - comments
This commit is contained in:
parent
1061c878a9
commit
69dd95811f
2 changed files with 156 additions and 73 deletions
|
@ -28,8 +28,12 @@
|
|||
|
||||
#include <syslog.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Maximum length of al log entry (only used for logger_s.log)
|
||||
*/
|
||||
#define MAX_LOG 8192
|
||||
|
||||
/**
|
||||
* @brief The logger object
|
||||
|
@ -40,10 +44,6 @@ struct private_logger_s {
|
|||
* Public data
|
||||
*/
|
||||
logger_t public;
|
||||
/**
|
||||
* fd to log, NULL for syslog
|
||||
*/
|
||||
FILE *target;
|
||||
/**
|
||||
* detail-level of logger
|
||||
*/
|
||||
|
@ -57,34 +57,96 @@ struct private_logger_s {
|
|||
|
||||
/**
|
||||
* implements logger_t-function log
|
||||
*
|
||||
* @see logger_s.log
|
||||
*/
|
||||
static status_t logg(private_logger_t *this, logger_level_t loglevel, char *format, ...)
|
||||
{
|
||||
if ((this->level & loglevel) == loglevel)
|
||||
{
|
||||
char buffer[MAX_LOG];
|
||||
snprintf(buffer, MAX_LOG, "%s: %s", this->name, format);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
if (this->target)
|
||||
{
|
||||
fprintf(this->target, format, args);
|
||||
fprintf(this->target, "\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
syslog(LOG_INFO, format, args);
|
||||
}
|
||||
vsyslog(LOG_INFO, buffer, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* implements logger_t-function destroy
|
||||
* @see logger_s.log_bytes
|
||||
*/
|
||||
static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len)
|
||||
{
|
||||
if ((this->level & loglevel) == loglevel)
|
||||
{
|
||||
char buffer[64];
|
||||
char *buffer_pos;
|
||||
char *bytes_pos, *bytes_roof;
|
||||
int i;
|
||||
|
||||
syslog(LOG_INFO, "%s: %s (%d bytes)", this->name, label, len);
|
||||
|
||||
bytes_pos = bytes;
|
||||
bytes_roof = bytes + len;
|
||||
buffer_pos = buffer;
|
||||
|
||||
for (i = 1; bytes_pos < bytes_roof; i++)
|
||||
{
|
||||
static const char hexdig[] = "0123456789ABCDEF";
|
||||
*buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
|
||||
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
|
||||
if ((i % 16) == 0)
|
||||
{
|
||||
*buffer_pos++ = '\0';
|
||||
buffer_pos = buffer;
|
||||
syslog(LOG_INFO, "| %s", buffer);
|
||||
}
|
||||
else if ((i % 8) == 0)
|
||||
{
|
||||
*buffer_pos++ = ' ';
|
||||
*buffer_pos++ = ' ';
|
||||
*buffer_pos++ = ' ';
|
||||
}
|
||||
else if ((i % 4) == 0)
|
||||
{
|
||||
*buffer_pos++ = ' ';
|
||||
*buffer_pos++ = ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
*buffer_pos++ = ' ';
|
||||
}
|
||||
|
||||
bytes_pos++;
|
||||
}
|
||||
|
||||
*buffer_pos++ = '\0';
|
||||
buffer_pos = buffer;
|
||||
syslog(LOG_INFO, "| %s", buffer);
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* implements logger_t-function log_chunk
|
||||
* @see logger_s.log_chunk
|
||||
*/
|
||||
static status_t log_chunk(logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk)
|
||||
{
|
||||
this->log_bytes(this, loglevel, label, chunk->ptr, chunk->len);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* implements logger_t-function enable_level
|
||||
*
|
||||
* @see logger_s.enable_level
|
||||
*/
|
||||
static status_t enable_level(private_logger_t *this, logger_level_t log_level)
|
||||
{
|
||||
|
@ -94,33 +156,28 @@ static status_t enable_level(private_logger_t *this, logger_level_t log_level)
|
|||
|
||||
/**
|
||||
* implements logger_t-function disable_level
|
||||
*
|
||||
* @see logger_s.disable_level
|
||||
*/
|
||||
static status_t disable_level(private_logger_t *this, logger_level_t log_level)
|
||||
{
|
||||
this->level &= (~log_level);
|
||||
this->level &= ~log_level;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* implements logger_t-function destroy
|
||||
*
|
||||
*/
|
||||
* @see logger_s.destroy
|
||||
*/
|
||||
static status_t destroy(private_logger_t *this)
|
||||
{
|
||||
if (this->target)
|
||||
{
|
||||
fclose(this->target);
|
||||
}
|
||||
allocator_free(this);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in Header
|
||||
*
|
||||
*/
|
||||
logger_t *logger_create(char *logger_name, char *file, logger_level_t log_level)
|
||||
logger_t *logger_create(char *logger_name, logger_level_t log_level)
|
||||
{
|
||||
private_logger_t *this = allocator_alloc_thing(private_logger_t);
|
||||
|
||||
|
@ -130,6 +187,8 @@ logger_t *logger_create(char *logger_name, char *file, logger_level_t log_level)
|
|||
}
|
||||
|
||||
this->public.log = (status_t(*)(logger_t*,logger_level_t,char*,...))logg;
|
||||
this->public.log_bytes = (status_t(*)(logger_t*, logger_level_t, char*,char*,size_t))log_bytes;
|
||||
this->public.log_chunk = log_chunk;
|
||||
this->public.enable_level = (status_t(*)(logger_t*,logger_level_t))enable_level;
|
||||
this->public.disable_level = (status_t(*)(logger_t*,logger_level_t))disable_level;
|
||||
this->public.destroy = (status_t(*)(logger_t*))destroy;
|
||||
|
@ -137,21 +196,7 @@ logger_t *logger_create(char *logger_name, char *file, logger_level_t log_level)
|
|||
this->level = log_level;
|
||||
this->name = logger_name;
|
||||
|
||||
/* use system logger ? */
|
||||
if (file)
|
||||
{
|
||||
this->target = fopen(file, "a");
|
||||
if (this->target == NULL)
|
||||
{
|
||||
allocator_free(this);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this->target = NULL;
|
||||
openlog("charon", 0, LOG_DAEMON);
|
||||
}
|
||||
openlog("charon", 0, LOG_DAEMON);
|
||||
|
||||
return (logger_t*)this;
|
||||
}
|
||||
|
|
|
@ -23,13 +23,17 @@
|
|||
#ifndef LOGGER_H_
|
||||
#define LOGGER_H_
|
||||
|
||||
|
||||
#include <freeswan.h>
|
||||
#include <pluto/constants.h>
|
||||
#include <pluto/defs.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* Log Levels supported by the logger object
|
||||
*/
|
||||
typedef enum logger_level_e logger_level_t;
|
||||
|
||||
enum logger_level_e {
|
||||
/**
|
||||
* basic control messages
|
||||
|
@ -53,58 +57,92 @@ enum logger_level_e {
|
|||
* @brief The logger object
|
||||
*/
|
||||
typedef struct logger_s logger_t;
|
||||
struct logger_s {
|
||||
struct logger_s {
|
||||
|
||||
/**
|
||||
* loggs an entry
|
||||
* @brief Log an entry, using printf()-like params.
|
||||
*
|
||||
* function is used like printf
|
||||
* The specefied loglevels must ALL be activated that
|
||||
* the log is done.
|
||||
*
|
||||
* @param this logger_t-object
|
||||
* @param loglevel loglevel of specific log entry
|
||||
* @param format printf like format string
|
||||
* @param ... printf like parameters
|
||||
* @return SUCCESS
|
||||
* @param this logger_t-object
|
||||
* @param loglevel or'ed set of loglevels
|
||||
* @param format printf like format string
|
||||
* @param ... printf like parameters
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*log) (logger_t *this, logger_level_t log_level, char *format, ...);
|
||||
|
||||
/**
|
||||
* @brief Log some bytes, useful for debugging.
|
||||
*
|
||||
* The specefied loglevels must ALL be activated that
|
||||
* the log is done.
|
||||
*
|
||||
* @param this logger_t-object
|
||||
* @param loglevel or'ed set of loglevels
|
||||
* @param label a labeling name, logged with the bytes
|
||||
* @param bytes pointer to the bytes to dump
|
||||
* @param len number of bytes to dump
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*log_bytes) (logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Log a chunk, useful for debugging.
|
||||
*
|
||||
* The specefied loglevels must ALL be activated that
|
||||
* the log is done.
|
||||
*
|
||||
* @param this logger_t-object
|
||||
* @param loglevel or'ed set of loglevels
|
||||
* @param label a labeling name, logged with the bytes
|
||||
* @param chunk pointer to a chunk to log
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*log_chunk) (logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk);
|
||||
|
||||
/**
|
||||
* enables a loglevel for the current logger_t-object
|
||||
* @brief Enables a loglevel for the current logger_t-object.
|
||||
*
|
||||
* @param this logger_t-object
|
||||
* @param log_level loglevel to enable
|
||||
* @return SUCCESS
|
||||
* @param this logger_t-object
|
||||
* @param log_level loglevel to enable
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*enable_level) (logger_t *this, logger_level_t log_level);
|
||||
|
||||
/**
|
||||
* disables a loglevel for the current logger_t-object
|
||||
* @brief Disables a loglevel for the current logger_t-object.
|
||||
*
|
||||
* @param this logger_t-object
|
||||
* @param log_level loglevel to disable
|
||||
* @return SUCCESS
|
||||
*/
|
||||
* @param this logger_t-object
|
||||
* @param log_level loglevel to enable
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*disable_level) (logger_t *this, logger_level_t log_level);
|
||||
|
||||
/**
|
||||
* @brief destroys a logger_t object
|
||||
* @brief destroys a logger_t object.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @return SUCCESS if succeeded, FAILED otherwise
|
||||
* @param this logger_t object
|
||||
* @return
|
||||
* - SUCCESS in any case
|
||||
*/
|
||||
status_t (*destroy) (logger_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor to create a logger_t-object
|
||||
* @brief Constructor to create a logger_t-object.
|
||||
*
|
||||
* @param logger_name Name for the logger_t-object
|
||||
* @param file FILE pointer to write the log-messages to. If NULL
|
||||
* syslogger is used.
|
||||
* @param log_level to assign to the new logger_t-object
|
||||
* @return logger_t-object or NULL if failed
|
||||
* @param logger_name Name for the logger_t-object
|
||||
* @param log_level or'ed set of log_levels to assign to the new logger_t-object
|
||||
* @return logger_t-object or NULL if failed
|
||||
*/
|
||||
logger_t *logger_create(char *logger_name, char *file, logger_level_t log_level);
|
||||
logger_t *logger_create(char *logger_name, logger_level_t log_level);
|
||||
|
||||
|
||||
#endif /*LOGGER_H_*/
|
||||
|
||||
|
|
Loading…
Reference in a new issue