Blame view

GUI/SW1/SRC/LOGGER.h 1.55 KB
f309591f   Steve Greedy   Update to SW1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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_