1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

feature #4097: Use glibc to log thourgh syslog, removes log4cpp

dependency
This commit is contained in:
Ruben S. Montero 2016-03-02 17:07:27 +01:00
parent 16e0f7cabe
commit 7ca1036683
8 changed files with 102 additions and 290 deletions

View File

@ -132,15 +132,6 @@ else:
main_env.Append(new_xmlrpc='no')
main_env.Append(CPPFLAGS=["-DOLD_XMLRPC"])
# SysLog
syslog=ARGUMENTS.get('syslog', 'no')
if syslog=='yes':
main_env.Append(syslog='yes')
main_env.Append(CPPFLAGS=["-DSYSLOG_LOG"])
main_env.Append(LIBS=['pthread','log4cpp'])
else:
main_env.Append(syslog='no')
# xmlrpc
xmlrpc_dir=ARGUMENTS.get('xmlrpc', 'none')
if xmlrpc_dir!='none':

View File

@ -20,13 +20,10 @@
#include <string>
#include <fstream>
#include <stdexcept>
#include <syslog.h>
#include "PoolObjectSQL.h"
#ifdef SYSLOG_LOG
# include "log4cpp/Priority.hh"
#endif /* SYSLOG_LOG */
using namespace std;
/**
@ -78,6 +75,7 @@ protected:
/**
* Zone ID for log messages, for all Log instances
* M
*/
static unsigned int zone_id;
};
@ -144,14 +142,14 @@ private:
/* -------------------------------------------------------------------------- */
/**
*
A Send log messages to the standard output stream std::clog
*/
class CerrLog : public Log
class StdLog : public Log
{
public:
CerrLog(const MessageType level = WARNING):Log(level){};
StdLog(const MessageType level = WARNING):Log(level){};
~CerrLog(){};
~StdLog(){};
void log(
const char * module,
@ -162,8 +160,6 @@ public:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#ifdef SYSLOG_LOG
/**
* Send log messages to syslog
*/
@ -173,114 +169,41 @@ public:
SysLog(const MessageType level,
const string& label);
virtual ~SysLog() {};
virtual void log(
const char * module,
const MessageType type,
const char * message);
static log4cpp::Priority::PriorityLevel get_priority_level(
const MessageType level);
protected:
/**
* Specialized constructor only for derived classes that uses an initialzed
* SysLog system.
*/
SysLog(const MessageType level):Log(level){};
/**
* This is the root category name used by any syslog resource
* in the process
*/
static const char * CATEGORY;
/**
* This is the daemon name+pid, used to label every message in the process
*/
static string LABEL;
};
#else
/**
* Dummy syslog class
*/
class SysLog : public Log
{
public:
SysLog(const MessageType level,
const string& label) {
throw runtime_error("Aborting oned, SysLog support not compiled!");
};
int oid,
const PoolObjectSQL::ObjectType obj_type);
virtual ~SysLog() {};
virtual void log(
const char * module,
const MessageType type,
const char * message) {};
};
#endif /* SYSLOG_LOG */
#ifdef SYSLOG_LOG
/**
* Send log messages to syslog per resource. It requires a Root Syslog
* to be initialized before using a SysLogResource
*/
class SysLogResource : public SysLog
{
public:
SysLogResource(
int oid,
const PoolObjectSQL::ObjectType obj_type,
const MessageType clevel);
virtual ~SysLogResource(){};
~SysLog(){};
void log(
const char * module,
const MessageType type,
const char * message);
protected:
/**
* This is the resource category name used by any syslog resource
* in the process
* Return the associated syslog level
*/
static const char * CATEGORY;
inline static int level(const MessageType level)
{
switch (level)
{
case Log::ERROR:
return LOG_ERR;
case Log::WARNING:
return LOG_WARNING;
case Log::INFO:
return LOG_INFO;
case Log::DEBUG:
case Log::DDEBUG:
case Log::DDDEBUG:
return LOG_DEBUG;
}
/**
* The resource log label
*/
string obj_label;
return LOG_INFO;
}
private:
string resource_label;
};
#else
/**
* Dummy SysLogResource class
*/
class SysLogResource : public SysLog
{
public:
SysLogResource(int oid,
const PoolObjectSQL::ObjectType obj_type,
const MessageType clevel):SysLog(clevel, "") {
throw runtime_error("Aborting oned, SysLog support not compiled!");
};
virtual ~SysLogResource(){};
void log(
const char * module,
const MessageType type,
const char * message) {};
};
#endif /* SYSLOG_LOG */
#endif /* _LOG_H_ */

