LOGGER.h 1.55 KB
#ifndef LOGGER_H_
#define LOGGER_H_

// Logger.h
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <mutex>
#include <ctime>

// Definition of a multithread safe singleton logger class
class Logger
{
public:
	static const std::string kLogLevelHeader;
	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<std::string>& 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;

	// Data type for acquiring current time
	std::time_t tTimeRaw;
	std::tm*    tTimeNow;

	// 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;
};

#endif // LOGGER_H_