Online Book Reader

Home Category

Professional C__ - Marc Gregoire [485]

By Root 1223 0

#include

#include

#include

// Definition of a multithread safe singleton logger class

class Logger

{

public:

static const std::string kLogLevelDebug;

static const std::string kLogLevelInfo;

static const std::string kLogLevelError;

// Returns a reference to the singleton Logger object

static Logger& instance();

// Logs a single message at the given log level

void log(const std::string& inMessage,

const std::string& inLogLevel);

// Logs a vector of messages at the given log level

void log(const std::vector& inMessages,

const std::string& inLogLevel);

protected:

// Static variable for the one-and-only instance

static Logger* pInstance;

// Constant for the filename

static const char* const kLogFileName;

// Data member for the output stream

std::ofstream mOutputStream;

// Embedded class to make sure the single Logger

// instance gets deleted on program shutdown.

friend class Cleanup;

class Cleanup

{

public:

~Cleanup();

};

// Logs message. The thread should own a lock on sMutex

// before calling this function.

void logHelper(const std::string& inMessage,

const std::string& inLogLevel);

private:

Logger();

virtual ~Logger();

Logger(const Logger&);

Logger& operator=(const Logger&);

static std::mutex sMutex;

};

Code snippet from ThreadSafeLogger\Logger.h

The new implementation is as follows:

#include

#include "Logger.h"

using namespace std;

const string Logger::kLogLevelDebug = "DEBUG";

const string Logger::kLogLevelInfo = "INFO";

const string Logger::kLogLevelError = "ERROR";

const char* const Logger::kLogFileName = "log.out";

Logger* Logger::pInstance = nullptr;

mutex Logger::sMutex;

Logger& Logger::instance()

{

static Cleanup cleanup;

lock_guard guard(sMutex);

if (pInstance == nullptr)

pInstance = new Logger();

return *pInstance;

}

Logger::Cleanup::~Cleanup()

{

lock_guard guard(Logger::sMutex);

delete Logger::pInstance;

Logger::pInstance = nullptr;

}

Logger::~Logger()

{

mOutputStream.close();

}

Logger::Logger()

{

mOutputStream.open(kLogFileName, ios_base::app);

if (!mOutputStream.good()) {

throw runtime_error("Unable to initialize the Logger!");

}

}

void Logger::log(const string& inMessage, const string& inLogLevel)

{

lock_guard guard(sMutex);

logHelper(inMessage, inLogLevel);

}

void Logger::log(const vector& inMessages, const string& inLogLevel)

{

lock_guard guard(sMutex);

for (size_t i = 0; i < inMessages.size(); i++) {

logHelper(inMessages[i], inLogLevel);

}

}

void Logger::logHelper(const std::string& inMessage,

const std::string& inLogLevel)

{

mOutputStream << inLogLevel << ": " << inMessage << endl;

}

Code snippet from ThreadSafeLogger\Logger.cpp

The Cleanup class is there to make sure the single Logger instance gets deleted properly on program shutdown. This is necessary because this implementation is dynamically allocating the Logger instance by using the new operator in a block of code protected with a mutex. A static instance of the Cleanup class will be created the first time the instance() method is called. When the program terminates, the C++ runtime will destroy this static Cleanup instance, which will trigger the deletion of the Logger object and a call to the Logger destructor to close the file.

This version uses the C++11 threading library for its lock_guard and mutex classes, discussed in Chapter 22. If you do not have support for this C++11 threading library, you can replace those with any other kind of lock_guard and mutex available on your platform.

THE FACTORY PATTERN


A factory in real life constructs tangible objects, such as tables or cars. Similarly, a factory in object-oriented programming constructs objects. When you use factories in your program, portions of code that want to create a particular object ask the factory for an instance of the object instead of calling the object constructor themselves. For example, an interior decorating program might have a FurnitureFactory object. When part of the code

Return Main Page Previous Page Next Page

®Online Book Reader