removed outdated files
This commit is contained in:
@@ -6,10 +6,8 @@ noinst_LIBRARIES = libutils.a
|
|||||||
|
|
||||||
libutils_a_SOURCES = appl.cpp \
|
libutils_a_SOURCES = appl.cpp \
|
||||||
appl.h \
|
appl.h \
|
||||||
daemon.cpp \
|
log.cpp \
|
||||||
daemon.h \
|
log.h \
|
||||||
logger.cpp \
|
|
||||||
logger.h \
|
|
||||||
tcpsocket.cpp \
|
tcpsocket.cpp \
|
||||||
tcpsocket.h \
|
tcpsocket.h \
|
||||||
thread.cpp \
|
thread.cpp \
|
||||||
|
|||||||
@@ -1,119 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
|
||||||
*
|
|
||||||
* 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 "daemon.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstring>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Daemon& Daemon::Instance()
|
|
||||||
{
|
|
||||||
static Daemon instance;
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Daemon::run(const char* pidfile)
|
|
||||||
{
|
|
||||||
m_status = false;
|
|
||||||
m_pidfile = pidfile;
|
|
||||||
m_pidfd = 0;
|
|
||||||
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
// fork off the parent process
|
|
||||||
pid = fork();
|
|
||||||
|
|
||||||
if (pid < 0) {
|
|
||||||
cerr << "daemon fork() failed." << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we got a good PID, then we can exit the parent process
|
|
||||||
if (pid > 0) {
|
|
||||||
// printf("Child process created: %d\n", pid);
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// At this point we are executing as the child process
|
|
||||||
|
|
||||||
// Set file permissions 750
|
|
||||||
umask(027);
|
|
||||||
|
|
||||||
// Create a new SID for the child process and
|
|
||||||
// detach the process from the parent (normally a shell)
|
|
||||||
if (setsid() < 0) {
|
|
||||||
cerr << "daemon setsid() failed." << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change the current working directory. This prevents the current
|
|
||||||
// directory from being locked; hence not being able to remove it.
|
|
||||||
if (chdir("/tmp") < 0) { //DAEMON_WORKDIR
|
|
||||||
cerr << "daemon chdir() failed." << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close stdin, stdout and stderr
|
|
||||||
close(STDIN_FILENO);
|
|
||||||
close(STDOUT_FILENO);
|
|
||||||
close(STDERR_FILENO);
|
|
||||||
|
|
||||||
// write pidfile and try to lock it
|
|
||||||
if (m_pidfile != NULL && pidfile_open() == false) {
|
|
||||||
cerr << "can't open pidfile: %s" << m_pidfile << endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_status = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Daemon::pidfile_open()
|
|
||||||
{
|
|
||||||
char pid[10];
|
|
||||||
|
|
||||||
m_pidfd = open(m_pidfile, O_RDWR|O_CREAT, 0600);
|
|
||||||
if (m_pidfd < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (lockf(m_pidfd, F_TLOCK, 0) < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
sprintf(pid, "%d\n", getpid());
|
|
||||||
if (write(m_pidfd, pid, strlen(pid)) < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Daemon::pidfile_close()
|
|
||||||
{
|
|
||||||
if (close(m_pidfd) < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (remove(m_pidfile) < 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
|
||||||
*
|
|
||||||
* 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_DAEMON_H_
|
|
||||||
#define LIBUTILS_DAEMON_H_
|
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
/** \file daemon.h */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class to daemonize a process.
|
|
||||||
*/
|
|
||||||
class Daemon
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* create an instance and return the reference.
|
|
||||||
* @return the reference to instance.
|
|
||||||
*/
|
|
||||||
static Daemon& Instance();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* daemonize act process.
|
|
||||||
* @param pidfile the name of the pid file.
|
|
||||||
*/
|
|
||||||
void run(const char* pidfile);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* stop daemon and delete the pid file.
|
|
||||||
*/
|
|
||||||
void stop() { if (m_pidfile != NULL) pidfile_close(); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* show actual status if daemonize.
|
|
||||||
* @return true if process is a daemon.
|
|
||||||
*/
|
|
||||||
bool status() { return m_status; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
/**
|
|
||||||
* private construtor.
|
|
||||||
*/
|
|
||||||
Daemon() : m_status(false), m_pidfile(NULL), m_pidfd(0) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* private copy construtor.
|
|
||||||
* @param reference to an instance.
|
|
||||||
*/
|
|
||||||
Daemon(const Daemon&);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* private = operator.
|
|
||||||
* @param reference to an instance.
|
|
||||||
* @return reference to instance.
|
|
||||||
*/
|
|
||||||
Daemon& operator=(const Daemon&);
|
|
||||||
|
|
||||||
/** status of process; true if we are a daemon */
|
|
||||||
bool m_status;
|
|
||||||
|
|
||||||
/** name of the pid file*/
|
|
||||||
const char* m_pidfile;
|
|
||||||
|
|
||||||
/** file descriptor of the pid file */
|
|
||||||
int m_pidfd;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* creates a pid file for process.
|
|
||||||
* @return true if success.
|
|
||||||
*/
|
|
||||||
bool pidfile_open();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* close and delete the pid file.
|
|
||||||
* @return true if success.
|
|
||||||
*/
|
|
||||||
bool pidfile_close();
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // LIBUTILS_DAEMON_H_
|
|
||||||
@@ -1,258 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
|
||||||
*
|
|
||||||
* 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 "logger.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cstring>
|
|
||||||
#include <ctime>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
/** static char array with logging area names */
|
|
||||||
static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus", "upd" };
|
|
||||||
|
|
||||||
/** static char array with logging level names */
|
|
||||||
static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" };
|
|
||||||
|
|
||||||
int calcAreaMask(const string areas)
|
|
||||||
{
|
|
||||||
int mask = 0;
|
|
||||||
|
|
||||||
// prepare data
|
|
||||||
string token;
|
|
||||||
istringstream stream(areas);
|
|
||||||
vector<string> cmd;
|
|
||||||
|
|
||||||
while (getline(stream, token, ',') != 0)
|
|
||||||
cmd.push_back(token);
|
|
||||||
|
|
||||||
for (vector<string>::iterator it = cmd.begin() ; it != cmd.end(); ++it)
|
|
||||||
for (int i = 0; i < Size_of_Areas; i++) {
|
|
||||||
if (strcasecmp("ALL", it->c_str()) == 0)
|
|
||||||
return (1 << (int)Size_of_Areas) - 1;
|
|
||||||
|
|
||||||
if (strcasecmp(AreaNames[i], it->c_str()) == 0)
|
|
||||||
mask |= 1 << i;
|
|
||||||
}
|
|
||||||
|
|
||||||
return mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
LevelType calcLevel(const string level)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < Size_of_Level; i++)
|
|
||||||
if (strcasecmp(LevelNames[i], level.c_str()) == 0)
|
|
||||||
return (LevelType)i;
|
|
||||||
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LogMessage::LogMessage(const int area, const int level, const string text)
|
|
||||||
: m_area(area), m_level(level), m_text(text)
|
|
||||||
{
|
|
||||||
char time[24];
|
|
||||||
struct timeval tv;
|
|
||||||
struct timezone tz;
|
|
||||||
struct tm* tm;
|
|
||||||
|
|
||||||
gettimeofday(&tv, &tz);
|
|
||||||
tm = localtime(&tv.tv_sec);
|
|
||||||
|
|
||||||
sprintf(&time[0], "%04d-%02d-%02d %02d:%02d:%02d.%03ld",
|
|
||||||
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
|
|
||||||
tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec/1000);
|
|
||||||
|
|
||||||
m_time = string(time);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LogSink::~LogSink()
|
|
||||||
{
|
|
||||||
m_logQueue.add(NULL);
|
|
||||||
|
|
||||||
Thread::join();
|
|
||||||
|
|
||||||
while (m_logQueue.size() > 0) {
|
|
||||||
LogMessage* message = m_logQueue.remove(false);
|
|
||||||
delete message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogSink::addMessage(const LogMessage& message)
|
|
||||||
{
|
|
||||||
m_logQueue.add(new LogMessage(LogMessage(message)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogSink::run()
|
|
||||||
{
|
|
||||||
while (isRunning() == true) {
|
|
||||||
LogMessage* message = m_logQueue.remove();
|
|
||||||
if (message == NULL)
|
|
||||||
break;
|
|
||||||
|
|
||||||
write(*message);
|
|
||||||
delete message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void LogConsole::write(const LogMessage& message) const
|
|
||||||
{
|
|
||||||
cout << message.getTime() << " ["
|
|
||||||
<< AreaNames[(int)message.getArea()] << " "
|
|
||||||
<< LevelNames[message.getLevel()] << "] "
|
|
||||||
<< message.getText() << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void LogFile::write(const LogMessage& message) const
|
|
||||||
{
|
|
||||||
fstream file(m_file.c_str(), ios::out | ios::app);
|
|
||||||
|
|
||||||
if (file.is_open() == true) {
|
|
||||||
file << message.getTime() << " ["
|
|
||||||
<< AreaNames[(int)message.getArea()] << " "
|
|
||||||
<< LevelNames[message.getLevel()] << "] "
|
|
||||||
<< message.getText() << endl;
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Logger& Logger::Instance()
|
|
||||||
{
|
|
||||||
static Logger instance;
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::~Logger()
|
|
||||||
{
|
|
||||||
if (m_sink != NULL) {
|
|
||||||
delete m_sink;
|
|
||||||
m_sink = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger& Logger::operator+=(LogSink* sink)
|
|
||||||
{
|
|
||||||
if (m_sink != NULL)
|
|
||||||
delete m_sink;
|
|
||||||
m_sink = sink;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger& Logger::operator-=(const LogSink* sink)
|
|
||||||
{
|
|
||||||
if (sink != NULL && sink == m_sink) {
|
|
||||||
delete m_sink;
|
|
||||||
m_sink = NULL;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Logger::setAreaMask(const int& areaMask)
|
|
||||||
{
|
|
||||||
if (m_sink != NULL)
|
|
||||||
m_sink->setAreaMask(areaMask);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Logger::setLevel(const int& level)
|
|
||||||
{
|
|
||||||
if (m_sink != NULL)
|
|
||||||
m_sink->setLevel(level);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Logger::hasSink(const int area, const int level)
|
|
||||||
{
|
|
||||||
if (m_sink != NULL) {
|
|
||||||
if (((m_sink->getAreaMask() & (1 << area)) != 0
|
|
||||||
&& m_sink->getLevel() >= level)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Logger::log(const int area, const int level, const string& data, ...)
|
|
||||||
{
|
|
||||||
if (m_direct == true || isRunning() == true) {
|
|
||||||
char* tmp;
|
|
||||||
va_list ap;
|
|
||||||
va_start(ap, data);
|
|
||||||
|
|
||||||
if (vasprintf(&tmp, data.c_str(), ap) != -1) {
|
|
||||||
string buffer(tmp);
|
|
||||||
LogMessage* message = new LogMessage(area, level, buffer);
|
|
||||||
if (m_direct == true)
|
|
||||||
handleMessage(message);
|
|
||||||
else
|
|
||||||
m_logQueue.add(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
va_end(ap);
|
|
||||||
free(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Logger::handleMessage(LogMessage* message) {
|
|
||||||
if (message == NULL)
|
|
||||||
return;
|
|
||||||
if (m_sink != NULL) {
|
|
||||||
if (((m_sink->getAreaMask() & (1 << message->getArea())) != 0
|
|
||||||
&& m_sink->getLevel() >= message->getLevel())) {
|
|
||||||
m_sink->addMessage(*message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete message;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Logger::start(const char* name)
|
|
||||||
{
|
|
||||||
m_direct = false;
|
|
||||||
return Thread::start(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Logger::run()
|
|
||||||
{
|
|
||||||
|
|
||||||
while (isRunning() == true) {
|
|
||||||
LogMessage* message = m_logQueue.remove();
|
|
||||||
if (message == NULL)
|
|
||||||
break;
|
|
||||||
handleMessage(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Logger::stop()
|
|
||||||
{
|
|
||||||
m_logQueue.add(NULL);
|
|
||||||
usleep(100000);
|
|
||||||
Thread::stop();
|
|
||||||
}
|
|
||||||
@@ -1,372 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
|
||||||
*
|
|
||||||
* 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_LOGGER_H_
|
|
||||||
#define LIBUTILS_LOGGER_H_
|
|
||||||
|
|
||||||
#include "wqueue.h"
|
|
||||||
#include "thread.h"
|
|
||||||
#include <string>
|
|
||||||
#include <functional>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <vector>
|
|
||||||
#include <cstdarg>
|
|
||||||
|
|
||||||
/** \file logger.h */
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
/** available types for all subsystems */
|
|
||||||
enum AreasType {
|
|
||||||
bas=0, /*!< basis */
|
|
||||||
net, /*!< network */
|
|
||||||
bus, /*!< ebus */
|
|
||||||
upd, /*!< updates found while listening to the bus */
|
|
||||||
Size_of_Areas=4 /*!< number of possible areas */
|
|
||||||
};
|
|
||||||
|
|
||||||
/** available logging levels */
|
|
||||||
enum LevelType {
|
|
||||||
error=0, /*!< silent run, only errors will be printed */
|
|
||||||
event, /*!< only interesting message for normal use */
|
|
||||||
trace, /*!< most of the information for normal use */
|
|
||||||
debug, /*!< print internal states too */
|
|
||||||
Size_of_Level /*!< number of possible levels */
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate the mask of logging areas from string.
|
|
||||||
* @param areas the string to parse the areas from.
|
|
||||||
* @return the bit combination of @a AreasType.
|
|
||||||
*/
|
|
||||||
int calcAreaMask(const string areas);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate the log level from string.
|
|
||||||
* @param level the level as string.
|
|
||||||
* @return @a the LevelType.
|
|
||||||
*/
|
|
||||||
LevelType calcLevel(const string level);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class which describes a logging message itself.
|
|
||||||
*/
|
|
||||||
class LogMessage
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* creates a new logging message.
|
|
||||||
* @param area the logging area of the message.
|
|
||||||
* @param level the logging level of the message.
|
|
||||||
* @param text the logging message.
|
|
||||||
*/
|
|
||||||
LogMessage(const int area, const int level, const string text);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the logging area.
|
|
||||||
* @return the logging area.
|
|
||||||
*/
|
|
||||||
int getArea() const { return m_area; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the logging level.
|
|
||||||
* @return the logging level.
|
|
||||||
*/
|
|
||||||
int getLevel() const { return m_level; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the logging text.
|
|
||||||
* @return the logging text.
|
|
||||||
*/
|
|
||||||
string getText() const { return m_text.c_str(); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the logging timestamp.
|
|
||||||
* @return the logging timestamp.
|
|
||||||
*/
|
|
||||||
string getTime() const { return m_time.c_str(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
/** the logging area. */
|
|
||||||
int m_area;
|
|
||||||
|
|
||||||
/** the logging level. */
|
|
||||||
int m_level;
|
|
||||||
|
|
||||||
/** the logging message. */
|
|
||||||
string m_text;
|
|
||||||
|
|
||||||
/** the logging timestamp. */
|
|
||||||
string m_time;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* base class for all type of logging sinks.
|
|
||||||
*/
|
|
||||||
class LogSink : public Thread
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* creates a virtual logging sink.
|
|
||||||
* @param areaMask the logging area mask.
|
|
||||||
* @param level the logging level.
|
|
||||||
*/
|
|
||||||
LogSink(const int areaMask, const int level) : m_areaMask(areaMask), m_level(level) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* destructor.
|
|
||||||
*/
|
|
||||||
virtual ~LogSink();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* adds the logging message to internal message queue.
|
|
||||||
* @param message a reference to logging message.
|
|
||||||
*/
|
|
||||||
void addMessage(const LogMessage& message);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* endless loop for logging sink instance.
|
|
||||||
*/
|
|
||||||
void run();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the logging area mask.
|
|
||||||
* @return the logging area mask.
|
|
||||||
*/
|
|
||||||
int getAreaMask() const { return m_areaMask; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set the logging area mask.
|
|
||||||
* @param areaMask the logging area mask.
|
|
||||||
*/
|
|
||||||
void setAreaMask(const int& areaMask) { m_areaMask = areaMask; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the logging level.
|
|
||||||
* @return the logging level.
|
|
||||||
*/
|
|
||||||
int getLevel() const { return (m_level); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set the logging level.
|
|
||||||
* @param level the logging level.
|
|
||||||
*/
|
|
||||||
void setLevel(const int& level) { m_level = level; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
/** queue for logging messages */
|
|
||||||
WQueue<LogMessage*> m_logQueue;
|
|
||||||
|
|
||||||
private:
|
|
||||||
/** the logging area mask. */
|
|
||||||
int m_areaMask;
|
|
||||||
|
|
||||||
/** the logging level. */
|
|
||||||
int m_level;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
/**
|
|
||||||
* virtual function for writing the logging message.
|
|
||||||
* @param message the logging message.
|
|
||||||
*/
|
|
||||||
virtual void write(const LogMessage& message) const = 0;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class for console logging sink type.
|
|
||||||
*/
|
|
||||||
class LogConsole : public LogSink
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* creates a console logging sink.
|
|
||||||
* @param areaMask the logging area mask.
|
|
||||||
* @param level the logging level.
|
|
||||||
* @param name the thread name for logging sink.
|
|
||||||
*/
|
|
||||||
LogConsole(const int areaMask, const int level, const char* name)
|
|
||||||
: LogSink(areaMask, level) { this->start(name); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* destructor.
|
|
||||||
*/
|
|
||||||
virtual ~LogConsole() {}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
/**
|
|
||||||
* write the logging message to stdout.
|
|
||||||
* @param message the logging message.
|
|
||||||
*/
|
|
||||||
virtual void write(const LogMessage& message) const;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class for logfile logging sink type.
|
|
||||||
*/
|
|
||||||
class LogFile : public LogSink
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* creates a log file logging sink.
|
|
||||||
* @param areaMask the logging area mask.
|
|
||||||
* @param level the logging level.
|
|
||||||
* @param name the thread name for logging sink.
|
|
||||||
* @param file the log file.
|
|
||||||
*/
|
|
||||||
LogFile(const int areaMask, const int level, const char* name, const char* file)
|
|
||||||
: LogSink(areaMask, level), m_file(file) { this->start(name); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* destructor.
|
|
||||||
*/
|
|
||||||
virtual ~LogFile() {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
/** the logging file */
|
|
||||||
string m_file;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
/**
|
|
||||||
* write the logging message to specific log file.
|
|
||||||
* @param message the logging message.
|
|
||||||
*/
|
|
||||||
virtual void write(const LogMessage& message) const;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* logger base class which provide the logging interface.
|
|
||||||
*/
|
|
||||||
class Logger : public Thread
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* create an instance and return the reference.
|
|
||||||
* @return the reference to instance.
|
|
||||||
*/
|
|
||||||
static Logger& Instance();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* destructor.
|
|
||||||
*/
|
|
||||||
virtual ~Logger();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a @a LogSink.
|
|
||||||
* @param sink the used @a LogSink.
|
|
||||||
* @return the reference to this @a Logger.
|
|
||||||
*/
|
|
||||||
Logger& operator+=(LogSink* sink);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove a @a LogSink and delete it.
|
|
||||||
* @param sink the used @a LogSink.
|
|
||||||
* @return the reference to this @a Logger.
|
|
||||||
*/
|
|
||||||
Logger& operator-=(const LogSink* sink);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the logging area mask on all @a LogSink instances.
|
|
||||||
* @param areaMask the logging area mask.
|
|
||||||
*/
|
|
||||||
void setAreaMask(const int& areaMask);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the logging level on all @a LogSink instances.
|
|
||||||
* @param level the logging level.
|
|
||||||
*/
|
|
||||||
void setLevel(const int& level);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return whether a @a LogSink is available that will produce output for the specified area and level.
|
|
||||||
* @param area the logging area of the message.
|
|
||||||
* @param level the logging level of the message.
|
|
||||||
* @return whether a @a LogSink is available that will produce output for the specified area and level.
|
|
||||||
*/
|
|
||||||
bool hasSink(const int area, const int level);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* creates a logging message and add them to internal message queue.
|
|
||||||
* @param area the logging area of the message.
|
|
||||||
* @param level the logging level of the message.
|
|
||||||
* @param text the logging message.
|
|
||||||
* @param ... possible 'variable argument lists'.
|
|
||||||
*/
|
|
||||||
void log(const int area, const int level, const string& text, ...);
|
|
||||||
|
|
||||||
//@copydoc
|
|
||||||
virtual bool start(const char* name);
|
|
||||||
|
|
||||||
//@copydoc
|
|
||||||
virtual void stop();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
//@copydoc
|
|
||||||
virtual void run();
|
|
||||||
|
|
||||||
private:
|
|
||||||
/**
|
|
||||||
* private constructor.
|
|
||||||
*/
|
|
||||||
Logger() : m_direct(true), m_sink(NULL) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* private copy constructor.
|
|
||||||
* @param reference to an instance.
|
|
||||||
*/
|
|
||||||
Logger(const Logger&);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* private = operator.
|
|
||||||
* @param reference to an instance.
|
|
||||||
* @return reference to instance.
|
|
||||||
*/
|
|
||||||
Logger& operator=(const Logger&);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Distribute the @a LogMessage to all known sinks and delete it afterwards.
|
|
||||||
* @param mesage the @a LogMessage to distribute.
|
|
||||||
*/
|
|
||||||
void handleMessage(LogMessage* message);
|
|
||||||
|
|
||||||
/** true to directly log to all sinks, false to buffer via @a m_logQueue. */
|
|
||||||
bool m_direct;
|
|
||||||
|
|
||||||
/** the used @a LogSink, or NULL. */
|
|
||||||
LogSink* m_sink;
|
|
||||||
|
|
||||||
/** queue for logging messages. */
|
|
||||||
WQueue<LogMessage*> m_logQueue;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // LIBUTILS_LOGGER_H_
|
|
||||||
Reference in New Issue
Block a user