Commit e3899294 authored by Steven Murray's avatar Steven Murray
Browse files

WIP

parent b988f2be
......@@ -28,11 +28,6 @@
namespace cta {
namespace log {
/**
* The default path of the socket to which the logging system should write.
*/
const std::string SOCKET_NAME = "/var/run/ctalog.sock";
const int EMERG = 0; // system is unusable
const int ALERT = 1; // action must be taken immediately
const int CRIT = 2; // critical conditions
......
......@@ -105,7 +105,7 @@ public:
* No further calls to operator() should be made after calling this
* method until the call to fork() has completed.
*/
virtual void prepareForFork() = 0;
virtual void prepareForFork() = 0;
/**
* Returns the name of the program that is to be prepended to every log
......@@ -130,7 +130,6 @@ public:
const std::string &msg,
const std::list<Param> &params = std::list<Param>());
/**
* Writes the header of a syslog message to teh specifed output stream.
*
......
......@@ -43,101 +43,20 @@
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::log::SyslogLogger::SyslogLogger(const std::string &socketName,
const std::string &programName, const int logMask):
Logger(programName, logMask),
m_socketName(_PATH_LOG),
m_logFile(-1) {
struct stat fileStatus;
bzero(&fileStatus, sizeof(fileStatus));
if(stat(m_socketName.c_str(), &fileStatus)) {
const std::string errMsg = cta::utils::errnoToString(errno);
cta::exception::Exception ex;
ex.getMessage() << "Failed to instantiate syslog logger: Failed to stat"
" socket \"" << m_socketName << "\"" ": " << errMsg;
throw ex;
}
cta::log::SyslogLogger::SyslogLogger(const std::string &programName, const int logMask):
Logger(programName, logMask) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::log::SyslogLogger::~SyslogLogger() {
closeLog();
}
//------------------------------------------------------------------------------
// prepareForFork
//------------------------------------------------------------------------------
void cta::log::SyslogLogger::prepareForFork() {
threading::MutexLocker lock(m_mutex);
closeLog();
}
//------------------------------------------------------------------------------
// openLog
//------------------------------------------------------------------------------
void cta::log::SyslogLogger::openLog() {
if(-1 != m_logFile) {
return;
}
{
struct sockaddr_un syslogAddr;
syslogAddr.sun_family = AF_UNIX;
strncpy(syslogAddr.sun_path, m_socketName.c_str(),
sizeof(syslogAddr.sun_path));
m_logFile = socket(AF_UNIX, SOCK_DGRAM, 0);
}
if(-1 == m_logFile) {
return;
}
if(-1 == fcntl(m_logFile, F_SETFD, FD_CLOEXEC)) {
close(m_logFile);
m_logFile = -1;
return;
}
{
struct sockaddr_un syslogAddr;
syslogAddr.sun_family = AF_UNIX;
strncpy(syslogAddr.sun_path, m_socketName.c_str(),
sizeof(syslogAddr.sun_path));
if(-1 == connect(m_logFile, (struct sockaddr *)&syslogAddr,
sizeof(syslogAddr))) {
close(m_logFile);
m_logFile = -1;
return;
}
}
#ifdef __APPLE__
{
// MAC has has no MSG_NOSIGNAL
// but >= 10.2 comes with SO_NOSIGPIPE
int set = 1;
if(0 != setsockopt(m_logFile, SOL_SOCKET, SO_NOSIGPIPE, &set,
sizeof(int))) {
close(m_logFile);
m_logFile = -1;
return;
}
}
#endif
}
//------------------------------------------------------------------------------
// closeLog
//------------------------------------------------------------------------------
void cta::log::SyslogLogger::closeLog() {
if(-1 == m_logFile) {
return;
}
close(m_logFile);
m_logFile = -1;
}
//-----------------------------------------------------------------------------
......@@ -147,30 +66,5 @@ void cta::log::SyslogLogger::reducedSyslog(const std::string& msg) {
// Truncate the log message if it exceeds the permitted maximum
std::string truncatedMsg = msg.substr(0, m_maxMsgLen);
// int send_flags = MSG_NOSIGNAL;
//
// // enter critical section
// threading::MutexLocker lock(m_mutex);
//
// // Try to connect if not already connected
// openLog();
//
// // If connected
// 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, 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, truncatedMsg.c_str(), truncatedMsg.length(), send_flags)) {
// closeLog();
// }
// }
// }
// }
syslog(LOG_LOCAL3|LOG_INFO, truncatedMsg.c_str());
}
......@@ -46,18 +46,16 @@ public:
/**
* Constructor
*
* @param socketName The socket to which the logging system should write.
* @param programName The name of the program to be prepended to every log
* message.
* @param logMask The log mask.
*/
SyslogLogger(const std::string &socketName, const std::string &programName,
const int logMask);
SyslogLogger(const std::string &programName, const int logMask);
/**
* Destructor.
*/
~SyslogLogger();
~SyslogLogger() override;
/**
* Prepares the logger object for a call to fork().
......@@ -65,41 +63,10 @@ public:
* No further calls to operator() should be made after calling this
* method until the call to fork() has completed.
*/
void prepareForFork() ;
void prepareForFork() override;
protected:
/**
* The socket to which the logging system should write.
*/
std::string m_socketName;
/**
* Mutex used to protect the critical section of the SyslogLogger
* object.
*/
threading::Mutex m_mutex;
/**
* The file descriptor of the socket used to send messages to syslog.
*/
int m_logFile;
/**
* Connects to syslog.
*
* Please note that this method must be called from within the critical
* section of the SyslogLogger object.
*
* If the connection with syslog is already open then this method does
* nothing.
*
* This method does not throw an exception if the connection cannot be made
* to syslog. In this case the internal state of the SyslogLogger
* object reflects the fact that no connection was made.
*/
void openLog();
/**
* A reduced version of syslog. This method is able to set the message
* timestamp. This is necessary when logging messages asynchronously of there
......@@ -107,18 +74,7 @@ protected:
*
* @param msg The message to be logged.
*/
void reducedSyslog(const std::string & msg);
/**
* Closes the connection to syslog.
*
* Please note that this method must be called from within the critical
* section of the SyslogLogger object.
*
* If the connection to syslog is already closed then this method does
* nothing.
*/
void closeLog();
void reducedSyslog(const std::string & msg) override;
}; // class SyslogLogger
......
......@@ -22,7 +22,6 @@
*****************************************************************************/
#include "common/log/SyslogLogger.hpp"
#include "common/log/TestingSyslogLogger.hpp"
#include <gtest/gtest.h>
#include <memory>
......@@ -32,8 +31,7 @@ namespace unitTests {
class cta_log_SyslogLoggerTest: public ::testing::Test {
public:
cta_log_SyslogLoggerTest()/*: m_log(cta::log::SOCKET_NAME, "unitttests",
cta::log::DEBUG)*/ {
cta_log_SyslogLoggerTest() {
}
protected:
......@@ -43,28 +41,8 @@ protected:
virtual void TearDown() {
}
//cta::log::TestingSyslogLogger m_log;
}; // class SyslogLoggerTest
//TEST_F(cta_log_SyslogLoggerTest, logMsgAndParamsList) {
// using namespace cta::log;
// std::list<Param> params;
// params.push_back(Param("testParam", "value of test param"));
//
// ASSERT_NO_THROW(
// m_log(
// LOG_INFO,
// "cta_log_SyslogLoggerTest logMsgAndParamsList",
// params));
//}
//
//TEST_F(cta_log_SyslogLoggerTest, logMsg) {
// using namespace cta::log;
//
// ASSERT_NO_THROW(
// m_log(LOG_INFO, "Calling logger without parameters or time stamp"));
//}
TEST_F(cta_log_SyslogLoggerTest, cleanStringWithoutReplacingUnderscores) {
const std::string s(" \t\t\n\n\"Hello there\tWorld\" \t\t\n\n");
const std::string cleaned = cta::log::Logger::cleanString(s, false);
......
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program 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 2
* of the License, or (at your option) any later version.
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* Interface to the CASTOR logging system
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#pragma once
#include "common/log/SyslogLogger.hpp"
namespace cta {
namespace log {
/**
* Class used to facilitate unit testing by making public one or more of the
* protected members of its super class.
*/
class TestingSyslogLogger: public SyslogLogger {
public:
/**
* Constructor
*
* @param socketName The socket to which the logging system should write.
* @param programName The name of the program to be prepended to every log
* message.
* @param logMask The log mask.
*/
TestingSyslogLogger(const std::string &socketName, const std::string &programName, const int logMask):
SyslogLogger(socketName, programName, logMask) {
}
}; // class TestingSyslogLogger
} // namespace log
} // namespace cta
......@@ -208,8 +208,7 @@ int main(const int argc, char **const argv) {
if (commandLine->logToStdout) {
logPtr.reset(new log::StdoutLogger("cta-taped"));
} else {
logPtr.reset(new log::SyslogLogger(log::SOCKET_NAME, "cta-taped",
log::DEBUG));
logPtr.reset(new log::SyslogLogger("cta-taped", log::DEBUG));
}
} catch(exception::Exception &ex) {
std::cerr <<
......
......@@ -263,7 +263,7 @@ XrdCtaFilesystem::XrdCtaFilesystem():
// Try to instantiate the logging system API
try {
m_log.reset(new log::SyslogLogger(log::SOCKET_NAME, "cta-frontend", log::DEBUG));
m_log.reset(new log::SyslogLogger("cta-frontend", log::DEBUG));
} catch(exception::Exception &ex) {
throw cta::exception::Exception(std::string("Failed to instantiate object representing CTA logging system: ")+ex.getMessage().str());
}
......
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