added simple, fast, and efficient logging

This commit is contained in:
john30
2015-01-25 19:01:24 +01:00
parent 16c52e9a39
commit 3b968afb41
2 changed files with 246 additions and 0 deletions
+140
View File
@@ -0,0 +1,140 @@
/*
* Copyright (C) John Baier 2014-2015 <ebusd@johnm.de>
*
* This file is part of ebusd.
*
* ebusd 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.
*
* ebusd 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 ebusd. If not, see http://www.gnu.org/licenses/.
*/
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
using namespace std;
/** the name of each @a LogFacility. */
static const char *facilityNames[] = {
"main",
"network",
"bus",
"update",
"all",
NULL
};
/** the name of each @a LogLevel. */
static const char* levelNames[] = {
"none",
"error",
"notice",
"info",
"debug",
NULL
};
/** the bit combination of currently active log facilities (1 << @a LogFacility). */
static int logFacilites = LF_ALL;
/** the current log level. */
static LogLevel logLevel = ll_notice;
/** the current log FILE. */
static FILE* logFile = stdout;
bool setLogFacilities(const char* facilities)
{
char *opt = (char*)facilities, *value = NULL;
int newFacilites = 0;
while (*opt) {
int val = getsubopt(&opt, (char *const *)facilityNames, &value);
if (val < 0 || val > lf_COUNT || value)
return false;
if (val == lf_COUNT) {
newFacilites = LF_ALL;
break;
}
newFacilites |= 1<<val;
}
logFacilites = newFacilites;
return true;
}
bool setLogLevel(const char* level)
{
char *opt = (char*)level, *value = NULL;
int newLevel = 0;
if (*opt) {
int val = getsubopt(&opt, (char *const *)levelNames, &value);
if (val < 0 || val >= ll_COUNT || value || *opt)
return false;
newLevel = val;
}
logLevel = (LogLevel)newLevel;
return true;
}
bool setLogFile(const char* filename)
{
FILE* newFile = fopen(filename, "w");
if (newFile == NULL)
return false;
closeLogFile();
logFile = newFile;
return true;
}
void closeLogFile()
{
if (logFile != NULL) {
fclose(logFile);
logFile = NULL;
}
}
bool needsLog(const LogFacility facility, const LogLevel level)
{
return ((logFacilites & (1<<facility)) != 0)
&& (logLevel >= level);
}
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...)
{
struct timespec ts;
struct tm* tm;
clock_gettime(CLOCK_REALTIME, &ts);
tm = localtime(&ts.tv_sec);
char* buf;
va_list ap;
va_start(ap, message);
if (vasprintf(&buf, message, ap) >= 0 && buf) {
fprintf(logFile, "%04d-%02d-%02d %02d:%02d:%02d.%03ld [%s %s] %s\n",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
facilityNames[facility], levelNames[level], buf);
fflush(logFile);
}
va_end(ap);
if (buf)
free(buf);
}
+106
View File
@@ -0,0 +1,106 @@
/*
* Copyright (C) John Baier 2014-2015 <ebusd@johnm.de>
*
* This file is part of ebusd.
*
* ebusd 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.
*
* ebusd 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 ebusd. If not, see http://www.gnu.org/licenses/.
*/
#ifndef LIBUTILS_LOG_H_
#define LIBUTILS_LOG_H_
/** \file log.h */
/** the available log facilities. */
enum LogFacility {
lf_main=0, //!< main loop
lf_network, //!< network related
lf_bus, //!< eBUS related
lf_update, //!< updates found while listening to the bus
lf_COUNT=4 //!< number of available log facilities
};
/** macro for enabling all log facilities. */
#define LF_ALL ((1<<lf_main) | (1<<lf_network) | (1<<lf_bus) | (1<<lf_update))
/** the available log levels. */
enum LogLevel {
ll_none=0, //!< no level at all
ll_error, //!< error message
ll_notice, //!< important message
ll_info, //!< informational message
ll_debug, //!< debugging message (normally suppressed)
ll_COUNT=5 //!< number of available log levels
};
/**
* Set the log facilities from the string.
* @param facilities the string to parse the facilities from (separated by comma).
* @return true on success, false on error.
*/
bool setLogFacilities(const char* facilities);
/**
* Parse the log level from the string.
* @param level the level as string.
* @return true on success, false on error.
*/
bool setLogLevel(const char* level);
/**
* Set the log file to use.
* @param filename the name of the log file to use.
* @return true on success, false on error.
*/
bool setLogFile(const char* filename);
/**
* Close the log file if necessary.
*/
void closeLogFile();
/**
* Return whether logging is needed for the specified facility and level.
* @param facility the @a LogFacility of the message to check.
* @param level the @a LogLevel of the message to check.
* @return true when logging is needed for the specified facility and level.
*/
bool needsLog(const LogFacility facility, const LogLevel level);
/**
* Log the message for the specified @a LogFacility and @a LogLevel (even if logging is not needed for the facility/level).
* @param facility the @a LogFacility of the message to log.
* @param level the @a LogLevel of the message to log.
* @param message the message to log.
* @param ... variable arguments depending on the @a message.
*/
void logWrite(const LogFacility facility, const LogLevel level, const char* message, ...);
/** A macro that calls the logging function only if needed. */
#define LOG(facility, level, ...) (needsLog(facility, level) ? logWrite(facility, level, __VA_ARGS__) : void(0))
/** A macro for an error message that calls the logging function only if needed. */
#define logError(facility, ...) (needsLog(facility, ll_error) ? logWrite(facility, ll_error, __VA_ARGS__) : void(0))
/** A macro for a notice message that calls the logging function only if needed. */
#define logNotice(facility, ...) (needsLog(facility, ll_notice) ? logWrite(facility, ll_notice, __VA_ARGS__) : void(0))
/** A macro for an info message that calls the logging function only if needed. */
#define logInfo(facility, ...) (needsLog(facility, ll_info) ? logWrite(facility, ll_info, __VA_ARGS__) : void(0))
/** A macro for a debug message that calls the logging function only if needed. */
#define logDebug(facility, ...) (needsLog(facility, ll_debug) ? logWrite(facility, ll_debug, __VA_ARGS__) : void(0))
#endif // LIBUTILS_LOG_H_