View File

@ -18,12 +18,10 @@
#define _NEBULA_LOG_H_
#include "Log.h"
#include "NebulaUtil.h"
#include <sstream>
#ifdef SYSLOG_LOG
# include <syslog.h>
#endif /* SYSLOG_LOG */
#include <syslog.h>
using namespace std;
@ -36,7 +34,7 @@ public:
enum LogType {
FILE = 0,
FILE_TS = 1,
CERR = 2,
STD = 2,
SYSLOG = 3,
UNDEFINED = 4
};
@ -52,26 +50,28 @@ public:
ios_base::openmode mode,
const string& daemon)
{
_log_type = ltype;
switch(ltype)
{
case FILE:
NebulaLog::logger = new FileLog(filename,clevel,mode);
NebulaLog::logger = new FileLog(filename, clevel, mode);
break;
case FILE_TS:
NebulaLog::logger = new FileLogTS(filename,clevel,mode);
NebulaLog::logger = new FileLogTS(filename, clevel, mode);
break;
case SYSLOG:
NebulaLog::logger = new SysLog(clevel, daemon);
break;
default:
NebulaLog::logger = new CerrLog(clevel);
NebulaLog::logger = new StdLog(clevel);
break;
}
};
static LogType str_to_type(string& type)
{
transform(type.begin(), type.end(), type.begin(), (int(*)(int))toupper);
one_util::toupper(type);
if (type == "FILE")
{
@ -81,9 +81,9 @@ public:
{
return SYSLOG;
}
else if (type == "STDERR")
else if (type == "STD")
{
return CERR;
return STD;
}
return UNDEFINED;
@ -123,11 +123,18 @@ public:
return logger->get_log_level();
};
static LogType log_type()
{
return _log_type;
};
private:
NebulaLog(){};
~NebulaLog(){};
static Log * logger;
static LogType _log_type;
static Log * logger;
};
/* -------------------------------------------------------------------------- */

View File

@ -46,6 +46,7 @@
# system: defines the logging system:
# file to log in the oned.log file
# syslog to use the syslog facilities
# std to use the default log stream (stderr) to use with systemd
# debug_level: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
#
# VM_SUBMIT_ON_HOLD: Forces VMs to be created on hold state instead of pending.

View File

