mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-06 12:58:18 +03:00
feature #1658: Moved init methods to constructors (static). Make use of log system enums for type cheking. Use the same LOG variables as oned for scheduler.
This commit is contained in:
parent
d3b790719d
commit
b924b8c81f
@ -150,26 +150,41 @@ public:
|
||||
class SysLog : public Log
|
||||
{
|
||||
public:
|
||||
SysLog(const MessageType level = WARNING):Log(level){};
|
||||
SysLog(const MessageType level,
|
||||
const string& label);
|
||||
|
||||
~SysLog() {};
|
||||
virtual ~SysLog() {};
|
||||
|
||||
virtual void log(
|
||||
const char * module,
|
||||
const MessageType type,
|
||||
const char * message);
|
||||
|
||||
static void init(
|
||||
Log::MessageType clevel,
|
||||
string name,
|
||||
string label);
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Send log messages to syslog per resource
|
||||
* Send log messages to syslog per resource. It requires a Root Syslog
|
||||
* to be initialized before using a SysLogResource
|
||||
*/
|
||||
class SysLogResource : public SysLog
|
||||
{
|
||||
@ -179,21 +194,23 @@ public:
|
||||
const PoolObjectSQL::ObjectType obj_type,
|
||||
const MessageType clevel);
|
||||
|
||||
~SysLogResource(){};
|
||||
virtual ~SysLogResource(){};
|
||||
|
||||
void log(
|
||||
const char * module,
|
||||
const MessageType type,
|
||||
const char * message);
|
||||
|
||||
static void init(
|
||||
Log::MessageType clevel,
|
||||
string name,
|
||||
string label);
|
||||
protected:
|
||||
/**
|
||||
* This is the resource category name used by any syslog resource
|
||||
* in the process
|
||||
*/
|
||||
static const char * CATEGORY;
|
||||
|
||||
static string name;
|
||||
|
||||
private:
|
||||
/**
|
||||
* The resource log label
|
||||
*/
|
||||
string obj_label;
|
||||
};
|
||||
|
||||
|
@ -204,13 +204,13 @@ public:
|
||||
|
||||
/**
|
||||
* Returns the value of LOG->SYSTEM in oned.conf file
|
||||
* @return the system, either "file" or "syslog"
|
||||
* @return the logging system CERR, FILE_TS or SYSLOG
|
||||
*/
|
||||
string get_log_system() const
|
||||
NebulaLog::LogType get_log_system() const
|
||||
{
|
||||
vector<const Attribute *> logs;
|
||||
int rc;
|
||||
string log_system = "file";
|
||||
vector<const Attribute *> logs;
|
||||
int rc;
|
||||
NebulaLog::LogType log_system = NebulaLog::UNDEFINED;
|
||||
|
||||
rc = nebula_configuration->get("LOG", logs);
|
||||
|
||||
@ -219,11 +219,13 @@ public:
|
||||
string value;
|
||||
const VectorAttribute * log = static_cast<const VectorAttribute *>
|
||||
(logs[0]);
|
||||
log_system = log->vector_value("SYSTEM");
|
||||
|
||||
value = log->vector_value("SYSTEM");
|
||||
log_system = NebulaLog::str_to_type(value);
|
||||
}
|
||||
|
||||
return log_system;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the value of ONE_LOCATION env variable. When this variable is
|
||||
|
@ -20,17 +20,8 @@
|
||||
#include "Log.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#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/SimpleLayout.hh"
|
||||
//#include "log4cpp/Priority.hh"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
@ -43,7 +34,8 @@ public:
|
||||
FILE = 0,
|
||||
FILE_TS = 1,
|
||||
CERR = 2,
|
||||
SYSLOG = 3
|
||||
SYSLOG = 3,
|
||||
UNDEFINED = 4
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
@ -53,32 +45,46 @@ public:
|
||||
static void init_log_system(
|
||||
LogType ltype,
|
||||
Log::MessageType clevel,
|
||||
const char * filename = 0,
|
||||
ios_base::openmode mode = ios_base::trunc)
|
||||
const char * filename,
|
||||
ios_base::openmode mode,
|
||||
const string& daemon)
|
||||
{
|
||||
switch(ltype)
|
||||
{
|
||||
case FILE:
|
||||
NebulaLog::logger = new FileLog(filename,clevel,mode);
|
||||
break;
|
||||
NebulaLog::logger = new FileLog(filename,clevel,mode);
|
||||
break;
|
||||
case FILE_TS:
|
||||
NebulaLog::logger = new FileLogTS(filename,clevel,mode);
|
||||
break;
|
||||
NebulaLog::logger = new FileLogTS(filename,clevel,mode);
|
||||
break;
|
||||
case SYSLOG:
|
||||
NebulaLog::logger = new SysLog(clevel, daemon);
|
||||
break;
|
||||
default:
|
||||
NebulaLog::logger = new CerrLog(clevel);
|
||||
break;
|
||||
NebulaLog::logger = new CerrLog(clevel);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
static void init_syslog_system(Log::MessageType clevel, string label)
|
||||
static LogType str_to_type(string& type)
|
||||
{
|
||||
// Initialize the 'root' syslog logger
|
||||
SysLog::init(clevel, "root", label);
|
||||
NebulaLog::logger = new SysLog(clevel);
|
||||
transform(type.begin(), type.end(), type.begin(), (int(*)(int))toupper);
|
||||
|
||||
// Initialize the 'resource' syslog logger
|
||||
SysLogResource::init(clevel, "resource", label);
|
||||
};
|
||||
if (type == "FILE")
|
||||
{
|
||||
return FILE_TS;
|
||||
}
|
||||
else if (type == "SYSLOG")
|
||||
{
|
||||
return SYSLOG;
|
||||
}
|
||||
else if (type == "STDERR")
|
||||
{
|
||||
return CERR;
|
||||
}
|
||||
|
||||
return UNDEFINED;
|
||||
}
|
||||
|
||||
static void finalize_log_system()
|
||||
{
|
||||
|
@ -38,8 +38,10 @@
|
||||
# VMID
|
||||
#
|
||||
# LOG: Configuration for the logging system
|
||||
# SYSTEM: either "file" or "syslog".
|
||||
# DEBUG_LEVEL: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
|
||||
# system: defines the logging system:
|
||||
# file to log in the oned.log file
|
||||
# syslog to use the syslog facilities
|
||||
# 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.
|
||||
# Values: YES or NO.
|
||||
@ -47,8 +49,8 @@
|
||||
#
|
||||
|
||||
LOG = [
|
||||
# SYSTEM = "file",
|
||||
DEBUG_LEVEL = 3
|
||||
system = "file",
|
||||
debug_level = 3
|
||||
]
|
||||
|
||||
#MANAGER_TIMER = 30
|
||||
|
111
src/log/Log.cc
111
src/log/Log.cc
@ -158,23 +158,35 @@ void CerrLog::log(
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void SysLog::init(
|
||||
Log::MessageType clevel,
|
||||
string name,
|
||||
string _label)
|
||||
const char * SysLog::CATEGORY = "ROOT";
|
||||
string SysLog::LABEL;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
SysLog::SysLog(const MessageType level,
|
||||
const string& label):Log(level)
|
||||
{
|
||||
ostringstream oss_label;
|
||||
string label;
|
||||
static bool initialized = false;
|
||||
|
||||
oss_label << _label << "[" << getpid() << "]";
|
||||
label = oss_label.str();
|
||||
if (!initialized) //Initialize just once for all SysLog instances
|
||||
{
|
||||
ostringstream oss;
|
||||
log4cpp::Appender *appender;
|
||||
|
||||
log4cpp::Appender *syslog_appender;
|
||||
syslog_appender = new log4cpp::SyslogAppender(name,label,LOG_DAEMON);
|
||||
syslog_appender->setLayout(new log4cpp::PatternLayout());
|
||||
log4cpp::Category& root = log4cpp::Category::getRoot();
|
||||
root.setPriority(SysLog::get_priority_level(clevel));
|
||||
root.addAppender(syslog_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);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -185,15 +197,14 @@ void SysLog::log(
|
||||
const MessageType type,
|
||||
const char * message)
|
||||
{
|
||||
log4cpp::Category& root = log4cpp::Category::getRoot();
|
||||
log4cpp::Category& root = log4cpp::Category::getRoot();
|
||||
log4cpp::Priority::PriorityLevel level = get_priority_level(type);
|
||||
|
||||
istringstream smessage;
|
||||
string line;
|
||||
istringstream smessage;
|
||||
string line;
|
||||
|
||||
smessage.str(message);
|
||||
|
||||
|
||||
while ( getline(smessage, line) )
|
||||
{
|
||||
root << level << "[" << module << "]"
|
||||
@ -241,49 +252,42 @@ log4cpp::Priority::PriorityLevel SysLog::get_priority_level(
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
string SysLogResource::name;
|
||||
const char * SysLogResource::CATEGORY = "RESOURCE";
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
void SysLogResource::init(
|
||||
Log::MessageType clevel,
|
||||
string name,
|
||||
string _label)
|
||||
{
|
||||
SysLogResource::name = name;
|
||||
|
||||
ostringstream oss_label;
|
||||
string label;
|
||||
|
||||
oss_label << _label << "[" << getpid() << "]";
|
||||
label = oss_label.str();
|
||||
|
||||
log4cpp::Appender *resource_appender;
|
||||
resource_appender = new log4cpp::SyslogAppender(name,label,LOG_DAEMON);
|
||||
|
||||
resource_appender->setLayout(new log4cpp::PatternLayout());
|
||||
log4cpp::Category& res = log4cpp::Category::getInstance(name);
|
||||
res.addAppender(resource_appender);
|
||||
res.setPriority(SysLog::get_priority_level(clevel));
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
SysLogResource::SysLogResource(
|
||||
int oid,
|
||||
const PoolObjectSQL::ObjectType obj_type,
|
||||
const MessageType clevel = WARNING)
|
||||
:SysLog(clevel)
|
||||
int oid,
|
||||
const PoolObjectSQL::ObjectType obj_type,
|
||||
const MessageType clevel):SysLog(clevel)
|
||||
{
|
||||
static bool initialized = false;
|
||||
ostringstream oss_label;
|
||||
string obj_type_str = PoolObjectSQL::type_to_str(obj_type);
|
||||
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();
|
||||
};
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -293,8 +297,7 @@ void SysLogResource::log(
|
||||
const MessageType type,
|
||||
const char * message)
|
||||
{
|
||||
log4cpp::Category& res = log4cpp::Category::getInstance(
|
||||
SysLogResource::name);
|
||||
log4cpp::Category& res = log4cpp::Category::getInstance(CATEGORY);
|
||||
log4cpp::Priority::PriorityLevel level = get_priority_level(type);
|
||||
|
||||
istringstream smessage;
|
||||
|
@ -317,26 +317,23 @@ void Nebula::start()
|
||||
|
||||
try
|
||||
{
|
||||
Log::MessageType clevel;
|
||||
string log_system;
|
||||
Log::MessageType clevel;
|
||||
NebulaLog::LogType log_system;
|
||||
|
||||
log_system = get_log_system();
|
||||
clevel = get_debug_level();
|
||||
clevel = get_debug_level();
|
||||
|
||||
// Initializing ONE Daemon log system
|
||||
if ( log_system == "syslog" )
|
||||
{
|
||||
NebulaLog::init_syslog_system(clevel, "oned");
|
||||
}
|
||||
else if ( log_system == "file" )
|
||||
if ( log_system != NebulaLog::UNDEFINED )
|
||||
{
|
||||
string log_fname;
|
||||
log_fname = log_location + "oned.log";
|
||||
|
||||
NebulaLog::init_log_system(NebulaLog::FILE_TS,
|
||||
NebulaLog::init_log_system(log_system,
|
||||
clevel,
|
||||
log_fname.c_str(),
|
||||
ios_base::trunc);
|
||||
ios_base::trunc,
|
||||
"oned");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -178,6 +178,7 @@ void OpenNebulaTemplate::set_conf_default()
|
||||
vvalue.clear();
|
||||
vvalue.insert(make_pair("SYSTEM","file"));
|
||||
vvalue.insert(make_pair("DEBUG_LEVEL","3"));
|
||||
|
||||
vattribute = new VectorAttribute("LOG",vvalue);
|
||||
conf_default.insert(make_pair(vattribute->name(),vattribute));
|
||||
/*
|
||||
|
@ -35,7 +35,11 @@
|
||||
# - rank: Custom arithmetic exprission to rank suitable hosts based in their
|
||||
# attributes
|
||||
#
|
||||
# LOG_SYSTEM: choose the log system, either "file" or "syslog"
|
||||
# LOG: Configuration for the logging system
|
||||
# - system: defines the logging system:
|
||||
# file to log in the sched.log file
|
||||
# syslog to use the syslog facilities
|
||||
# - debug_level: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBU
|
||||
#*******************************************************************************
|
||||
|
||||
ONED_PORT = 2633
|
||||
@ -59,4 +63,7 @@ DEFAULT_SCHED = [
|
||||
# rank = "- (RUNNING_VMS * 50 + FREE_CPU)"
|
||||
#]
|
||||
|
||||
#LOG_SYSTEM = "file"
|
||||
LOG = [
|
||||
system = "file",
|
||||
debug_level = 3
|
||||
]
|
||||
|
@ -51,7 +51,6 @@ protected:
|
||||
acls(0),
|
||||
timer(0),
|
||||
url(""),
|
||||
log_system("file"),
|
||||
machines_limit(0),
|
||||
dispatch_limit(0),
|
||||
host_dispatch_limit(0),
|
||||
@ -126,7 +125,6 @@ private:
|
||||
|
||||
friend void * scheduler_action_loop(void *arg);
|
||||
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// Scheduling Policies
|
||||
// ---------------------------------------------------------------
|
||||
@ -166,11 +164,6 @@ private:
|
||||
*/
|
||||
Client * client;
|
||||
|
||||
/**
|
||||
* Log system
|
||||
*/
|
||||
string log_system;
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// Timer to periodically schedule and dispatch VMs
|
||||
// ---------------------------------------------------------------
|
||||
|
@ -125,31 +125,53 @@ void Scheduler::start()
|
||||
|
||||
conf.get("HYPERVISOR_MEM", hypervisor_mem);
|
||||
|
||||
conf.get("LOG_SYSTEM", log_system);
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Log system & Configuration File
|
||||
// -----------------------------------------------------------
|
||||
|
||||
try
|
||||
{
|
||||
// Start the log system
|
||||
if ( log_system == "syslog" )
|
||||
vector<const Attribute *> logs;
|
||||
int rc;
|
||||
|
||||
NebulaLog::LogType log_system = NebulaLog::UNDEFINED;
|
||||
Log::MessageType clevel = Log::ERROR;;
|
||||
|
||||
rc = conf.get("LOG", logs);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
NebulaLog::init_syslog_system(Log::DEBUG, "mm_sched");
|
||||
string value;
|
||||
int ilevel;
|
||||
|
||||
const VectorAttribute * log = static_cast<const VectorAttribute *>
|
||||
(logs[0]);
|
||||
value = log->vector_value("SYSTEM");
|
||||
log_system = NebulaLog::str_to_type(value);
|
||||
|
||||
value = log->vector_value("DEBUG_LEVEL");
|
||||
ilevel = atoi(value.c_str());
|
||||
|
||||
if (0 <= ilevel && ilevel <= 3 )
|
||||
{
|
||||
clevel = static_cast<Log::MessageType>(ilevel);
|
||||
}
|
||||
}
|
||||
else if ( log_system == "file" )
|
||||
|
||||
// Start the log system
|
||||
if ( log_system != NebulaLog::UNDEFINED )
|
||||
{
|
||||
NebulaLog::init_log_system(NebulaLog::FILE,
|
||||
Log::DEBUG,
|
||||
log_file.c_str());
|
||||
NebulaLog::init_log_system(log_system,
|
||||
clevel,
|
||||
log_file.c_str(),
|
||||
ios_base::trunc,
|
||||
"mm_sched");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw runtime_error("Unknown LOG_SYSTEM.");
|
||||
}
|
||||
|
||||
|
||||
NebulaLog::log("SCHED", Log::INFO, "Init Scheduler Log system");
|
||||
}
|
||||
catch(runtime_error &)
|
||||
@ -181,7 +203,6 @@ void Scheduler::start()
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
xmlInitParser();
|
||||
|
||||
// -----------------------------------------------------------
|
||||
|
@ -29,6 +29,7 @@ void SchedulerTemplate::set_conf_default()
|
||||
SingleAttribute * attribute;
|
||||
VectorAttribute * vattribute;
|
||||
string value;
|
||||
map<string,string> vvalue;
|
||||
|
||||
/*
|
||||
#*******************************************************************************
|
||||
@ -42,6 +43,7 @@ void SchedulerTemplate::set_conf_default()
|
||||
# DEFAULT_SCHED
|
||||
# LIVE_RESCHEDS
|
||||
# HYPERVISOR_MEM
|
||||
# LOG
|
||||
#-------------------------------------------------------------------------------
|
||||
*/
|
||||
// ONED_PORT
|
||||
@ -81,7 +83,7 @@ void SchedulerTemplate::set_conf_default()
|
||||
conf_default.insert(make_pair(attribute->name(),attribute));
|
||||
|
||||
//DEFAULT_SCHED
|
||||
map<string,string> vvalue;
|
||||
vvalue.clear();
|
||||
vvalue.insert(make_pair("POLICY","1"));
|
||||
|
||||
vattribute = new VectorAttribute("DEFAULT_SCHED",vvalue);
|
||||
@ -93,11 +95,13 @@ void SchedulerTemplate::set_conf_default()
|
||||
attribute = new SingleAttribute("HYPERVISOR_MEM",value);
|
||||
conf_default.insert(make_pair(attribute->name(),attribute));
|
||||
|
||||
//LOG_SYSTEM
|
||||
value = "file";
|
||||
//LOG CONFIGURATION
|
||||
vvalue.clear();
|
||||
vvalue.insert(make_pair("SYSTEM","file"));
|
||||
vvalue.insert(make_pair("DEBUG_LEVEL","3"));
|
||||
|
||||
attribute = new SingleAttribute("LOG_SYSTEM", value);
|
||||
conf_default.insert(make_pair(attribute->name(),attribute));
|
||||
vattribute = new VectorAttribute("LOG",vvalue);
|
||||
conf_default.insert(make_pair(vattribute->name(),vattribute));
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -191,25 +191,31 @@ int VirtualMachine::select(SqlDB * db)
|
||||
//--------------------------------------------------------------------------
|
||||
try
|
||||
{
|
||||
Log::MessageType clevel;
|
||||
string log_system;
|
||||
Log::MessageType clevel;
|
||||
NebulaLog::LogType log_system;
|
||||
|
||||
log_system = nd.get_log_system();
|
||||
clevel = nd.get_debug_level();
|
||||
|
||||
if ( log_system == "file" )
|
||||
switch(log_system)
|
||||
{
|
||||
_log = new FileLog(nd.get_vm_log_filename(oid), clevel);
|
||||
}
|
||||
else if ( log_system == "syslog" )
|
||||
{
|
||||
_log = new SysLogResource(oid, obj_type, clevel);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw('Unknown log system.');
|
||||
}
|
||||
case NebulaLog::FILE_TS:
|
||||
case NebulaLog::FILE:
|
||||
_log = new FileLog(nd.get_vm_log_filename(oid), clevel);
|
||||
break;
|
||||
|
||||
case NebulaLog::SYSLOG:
|
||||
_log = new SysLogResource(oid, obj_type, clevel);
|
||||
break;
|
||||
|
||||
case NebulaLog::CERR:
|
||||
_log = new CerrLog(clevel);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw runtime_error("Unknown log system.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(exception &e)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user