summaryrefslogtreecommitdiffstats
path: root/src/Logger.h
blob: e6c4460faf468330a0b6a32ddfeca6a8e7f02e7c (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#pragma once


class cLogger
{
public:

	enum eLogLevel
	{
		llRegular,
		llInfo,
		llWarning,
		llError,
	};


	class cListener
	{
		public:
		virtual void Log(AString a_Message, eLogLevel a_LogLevel) = 0;

		virtual ~cListener(){}
	};

	class cAttachment
	{
		public:

		cAttachment() : m_listener(nullptr) {}
		cAttachment(cAttachment && a_other)
			: m_listener(a_other.m_listener)
		{
			a_other.m_listener = nullptr;
		}

		~cAttachment()
		{
			if (m_listener != nullptr)
			{
				cLogger::GetInstance().DetachListener(m_listener);
			}
		}

		cAttachment & operator=(cAttachment && a_other)
		{
			m_listener = a_other.m_listener;
			a_other.m_listener = nullptr;
			return *this;
		}

		private:

		cListener * m_listener;

		friend class cLogger;

		cAttachment(cListener * a_listener) : m_listener(a_listener) {}
	};

	/** Log a message formatted with a printf style formatting string. */
	void vLogPrintf(const char * a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList);
	template <typename... Args>
	void LogPrintf(const char * a_Format, eLogLevel a_LogLevel, const Args & ... args)
	{
		vLogPrintf(a_Format, a_LogLevel, fmt::make_printf_args(args...));
	}

	/** Log a message formatted with a python style formatting string. */
	void vLogFormat(const char * a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList);
	template <typename... Args>
	void LogFormat(const char * a_Format, eLogLevel a_LogLevel, const Args & ... args)
	{
		vLogFormat(a_Format, a_LogLevel, fmt::make_format_args(args...));
	}

	/** Logs the simple text message at the specified log level. */
	void LogSimple(AString a_Message, eLogLevel a_LogLevel = llRegular);

	cAttachment AttachListener(std::unique_ptr<cListener> a_Listener);

	static cLogger & GetInstance(void);
	// Must be called before calling GetInstance in a multithreaded context
	static void InitiateMultithreading();
private:

	cCriticalSection m_CriticalSection;
	std::vector<std::unique_ptr<cListener>> m_LogListeners;

	void DetachListener(cListener * a_Listener);

};