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

The constructor of each C++ CASTOR daemon now takes a reference to Log object

as a parameter.  The functions that create the CASTOR daemon objects all
create a castor::log::LogImplementation object and pass it to the constructor
of their corresponding CASTOR daemon object.  When unit testting, one now has
the posibility to develop a dummy Log object that implements the interface
with dummy or mock routines so that the unit tests can run without having to
write to syslog or rsyslog which is a bit heavy for unit tests.  The
castor::log::LogImplementation basically contains the logging code centered
around syslog that was developed by Sebastien and Dennis.

Please note that I now have to add one or more log functions to the base
class of the CASTOR daemon objects, namely castor::server::BaseServer.
parent ec5df148
......@@ -30,6 +30,7 @@
#include "castor/gc/GcDaemon.hpp"
#include "castor/gc/DeletionThread.hpp"
#include "castor/gc/SynchronizationThread.hpp"
#include "castor/log/LogImplementation.hpp"
#include "castor/server/SignalThreadPool.hpp"
#include "castor/server/BaseThreadPool.hpp"
#include "Cgetopt.h"
......@@ -44,7 +45,8 @@
int main(int argc, char *argv[]) {
try {
castor::gc::GcDaemon daemon;
castor::log::LogImplementation log("gcd");
castor::gc::GcDaemon daemon(log);
// Randomize the start-up of the daemon between 1 and 15 minutes.
char *value;
......@@ -108,7 +110,8 @@ int main(int argc, char *argv[]) {
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
castor::gc::GcDaemon::GcDaemon(): castor::server::BaseDaemon("gcd") {
castor::gc::GcDaemon::GcDaemon(log::Log &log):
castor::server::BaseDaemon(log) {
// Now with predefined messages
castor::dlf::Message messages[] = {
......
......@@ -45,9 +45,11 @@ namespace castor {
public:
/**
* Default constructor
* Constructor
*
* @param log Object representing the API of the CASTOR logging system.
*/
GcDaemon();
GcDaemon(log::Log &log);
/**
* Default destructor
......
......@@ -24,6 +24,21 @@
#include "castor/log/Log.hpp"
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Log::Log(const std::string &programName)
throw(castor::exception::Internal, castor::exception::InvalidArgument):
m_programName(programName) {
}
//------------------------------------------------------------------------------
// getProgramName
//------------------------------------------------------------------------------
const std::string &castor::log::Log::getProgramName() const throw() {
return m_programName;
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
......
......@@ -45,6 +45,21 @@ namespace log {
class Log {
public:
/**
* Constructor
*
* @param programName The name of the program to be prepended to every log
* message.
*/
Log(const std::string &programName)
throw(castor::exception::Internal, castor::exception::InvalidArgument);
/**
* Returns the name of the program that is to be prepended to every log
* message.
*/
const std::string &getProgramName() const throw();
/**
* Destructor.
*/
......@@ -143,6 +158,14 @@ public:
virtual void writeMsg(
const int priority,
const std::string &msg) throw() = 0;
protected:
/**
* The name of the program to be prepended to every log message.
*/
const std::string m_programName;
}; // class Log
} // namespace log
......
......@@ -43,7 +43,7 @@
castor::log::LogImplementation::LogImplementation(
const std::string &programName)
throw(castor::exception::Internal, castor::exception::InvalidArgument):
m_programName(programName),
Log(programName),
m_maxMsgLen(determineMaxMsgLen()),
m_logFile(-1),
m_connected(false),
......
......@@ -138,11 +138,6 @@ private:
*/
static const size_t LOG_MAX_LINELEN = 8192;
/**
* The name of the program to be prepended to every log message.
*/
const std::string m_programName;
/**
* The maximum message length that the client syslog server can handle.
*/
......
......@@ -27,6 +27,7 @@
#include "castor/exception/Internal.hpp"
#include "castor/server/TCPListenerThreadPool.hpp"
#include "castor/server/AuthListenerThreadPool.hpp"
#include "castor/log/LogImplementation.hpp"
#include "castor/metrics/MetricsCollector.hpp"
#include "castor/metrics/ObjTypeCounter.hpp"
#include "castor/rh/UserCounter.hpp"
......@@ -61,7 +62,8 @@ const char *castor::rh::PORT_SEC_CONF = "SEC_PORT";
//------------------------------------------------------------------------------
int main(int argc, char *argv[]) {
try {
castor::rh::Server server;
castor::log::LogImplementation log("rhd");
castor::rh::Server server(log);
// parse the command line
server.parseCommandLine(argc, argv);
......@@ -91,8 +93,8 @@ int main(int argc, char *argv[]) {
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
castor::rh::Server::Server() :
castor::server::BaseDaemon("rhd"),
castor::rh::Server::Server(castor::log::Log &log) :
castor::server::BaseDaemon(log),
m_port(-1),
m_secure(false),
m_waitIfBusy(true),
......
......@@ -58,8 +58,10 @@ namespace castor {
/**
* Constructor
*
* @param log Object representing the API of the CASTOR logging system.
*/
Server();
Server(log::Log &log);
/**
* Destructor
......
......@@ -44,8 +44,8 @@
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::server::BaseDaemon::BaseDaemon(const std::string serverName) :
castor::server::BaseServer(serverName),
castor::server::BaseDaemon::BaseDaemon(log::Log &log) :
castor::server::BaseServer(log),
m_signalMutex(0) {
memset(&m_signalSet, 0, sizeof(m_signalSet));
}
......
......@@ -32,6 +32,7 @@
#include <map>
#include "castor/exception/Exception.hpp"
#include "castor/BaseObject.hpp"
#include "castor/log/Log.hpp"
#include "castor/server/BaseServer.hpp"
#include "castor/server/Mutex.hpp"
......@@ -58,9 +59,9 @@ namespace castor {
/**
* Constructor
* @param serverName as in BaseServer
* @param log Object representing the API of the CASTOR logging system.
*/
BaseDaemon(const std::string serverName);
BaseDaemon(log::Log &log);
/**
* Destructor
......
......@@ -51,10 +51,10 @@
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
castor::server::BaseServer::BaseServer(const std::string serverName) :
castor::server::BaseServer::BaseServer(log::Log &log) :
m_foreground(false),
m_runAsStagerSuperuser(false),
m_serverName(serverName)
m_log(log)
{
m_cmdLineParams.clear();
m_cmdLineParams << "fc:mh";
......@@ -126,7 +126,7 @@ void castor::server::BaseServer::init() throw (castor::exception::Exception)
void castor::server::BaseServer::dlfInit(castor::dlf::Message messages[])
throw (castor::exception::Exception)
{
castor::dlf::dlf_init((char*)m_serverName.c_str(), messages);
castor::dlf::dlf_init((char*)m_log.getProgramName().c_str(), messages);
// Add framework specific messages
castor::dlf::Message frameworkMessages[] =
{{ 1, "Error while reading datagrams" },
......@@ -158,7 +158,7 @@ void castor::server::BaseServer::dlfInit(castor::dlf::Message messages[])
void castor::server::BaseServer::start() throw (castor::exception::Exception)
{
if (m_foreground) {
std::cout << "Starting " << m_serverName << std::endl;
std::cout << "Starting " << m_log.getProgramName() << std::endl;
}
std::map<const char, castor::server::BaseThreadPool*>::const_iterator tp;
......
......@@ -34,6 +34,7 @@
#include "castor/BaseObject.hpp"
#include "castor/server/BaseThreadPool.hpp"
#include "castor/dlf/Message.hpp"
#include "castor/log/Log.hpp"
namespace castor {
......@@ -49,10 +50,9 @@ namespace castor {
/**
* Constructor
* @param serverName a friendly name for this daemon,
* used to initialize the logging facility.
* @param log Object representing the API of the CASTOR logging system.
*/
BaseServer(const std::string serverName);
BaseServer(log::Log &log);
/*
* Destructor
......@@ -114,8 +114,8 @@ namespace castor {
};
/// Returns this server's name
std::string& getServerName() {
return m_serverName;
const std::string &getServerName() {
return m_log.getProgramName();
}
/**
......@@ -155,9 +155,9 @@ namespace castor {
bool m_runAsStagerSuperuser;
/**
* Name of the server, for logging purposes.
* Object representing the API of the CASTOR logging system.
*/
std::string m_serverName;
log::Log &m_log;
/**
* Command line parameters. Includes by default a parameter
......
......@@ -35,6 +35,7 @@
#include "castor/dlf/Message.hpp"
#include "castor/exception/Exception.hpp"
#include "castor/exception/InvalidArgument.hpp"
#include "castor/log/LogImplementation.hpp"
#include "castor/PortNumbers.hpp"
#include "castor/System.hpp"
#include "castor/server/SignalThreadPool.hpp"
......@@ -58,7 +59,8 @@
int main(int argc, char* argv[]){
try{
castor::stager::daemon::StagerDaemon stagerDaemon;
castor::log::LogImplementation log("stagerd");
castor::stager::daemon::StagerDaemon stagerDaemon(log);
castor::stager::IStagerSvc* stgService =
dynamic_cast<castor::stager::IStagerSvc*>
......@@ -155,9 +157,9 @@ int main(int argc, char* argv[]){
/******************************************************************************************/
/* constructor: initiallizes the DLF logging and set the default value to its attributes */
/****************************************************************************************/
castor::stager::daemon::StagerDaemon::StagerDaemon()
castor::stager::daemon::StagerDaemon::StagerDaemon(log::Log &log)
throw (castor::exception::Exception)
: castor::server::BaseDaemon("stagerd") {
: castor::server::BaseDaemon(log) {
castor::dlf::Message stagerDlfMessages[]={
......
......@@ -46,9 +46,17 @@ namespace castor{
class StagerDaemon : public castor::server::BaseDaemon {
public:
/*** constructor ***/
StagerDaemon() throw (castor::exception::Exception);
/*** destructor ***/
/**
* Constructor
*
* @param log Object representing the API of the CASTOR logging system.
*/
StagerDaemon(log::Log &log) throw (castor::exception::Exception);
/**
* Destructor
*/
virtual ~StagerDaemon() throw() {};
void help(std::string programName);
......
......@@ -45,9 +45,9 @@
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::tape::tapebridge::TapeBridgeDaemon::TapeBridgeDaemon()
castor::tape::tapebridge::TapeBridgeDaemon::TapeBridgeDaemon(log::Log &log)
throw(castor::exception::Exception) :
castor::server::BaseDaemon("tapebridged"),
castor::server::BaseDaemon(log),
m_vdqmRequestHandlerThreadPool(0) {
}
......
......@@ -53,8 +53,10 @@ public:
/**
* Constructor.
*
* @param log Object representing the API of the CASTOR logging system.
*/
TapeBridgeDaemon() throw(castor::exception::Exception);
TapeBridgeDaemon(log::Log &log) throw(castor::exception::Exception);
/**
* Destructor.
......
......@@ -24,6 +24,7 @@
#include "castor/log/LogImplementation.hpp"
#include "castor/tape/tapebridge/TapeBridgeDaemon.hpp"
......@@ -32,7 +33,8 @@
//------------------------------------------------------------------------------
int main(int argc, char **argv) {
castor::tape::tapebridge::TapeBridgeDaemon daemon;
castor::log::LogImplementation log("tapebridged");
castor::tape::tapebridge::TapeBridgeDaemon daemon(log);
return daemon.main(argc, argv);
}
......@@ -164,9 +164,8 @@ int castor::tape::tapegateway::TapeGatewayDaemon::exceptionThrowingMain(int argc
//------------------------------------------------------------------------------
// TapeGatewayDaemon Constructor
//------------------------------------------------------------------------------
castor::tape::tapegateway::TapeGatewayDaemon::TapeGatewayDaemon() : castor::server::BaseDaemon("tapegatewayd")
{
castor::tape::tapegateway::TapeGatewayDaemon::TapeGatewayDaemon(log::Log &log):
castor::server::BaseDaemon(log) {
// get the port
char* tmp=NULL;
......
......@@ -45,32 +45,25 @@ namespace castor {
/**
* port to accept connections (environment)
*/
int m_listenPort;
/**
* DLF message strings.
*/
static castor::dlf::Message s_dlfMessages[];
/**
* Exception throwing main() function which basically implements the
* non-exception throwing main() function except for the initialisation of
* DLF and the "exception catch and log" logic.
*/
int exceptionThrowingMain(int argc,char **argv)
throw(castor::exception::Exception);
/**
* Logs the start of the daemon.
*/
void logStart(const int argc, const char *const *const argv) throw();
/**
* Writes the command-line usage message of the daemon to standard out.
......@@ -89,11 +82,11 @@ namespace castor {
public:
/**
* constructor
* Constructor
*
* @param log Object representing the API of the CASTOR logging system.
*/
TapeGatewayDaemon();
TapeGatewayDaemon(log::Log &log);
/**
* The main entry function of the mighunter daemon.
......@@ -104,7 +97,6 @@ namespace castor {
* @param argc Argument count from the executable's entry function: main().
* @param argv Argument vector from the executable's entry function: main().
*/
int main(const int argc, char **argv);
......@@ -116,7 +108,6 @@ namespace castor {
/** to access the value of the port which is used */
inline int listenPort(){ return m_listenPort; }
};
} //end of namespace tapegateway
......
......@@ -22,6 +22,7 @@
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "castor/log/LogImplementation.hpp"
#include "castor/tape/tapegateway/daemon/TapeGatewayDaemon.hpp"
#include "castor/exception/Exception.hpp"
......@@ -30,7 +31,8 @@
//------------------------------------------------------------------------------
int main(int argc, char **argv) {
try {
castor::tape::tapegateway::TapeGatewayDaemon daemon;
castor::log::LogImplementation log("tapegatewayd");
castor::tape::tapegateway::TapeGatewayDaemon daemon(log);
return daemon.main(argc, argv);
} catch (castor::exception::Exception e) {
std::cerr << "Caught exception :\n" << e.getMessage().str() << "\nExiting" << std::endl;
......
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