Commit 61a540ae authored by Sebastien Ponce's avatar Sebastien Ponce
Browse files

New iteration of the logging facilities improvements

parent 0a06b54e
......@@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#)$RCSfile: BaseObject.cpp,v $ $Revision: 1.5 $ $Release$ $Date: 2004/07/07 16:01:07 $ $Author: sponcec3 $
* @(#)$RCSfile: BaseObject.cpp,v $ $Revision: 1.6 $ $Release$ $Date: 2004/07/08 08:26:33 $ $Author: sponcec3 $
*
*
*
......@@ -37,8 +37,7 @@
// -----------------------------------------------------------------------
// static values initialization
// -----------------------------------------------------------------------
castor::MsgSvc* castor::BaseObject::s_msgSvc(0);
int castor::BaseObject::s_lock(0);
std::string castor::BaseObject::s_msgSvcName("");
// -----------------------------------------------------------------------
// constructor
......@@ -55,8 +54,7 @@ castor::BaseObject::~BaseObject() throw() {}
// -----------------------------------------------------------------------
castor::MsgSvc* castor::BaseObject::msgSvc(std::string name)
throw (castor::exception::Exception) {
IService* svc = castor::Services::globalService
(name, castor::MsgSvc::ID());
IService* svc = svcs()->service(name, castor::MsgSvc::ID());
if (0 == svc) {
castor::exception::Internal e;
e.getMessage() << "Unable to retrieve MsgSvc";
......@@ -111,18 +109,12 @@ void castor::BaseObject::getTLS(void **thip)
}
//------------------------------------------------------------------------------
// initLog
// initlog
//------------------------------------------------------------------------------
void castor::BaseObject::initLog(std::string name)
throw(castor::exception::Exception) {
Cthread_mutex_lock(&s_lock);
if (0 != s_msgSvc) {
s_msgSvc->release();
s_msgSvc = 0;
}
// This always return some sensible value if no exception is raised
s_msgSvc = msgSvc(name);
Cthread_mutex_unlock(&s_lock);
void castor::BaseObject::initLog(std::string name) throw() {
Cthread_mutex_lock(&s_msgSvcName);
s_msgSvcName = name;
Cthread_mutex_unlock(&s_msgSvcName);
}
//------------------------------------------------------------------------------
......@@ -130,10 +122,5 @@ void castor::BaseObject::initLog(std::string name)
//------------------------------------------------------------------------------
castor::logstream& castor::BaseObject::clog()
throw(castor::exception::Exception) {
if (0 == s_msgSvc) {
castor::exception::Internal e;
e.getMessage() << "Please call initLog() before calling clog()";
throw e;
}
return s_msgSvc->stream();
return msgSvc(s_msgSvcName)->stream();
}
......@@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#)$RCSfile: BaseObject.hpp,v $ $Revision: 1.5 $ $Release$ $Date: 2004/07/07 16:01:07 $ $Author: sponcec3 $
* @(#)$RCSfile: BaseObject.hpp,v $ $Revision: 1.6 $ $Release$ $Date: 2004/07/08 08:26:34 $ $Author: sponcec3 $
*
* Basic object support, including pointer to Services and log support
*
......@@ -73,12 +73,14 @@ namespace castor {
* @exception Exception if something went wrong
*/
MsgSvc* msgSvc(std::string name) throw(castor::exception::Exception);
/**
* Initialization of to the log stream
* @param name the name of the log stream
* Sets the name of the MsgSvc to use for logging
* Note that this name should be set only once if DLF
* is used due to limitations in the current implementation
* of DLF itsef
*/
void initLog(std::string name) throw(castor::exception::Exception);
void initLog(std::string name) throw();
/**
* Access to the log stream
......@@ -95,15 +97,9 @@ namespace castor {
private:
/**
* The message service to be used for logging
* This is shared among instances
*/
static castor::MsgSvc* s_msgSvc;
/**
* a lock to synchronize the s_msgSvc creation/deletion
* The name of the MsgSvc to use
*/
static int s_lock;
static std::string s_msgSvcName;
}; // end of class BaseObject
......
......@@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#)$RCSfile: logbuf.cpp,v $ $Revision: 1.3 $ $Release$ $Date: 2004/07/07 16:01:07 $ $Author: sponcec3 $
* @(#)$RCSfile: logbuf.cpp,v $ $Revision: 1.4 $ $Release$ $Date: 2004/07/08 08:26:34 $ $Author: sponcec3 $
*
*
*
......@@ -26,3 +26,10 @@
// Include Files
#include "castor/logbuf.h"
// -----------------------------------------------------------------------
// static values initialization
// -----------------------------------------------------------------------
bool castor::logbuf::s_dlfInitCalled(false);
int castor::logbuf::s_lock(0);
......@@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#)$RCSfile: logbuf.h,v $ $Revision: 1.2 $ $Release$ $Date: 2004/07/07 16:01:08 $ $Author: sponcec3 $
* @(#)$RCSfile: logbuf.h,v $ $Revision: 1.3 $ $Release$ $Date: 2004/07/08 08:26:35 $ $Author: sponcec3 $
*
*
*
......@@ -34,6 +34,7 @@
#include <string>
#include <serrno.h>
#include <iostream>
#include <Cthread_api.h>
namespace castor {
......@@ -64,11 +65,22 @@ namespace castor {
logbuf(std::string &name) :
std::stringbuf(std::ios_base::out),
m_curLevel(INFO) {
int e;
if ((e = dlf_init(name.c_str())) < 0) {
std::cerr << "Unable to initialize DLF :"
<< std::endl << sstrerror(e)
<< std::endl;
if (!s_dlfInitCalled) {
// If dlf_init not already called,
// try to call it. But thread safety
// needs a lock here and a double check
Cthread_mutex_lock(&s_lock);
// It could be that somebody was quicker
if (!s_dlfInitCalled) {
int e;
if ((e = dlf_init(name.c_str())) < 0) {
std::cerr << "Unable to initialize DLF :"
<< std::endl << sstrerror(e)
<< std::endl;
}
s_dlfInitCalled = true;
}
Cthread_mutex_unlock(&s_lock);
}
}
......@@ -149,6 +161,16 @@ namespace castor {
*/
logbuf::Level m_curLevel;
/**
* Whether dlf_init was already called or not
*/
static bool s_dlfInitCalled;
/**
* A lock to ensure a unique call to dlf_init
*/
static int s_lock;
};
......
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