2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-03 23:45:27 +00:00

[#1266] Initialize logger internal pointer only once

This commit is contained in:
Razvan Becheriu
2020-06-03 11:56:47 +03:00
parent 6faf4a3661
commit d00a7e90f8
3 changed files with 13 additions and 12 deletions

View File

@@ -16,6 +16,8 @@
#include <log/message_dictionary.h> #include <log/message_dictionary.h>
#include <log/message_types.h> #include <log/message_types.h>
#include <boost/make_shared.hpp>
#include <util/strutil.h> #include <util/strutil.h>
using namespace std; using namespace std;
@@ -23,7 +25,7 @@ using namespace std;
namespace isc { namespace isc {
namespace log { namespace log {
LoggerImpl* LoggerImplPtr
Logger::getLoggerPtr() { Logger::getLoggerPtr() {
if (!initialized_) { if (!initialized_) {
lock_guard<mutex> lk(mutex_); lock_guard<mutex> lk(mutex_);
@@ -39,7 +41,7 @@ Logger::getLoggerPtr() {
void void
Logger::initLoggerImpl() { Logger::initLoggerImpl() {
if (isLoggingInitialized()) { if (isLoggingInitialized()) {
loggerptr_ = new LoggerImpl(name_); loggerptr_ = boost::make_shared<LoggerImpl>(name_);
} else { } else {
isc_throw(LoggingNotInitialized, "attempt to access logging function " isc_throw(LoggingNotInitialized, "attempt to access logging function "
"before logging has been initialized"); "before logging has been initialized");
@@ -49,12 +51,6 @@ Logger::initLoggerImpl() {
// Destructor. // Destructor.
Logger::~Logger() { Logger::~Logger() {
delete loggerptr_;
// The next statement is required for the Kea hooks framework, where a
// statically-linked Kea loads and unloads multiple libraries. See the hooks
// documentation for more details.
loggerptr_ = 0;
} }
// Get Version // Get Version

View File

@@ -10,13 +10,14 @@
#include <atomic> #include <atomic>
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
#include <string>
#include <cstring> #include <cstring>
#include <mutex> #include <mutex>
#include <string>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <log/logger_impl.h>
#include <log/logger_level.h> #include <log/logger_level.h>
#include <log/message_types.h> #include <log/message_types.h>
#include <log/log_formatter.h> #include <log/log_formatter.h>
@@ -164,7 +165,7 @@ public:
/// \note Note also that there is no constructor taking a std::string. This /// \note Note also that there is no constructor taking a std::string. This
/// minimizes the possibility of initializing a static logger with a /// minimizes the possibility of initializing a static logger with a
/// string, so leading to problems mentioned above. /// string, so leading to problems mentioned above.
Logger(const char* name) : loggerptr_(NULL), initialized_(false) { Logger(const char* name) : loggerptr_(), initialized_(false) {
// Validate the name of the logger. // Validate the name of the logger.
if (name == NULL) { if (name == NULL) {
@@ -346,13 +347,13 @@ private:
/// cause a "LoggingNotInitialized" exception to be thrown. /// cause a "LoggingNotInitialized" exception to be thrown.
/// ///
/// \return Returns pointer to implementation /// \return Returns pointer to implementation
LoggerImpl* getLoggerPtr(); LoggerImplPtr getLoggerPtr();
/// \brief Initialize Underlying Implementation and Set loggerptr_ /// \brief Initialize Underlying Implementation and Set loggerptr_
void initLoggerImpl(); void initLoggerImpl();
///< Pointer to underlying logger ///< Pointer to underlying logger
LoggerImpl* loggerptr_; LoggerImplPtr loggerptr_;
///< Copy of the logger name ///< Copy of the logger name
char name_[MAX_LOGGER_NAME_SIZE + 1]; char name_[MAX_LOGGER_NAME_SIZE + 1];

View File

@@ -15,6 +15,8 @@
#include <string> #include <string>
#include <map> #include <map>
#include <utility> #include <utility>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
@@ -192,6 +194,8 @@ private:
isc::log::interprocess::InterprocessSync* sync_; isc::log::interprocess::InterprocessSync* sync_;
}; };
typedef boost::shared_ptr<LoggerImpl> LoggerImplPtr;
} // namespace log } // namespace log
} // namespace isc } // namespace isc