Skip to content

Commit

Permalink
log level
Browse files Browse the repository at this point in the history
  • Loading branch information
abudnik committed Jun 8, 2015
1 parent d7ba35a commit 890acf1
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 47 deletions.
3 changes: 2 additions & 1 deletion conf/master.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"max_simult_result_getters" : 100,
"max_simult_command_send" : 100,
"ipv6" : false,
"masterdb" : "localhost"
"masterdb" : "localhost",
"log_level" : "info"
}
3 changes: 2 additions & 1 deletion conf/worker.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"js" : "/usr/bin/node",
"pidfile" : "/var/run/pworker.pid",
"completion_ping_delay" : 1,
"ipv6" : false
"ipv6" : false,
"log_level" : "info"
}
3 changes: 2 additions & 1 deletion master.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"max_simult_result_getters" : 100,
"max_simult_command_send" : 100,
"ipv6" : false,
"masterdb" : "localhost"
"masterdb" : "localhost",
"log_level" : "debug"
}
3 changes: 2 additions & 1 deletion masterdb.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"pidfile" : "masterdb.pid",
"db_path" : "db",
"ipv6" : false
"ipv6" : false,
"log_level" : "debug"
}
42 changes: 32 additions & 10 deletions src/common/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,34 @@ namespace common {
namespace logger
{

bool isDaemon = false;
bool useSyslog = false;
const char *serviceName = "";
bool isTerminal = false;
LogLevel logLevel;


void InitLogger( bool isDaemon, const char *serviceName )
void InitLogger( bool useSyslog, const char *serviceName, const char *level )
{
logger::isDaemon = isDaemon;
logger::useSyslog = useSyslog;
logger::serviceName = serviceName;

if ( isDaemon )
if ( !strcasecmp( level, "debug" ) )
logLevel = LogLevel::DEBUG;
else
if ( !strcasecmp( level, "info" ) )
logLevel = LogLevel::INFO;
else
if ( !strcasecmp( level, "warning" ) )
logLevel = LogLevel::WARNING;
else
if ( !strcasecmp( level, "error" ) )
logLevel = LogLevel::ERROR;
else
{
std::cout << "InitLogger: unknown log level '" << level << "', using 'debug' by default" << std::endl;
logLevel = LogLevel::DEBUG;
}

if ( useSyslog )
{
openlog( serviceName, LOG_CONS, LOG_DAEMON );
}
Expand All @@ -53,7 +70,7 @@ void InitLogger( bool isDaemon, const char *serviceName )

void ShutdownLogger()
{
if ( isDaemon )
if ( useSyslog )
{
closelog();
}
Expand Down Expand Up @@ -87,7 +104,7 @@ void Print( char level, const char *msg )

void Log( const char *msg )
{
if ( isDaemon )
if ( useSyslog )
{
syslog( LOG_INFO, "%s", msg );
}
Expand All @@ -99,7 +116,7 @@ void Log( const char *msg )

void LogDebug( const char *msg )
{
if ( isDaemon )
if ( useSyslog )
{
syslog( LOG_DEBUG, "%s", msg );
}
Expand All @@ -111,7 +128,7 @@ void LogDebug( const char *msg )

void LogWarning( const char *msg )
{
if ( isDaemon )
if ( useSyslog )
{
syslog( LOG_WARNING, "%s", msg );
}
Expand All @@ -123,7 +140,7 @@ void LogWarning( const char *msg )

void LogError( const char *msg )
{
if ( isDaemon )
if ( useSyslog )
{
syslog( LOG_ERR, "%s", msg );
}
Expand All @@ -133,6 +150,11 @@ void LogError( const char *msg )
}
}

bool CheckLogLevel( LogLevel want )
{
return static_cast<int>(logLevel) <= static_cast<int>(want);
}

} // namespace logger

} // namespace common
44 changes: 27 additions & 17 deletions src/common/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,43 @@ the License.
os << MSG << " (from " << __FILE__ << ':' << __LINE__ << ')';

#define PLOG( MSG )\
{\
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::Log( os.str().c_str() ); \
}
if ( common::logger::CheckLogLevel( common::logger::LogLevel::INFO ) ) { \
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::Log( os.str().c_str() ); \
}

#define PLOG_DBG( MSG )\
{\
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::LogDebug( os.str().c_str() ); \
}
if ( common::logger::CheckLogLevel( common::logger::LogLevel::DEBUG ) ) { \
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::LogDebug( os.str().c_str() ); \
}

#define PLOG_WRN( MSG )\
{\
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::LogWarning( os.str().c_str() ); \
}
if ( common::logger::CheckLogLevel( common::logger::LogLevel::WARNING ) ) { \
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::LogWarning( os.str().c_str() ); \
}

#define PLOG_ERR( MSG )\
{\
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::LogError( os.str().c_str() ); \
}
if ( common::logger::CheckLogLevel( common::logger::LogLevel::ERROR ) ) { \
PLOG_PREPARE_MESSAGE( MSG ); \
common::logger::LogError( os.str().c_str() ); \
}

namespace common {

namespace logger
{

void InitLogger( bool isDaemon, const char *serviceName );
enum LogLevel
{
DEBUG = 0,
INFO = 1,
WARNING = 2,
ERROR = 3
};

void InitLogger( bool useSyslog, const char *serviceName, const char *level );

void ShutdownLogger();

Expand All @@ -70,6 +78,8 @@ void LogWarning( const char *msg );

void LogError( const char *msg );

bool CheckLogLevel( LogLevel want );

} // namespace logger

} // namespace common
Expand Down
9 changes: 5 additions & 4 deletions src/master/master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ class MasterApplication

void Initialize()
{
common::logger::InitLogger( isDaemon_, "pmaster" );

PLOG_DBG( "MasterApplication::Initialize: master_id=" << masterId_ );

SetupSignalHandlers();
atexit( AtExit );

Expand All @@ -199,6 +195,11 @@ class MasterApplication
}
ApplyDefaults( cfg );

std::string logLevel = cfg.Get<std::string>( "log_level" );
common::logger::InitLogger( isDaemon_, "pmaster", logLevel.c_str() );

PLOG_DBG( "MasterApplication::Initialize: master_id=" << masterId_ );

unsigned int numHeartbeatThread = 1;
unsigned int numPingReceiverThread = cfg.Get<unsigned int>( "num_ping_receiver_thread" );
unsigned int numJobSendThread = 1 + cfg.Get<unsigned int>( "num_job_send_thread" );
Expand Down
2 changes: 1 addition & 1 deletion src/master/worker_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ bool WorkerManager::GetAchievedTask( WorkerTask &worker, std::string &hostIP )
if ( achievedWorkers_.empty() )
return false;

PLOG( "GetAchievedWorker: num achieved workers=" << achievedWorkers_.size() );
PLOG_DBG( "GetAchievedWorker: num achieved workers=" << achievedWorkers_.size() );

const PairTypeAW &w = achievedWorkers_.front();
worker = w.first;
Expand Down
5 changes: 3 additions & 2 deletions src/masterdb/masterdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ class MasterDbApplication

void Initialize()
{
common::logger::InitLogger( isDaemon_, "pmasterdb" );

SetupSignalHandlers();
atexit( AtExit );

Expand All @@ -146,6 +144,9 @@ class MasterDbApplication
}
ApplyDefaults( cfg );

std::string logLevel = cfg.Get<std::string>( "log_level" );
common::logger::InitLogger( isDaemon_, "pmasterdb", logLevel.c_str() );

// initialize main components
dbClient_.Initialize( exeDir_ );

Expand Down
9 changes: 5 additions & 4 deletions src/worker/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,10 +1317,6 @@ class ExecApplication

void Initialize()
{
PLOG_DBG( "ExecApplication::Initialize" );

common::logger::InitLogger( isDaemon_, "prexec" );

common::Config &cfg = common::Config::Instance();
if ( cfgDir_.empty() )
{
Expand All @@ -1331,6 +1327,11 @@ class ExecApplication
cfg.ParseConfig( "", cfgDir_.c_str() );
}

std::string logLevel = cfg.Get<std::string>( "log_level" );
common::logger::InitLogger( isDaemon_, "prexec", logLevel.c_str() );

PLOG_DBG( "ExecApplication::Initialize" );

SetupLanguageRuntime( execContext_ );

SetupPrExecIPC();
Expand Down
9 changes: 5 additions & 4 deletions src/worker/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,15 +1010,11 @@ class WorkerApplication

void Initialize()
{
PLOG_DBG( "WorkerApplication::Initialize" );

ComputerInfo &compInfo = ComputerInfo::Instance();
unsigned int numExecThread = compInfo.GetNumCPU();
numRequestThread_ = 2 * numExecThread;
numThread_ = numRequestThread_ + 1; // +1 for accept thread

common::logger::InitLogger( isDaemon_, "pworker" );

common::Config &cfg = common::Config::Instance();
if ( cfgDir_.empty() )
{
Expand All @@ -1030,6 +1026,11 @@ class WorkerApplication
}
ApplyDefaults( cfg );

std::string logLevel = cfg.Get<std::string>( "log_level" );
common::logger::InitLogger( isDaemon_, "pworker", logLevel.c_str() );

PLOG_DBG( "WorkerApplication::Initialize" );

JobCompletionTable::Instance();

SetupSignalHandlers();
Expand Down
3 changes: 2 additions & 1 deletion worker.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"js" : "/usr/bin/node",
"pidfile" : "worker.pid",
"completion_ping_delay" : 1,
"ipv6" : false
"ipv6" : false,
"log_level" : "debug"
}

0 comments on commit 890acf1

Please sign in to comment.