@ -25,18 +25,6 @@
#include <sys/types.h>
#include <unistd.h>
#ifdef SYSLOG_LOG
# include <syslog.h>
# include "log4cpp/Category.hh"
# include "log4cpp/CategoryStream.hh"
# include "log4cpp/Appender.hh"
# include "log4cpp/SyslogAppender.hh"
# include "log4cpp/Layout.hh"
# include "log4cpp/PatternLayout.hh"
# include "log4cpp/Priority.hh"
#endif /* SYSLOG_LOG */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -119,7 +107,7 @@ void FileLog::log(
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void CerrLog::log(
void StdLog::log(
const char * module,
const MessageType type,
const char * message)
@ -140,53 +128,42 @@ void CerrLog::log(
// Get rid of final enter character
str[24] = '\0';
cerr << str << " ";
cerr << "[Z"<< zone_id<< "]";
cerr << "[" << module << "]";
cerr << "[" << error_names[type] << "]: ";
cerr << message;
cerr << endl;
std::clog << str << " ";
std::clog << "[Z"<< zone_id<< "]";
std::clog << "[" << module << "]";
std::clog << "[" << error_names[type] << "]: ";
std::clog << message;
std::clog << endl;
cerr.flush();
std::clog.flush();
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#ifdef SYSLOG_LOG
const char * SysLog::CATEGORY = "ROOT";
string SysLog::LABEL;
/* -------------------------------------------------------------------------- */
SysLog::SysLog(const MessageType level,
const string& label):Log(level)
const string& label):Log(level), resource_label("")
{
static bool initialized = false;
if (!initialized) //Initialize just once for all SysLog instances
{
ostringstream oss;
log4cpp::Appender *appender;
oss << label << "[" << getpid() << "]";
LABEL = oss.str();
appender = new log4cpp::SyslogAppender(CATEGORY, LABEL, LOG_DAEMON);
appender->setLayout(new log4cpp::PatternLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(SysLog::get_priority_level(level));
root.addAppender(appender);
openlog(label.c_str(), LOG_PID, LOG_DAEMON);
initialized = true;
}
};
SysLog::SysLog(const MessageType level, int oid,
const PoolObjectSQL::ObjectType obj_type):Log(level)
{
ostringstream oss;
oss << "[" << PoolObjectSQL::type_to_str(obj_type) << " " << oid << "]";
resource_label = oss.str();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -195,123 +172,27 @@ void SysLog::log(
const MessageType type,
const char * message)
{
log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::Priority::PriorityLevel level = get_priority_level(type);
istringstream smessage;
string line;
smessage.str(message);
while ( getline(smessage, line) )
if( type <= log_level)
{
root << level << "[Z"<< zone_id<< "]"
<< "[" << module << "]"
<< "[" << error_names[type] << "]: "
<< line;
istringstream smessage(message);
string line;
int slevel = level(type);
while ( getline(smessage, line) )
{
ostringstream oss;
oss << "[Z"<< zone_id<< "]"
<< "[" << module << "]"
<< "[" << error_names[type] << "]: "
<< resource_label << line;
syslog(slevel, oss.str().c_str());
}
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
log4cpp::Priority::PriorityLevel SysLog::get_priority_level(
const MessageType level)
{
log4cpp::Priority::PriorityLevel priority_level;
switch (level)
{
case Log::ERROR:
priority_level = log4cpp::Priority::ERROR;
break;
case Log::WARNING:
priority_level = log4cpp::Priority::WARN;
break;
case Log::INFO:
priority_level = log4cpp::Priority::INFO;
break;
case Log::DEBUG:
case Log::DDEBUG:
case Log::DDDEBUG:
priority_level = log4cpp::Priority::DEBUG;
break;
default:
priority_level = log4cpp::Priority::NOTSET;
break;
}
return priority_level;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
const char * SysLogResource::CATEGORY = "RESOURCE";
/* -------------------------------------------------------------------------- */
SysLogResource::SysLogResource(
int oid,
const PoolObjectSQL::ObjectType obj_type,
const MessageType clevel):SysLog(clevel)
{
static bool initialized = false;
ostringstream oss_label;
string obj_type_str;
if (!initialized)
{
log4cpp::Appender *appender;
appender = new log4cpp::SyslogAppender(CATEGORY,
SysLog::LABEL,
LOG_DAEMON);
appender->setLayout(new log4cpp::PatternLayout());
log4cpp::Category& res = log4cpp::Category::getInstance(CATEGORY);
res.addAppender(appender);
res.setPriority(SysLog::get_priority_level(clevel));
initialized = true;
}
obj_type_str = PoolObjectSQL::type_to_str(obj_type);
oss_label << "[" << obj_type_str << " " << oid << "]";
obj_label = oss_label.str();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void SysLogResource::log(
const char * module,
const MessageType type,
const char * message)
{
log4cpp::Category& res = log4cpp::Category::getInstance(CATEGORY);
log4cpp::Priority::PriorityLevel level = get_priority_level(type);
istringstream smessage;
string line;
smessage.str(message);
while ( getline(smessage, line) )
{
res << level << obj_label
<< "[Z" << zone_id << "]"
<< "[" << module << "]"
<< "[" << error_names[type] << "]: "
<< line;
}
}
#endif /* SYSLOG_LOG */

View File

@ -21,6 +21,7 @@
/* -------------------------------------------------------------------------- */
Log * NebulaLog::logger;
NebulaLog::LogType NebulaLog::_log_type;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -381,14 +381,22 @@ void Nebula::start(bool bootstrap_only)
fd = open("/dev/null", O_RDWR);
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
if (NebulaLog::log_type() != NebulaLog::STD )
{
dup2(fd,1);
dup2(fd,2);
}
close(fd);
fcntl(0,F_SETFD,0); // Keep them open across exec funcs
fcntl(1,F_SETFD,0);
fcntl(2,F_SETFD,0);
if (NebulaLog::log_type() != NebulaLog::STD )
{
fcntl(1,F_SETFD,0);
fcntl(2,F_SETFD,0);
}
// -----------------------------------------------------------
// Block all signals before creating any Nebula thread

View File

@ -246,11 +246,11 @@ int VirtualMachine::select(SqlDB * db)
break;
case NebulaLog::SYSLOG:
_log = new SysLogResource(oid, obj_type, clevel);
_log = new SysLog(clevel, oid, obj_type);
break;
case NebulaLog::CERR:
_log = new CerrLog(clevel);
case NebulaLog::STD:
_log = new StdLog(clevel);
break;
default: