-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.h
More file actions
79 lines (64 loc) · 2.17 KB
/
LogFile.h
File metadata and controls
79 lines (64 loc) · 2.17 KB
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
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#ifndef LOGFILE_H
#define LOGFILE_H
#include <sstream>
#include <map>
#include <list>
#include <atomic>
#include <mutex>
using namespace std;
class CLogFile
{
public:
enum class LOGTYPES : uint32_t
{
END = 1, PUTTIME = 2
};
static CLogFile& GetInstance(const wstring& strLogfileName);
CLogFile(const CLogFile& src);
virtual ~CLogFile();
CLogFile& operator << (const LOGTYPES lt);
CLogFile& operator << (const uint64_t nSize);
CLogFile& operator << (const uint32_t nSize);
CLogFile& operator << (const string& strItem);
CLogFile& operator << (const char* const strItem);
CLogFile& WriteToLog();
template<typename ...Args>
CLogFile& WriteToLog(LOGTYPES value, const Args&... rest)
{
if (m_strFileName.empty() == false)
(*this) << value;
return WriteToLog(rest...);
}
template<typename T, typename ...Args>
CLogFile& WriteToLog(const T& value, const Args&... rest)
{
if (m_strFileName.empty() == false)
m_ssMsg << value;
return WriteToLog(rest...);
}
static void SetDontLog(bool bDontLog = true) noexcept;
private:
CLogFile() = delete;
CLogFile(CLogFile&&) = delete;
explicit CLogFile(const wstring& strLogfileName) : m_strFileName(strLogfileName), m_atThrRunning(false) {}
CLogFile& operator=(CLogFile&&) = delete;
CLogFile& operator=(const CLogFile&) = delete;
void StartWriteThread(const string& szMessage);
private:
wstring m_strFileName;
list<string> m_lstMessages;
mutex m_mtxBacklog;
atomic<bool> m_atThrRunning;
thread_local static stringstream m_ssMsg;
thread_local static bool s_bDontLog;
static map<wstring, CLogFile> s_lstLogFiles;
};
#endif // !LOGFILE_H