Add Log class, only implemented for Linux. See #103
This commit is contained in:
parent
d7e76f1bc1
commit
72188ef02f
3 changed files with 139 additions and 0 deletions
|
@ -19,6 +19,7 @@ SET( telldus-service_SRCS
|
||||||
Device.cpp
|
Device.cpp
|
||||||
DeviceManager.cpp
|
DeviceManager.cpp
|
||||||
Event.cpp
|
Event.cpp
|
||||||
|
Log.cpp
|
||||||
Sensor.cpp
|
Sensor.cpp
|
||||||
Settings.cpp
|
Settings.cpp
|
||||||
TelldusMain.cpp
|
TelldusMain.cpp
|
||||||
|
@ -78,6 +79,7 @@ SET( telldus-service_HDRS
|
||||||
DeviceManager.h
|
DeviceManager.h
|
||||||
Event.h
|
Event.h
|
||||||
EventHandler.h
|
EventHandler.h
|
||||||
|
Log.h
|
||||||
Sensor.h
|
Sensor.h
|
||||||
Settings.h
|
Settings.h
|
||||||
TelldusMain.h
|
TelldusMain.h
|
||||||
|
@ -144,6 +146,7 @@ ELSE (APPLE) #### Linux ####
|
||||||
SET(DEFAULT_FTDI_ENGINE "libftdi")
|
SET(DEFAULT_FTDI_ENGINE "libftdi")
|
||||||
FIND_LIBRARY(CONFUSE_LIBRARY confuse)
|
FIND_LIBRARY(CONFUSE_LIBRARY confuse)
|
||||||
ADD_DEFINITIONS( -D_CONFUSE )
|
ADD_DEFINITIONS( -D_CONFUSE )
|
||||||
|
ADD_DEFINITIONS( -D_LINUX )
|
||||||
|
|
||||||
SET( telldus-service_TARGET telldusd )
|
SET( telldus-service_TARGET telldusd )
|
||||||
LIST(APPEND telldus-service_SRCS
|
LIST(APPEND telldus-service_SRCS
|
||||||
|
|
106
telldus-core/service/Log.cpp
Normal file
106
telldus-core/service/Log.cpp
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
#include "Log.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#ifdef _LINUX
|
||||||
|
#include <syslog.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class Log::PrivateData {
|
||||||
|
public:
|
||||||
|
PrivateData() : logOutput(Log::System) {}
|
||||||
|
|
||||||
|
Log::LogOutput logOutput;
|
||||||
|
|
||||||
|
static Log *instance;
|
||||||
|
};
|
||||||
|
|
||||||
|
Log *Log::PrivateData::instance = 0;
|
||||||
|
|
||||||
|
Log::Log()
|
||||||
|
:d(new PrivateData)
|
||||||
|
{
|
||||||
|
#ifdef _LINUX
|
||||||
|
setlogmask(LOG_UPTO(LOG_INFO));
|
||||||
|
openlog("telldusd", LOG_CONS, LOG_USER);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::~Log() {
|
||||||
|
delete d;
|
||||||
|
#ifdef _LINUX
|
||||||
|
closelog();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::debug(const char *fmt, ...) {
|
||||||
|
Log *log = Log::instance();
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
log->message(Debug, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::notice(const char *fmt, ...) {
|
||||||
|
Log *log = Log::instance();
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
log->message(Notice, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::warning(const char *fmt, ...) {
|
||||||
|
Log *log = Log::instance();
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
log->message(Warning, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::error(const char *fmt, ...) {
|
||||||
|
Log *log = Log::instance();
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
log->message(Error, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::setLogOutput(LogOutput logOutput) {
|
||||||
|
Log *log = Log::instance();
|
||||||
|
log->d->logOutput = logOutput;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Log::message(Log::LogLevel logLevel, const char *format, va_list ap) const {
|
||||||
|
if (d->logOutput == StdOut) {
|
||||||
|
FILE *stream = stdout;
|
||||||
|
if (logLevel == Warning || logLevel == Error) {
|
||||||
|
stream = stderr;
|
||||||
|
}
|
||||||
|
vfprintf(stream, format, ap);
|
||||||
|
fprintf(stream, "\n");
|
||||||
|
fflush(stream);
|
||||||
|
} else {
|
||||||
|
#ifdef _LINUX
|
||||||
|
switch (logLevel) {
|
||||||
|
case Debug:
|
||||||
|
vsyslog(LOG_DEBUG, format, ap);
|
||||||
|
break;
|
||||||
|
case Notice:
|
||||||
|
vsyslog(LOG_NOTICE, format, ap);
|
||||||
|
break;
|
||||||
|
case Warning:
|
||||||
|
vsyslog(LOG_WARNING, format, ap);
|
||||||
|
break;
|
||||||
|
case Error:
|
||||||
|
vsyslog(LOG_ERR, format, ap);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log *Log::instance() {
|
||||||
|
if (PrivateData::instance == 0) {
|
||||||
|
PrivateData::instance = new Log();
|
||||||
|
}
|
||||||
|
return PrivateData::instance;
|
||||||
|
}
|
30
telldus-core/service/Log.h
Normal file
30
telldus-core/service/Log.h
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef LOG_H
|
||||||
|
#define LOG_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
class Log {
|
||||||
|
public:
|
||||||
|
enum LogLevel { Debug, Notice, Warning, Error };
|
||||||
|
enum LogOutput { StdOut, System };
|
||||||
|
virtual ~Log();
|
||||||
|
|
||||||
|
static void debug(const char *fmt, ...);
|
||||||
|
static void notice(const char *fmt, ...);
|
||||||
|
static void warning(const char *fmt, ...);
|
||||||
|
static void error(const char *fmt, ...);
|
||||||
|
|
||||||
|
static void setLogOutput(LogOutput logOutput);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Log();
|
||||||
|
void message(LogLevel logLevel, const char *format, va_list ap) const;
|
||||||
|
static Log *instance();
|
||||||
|
|
||||||
|
private:
|
||||||
|
class PrivateData;
|
||||||
|
PrivateData *d;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //LOG_H
|
Loading…
Add table
Add a link
Reference in a new issue