Commit a084f7ff authored by Eric Cano's avatar Eric Cano
Browse files

Simplified interface for Logger::reducedSyslog()

parent e900b0b3
......@@ -44,5 +44,5 @@ void cta::log::DummyLogger::prepareForFork() {}
//------------------------------------------------------------------------------
// reducedSyslog
//------------------------------------------------------------------------------
void cta::log::DummyLogger::reducedSyslog(std::string msg) {}
void cta::log::DummyLogger::reducedSyslog(const std::string & msg) {}
......@@ -63,7 +63,7 @@ public:
void prepareForFork() ;
protected:
virtual void reducedSyslog(std::string msg);
virtual void reducedSyslog(const std::string & msg);
}; // class DummyLogger
......
......@@ -173,7 +173,7 @@ protected:
*
* @param msg The message to be logged.
*/
virtual void reducedSyslog(std::string msg) = 0;
virtual void reducedSyslog(const std::string & msg) = 0;
/**
* The log mask.
......
......@@ -39,6 +39,6 @@ void cta::log::StdoutLogger::prepareForFork() {}
//------------------------------------------------------------------------------
// reducedSyslog
//------------------------------------------------------------------------------
void cta::log::StdoutLogger::reducedSyslog(std::string msg) {
printf("%s", msg.c_str());
void cta::log::StdoutLogger::reducedSyslog(const std::string & msg) {
printf("%s", msg.substr(0,m_maxMsgLen).c_str());
}
......@@ -59,7 +59,7 @@ protected:
*
* @param msg The message to be logged.
*/
void reducedSyslog(std::string msg);
void reducedSyslog(const std::string & msg);
}; // class StringLogger
......
......@@ -47,19 +47,13 @@ void cta::log::StringLogger::prepareForFork() {
//-----------------------------------------------------------------------------
// reducedSyslog
//-----------------------------------------------------------------------------
void cta::log::StringLogger::reducedSyslog(std::string msg) {
// Truncate the log message if it exceeds the permitted maximum
if(msg.length() > m_maxMsgLen) {
msg.resize(m_maxMsgLen);
msg[msg.length() - 1] = '\n';
}
void cta::log::StringLogger::reducedSyslog(const std::string & msg) {
// enter critical section
threading::MutexLocker lock(m_mutex);
// Append the message to the log
m_log << msg << std::endl;
// Append the message to the log (truncated to the maximum length)
m_log << msg.substr(0, m_maxMsgLen) << std::endl;
// Temporary hack: also print them out:
printf (msg.c_str());
// Uncomment this to get the logs printed to stdout during unit tests.
// printf (msg.substr(0, m_maxMsgLen).c_str());
}
......@@ -83,7 +83,7 @@ protected:
*
* @param msg The message to be logged.
*/
void reducedSyslog(std::string msg);
void reducedSyslog(const std::string & msg);
}; // class StringLogger
......
......@@ -143,12 +143,9 @@ void cta::log::SyslogLogger::closeLog() {
//-----------------------------------------------------------------------------
// reducedSyslog
//-----------------------------------------------------------------------------
void cta::log::SyslogLogger::reducedSyslog(std::string msg) {
void cta::log::SyslogLogger::reducedSyslog(const std::string& msg) {
// Truncate the log message if it exceeds the permitted maximum
if(msg.length() > m_maxMsgLen) {
msg.resize(m_maxMsgLen);
msg[msg.length() - 1] = '\n';
}
std::string truncatedMsg = msg.substr(0, m_maxMsgLen);
int send_flags = MSG_NOSIGNAL;
......@@ -162,13 +159,13 @@ void cta::log::SyslogLogger::reducedSyslog(std::string msg) {
if(-1 != m_logFile) {
// If sending the log message fails then try to reopen the syslog
// connection and try again
if(0 > send(m_logFile, msg.c_str(), msg.length(), send_flags)) {
if(0 > send(m_logFile, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
closeLog();
openLog();
if (-1 != m_logFile) {
// If the second attempt to send the log message fails then give up and
// attempt re-open next time
if(0 > send(m_logFile, msg.c_str(), msg.length(), send_flags)) {
if(0 > send(m_logFile, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
closeLog();
}
}
......
......@@ -107,7 +107,7 @@ protected:
*
* @param msg The message to be logged.
*/
void reducedSyslog(std::string msg);
void reducedSyslog(const std::string & msg);
/**
* Closes the connection to syslog.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment