-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogix.cpp
86 lines (72 loc) · 1.77 KB
/
Logix.cpp
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
#include "Logix.h"
#include "logix/sinks/FileSink.h"
#include "logix/sinks/ConsoleSink.h"
#include "logix/AsyncLogger.h"
#include <iostream>
#include <algorithm>
namespace logix
{
inline std::shared_ptr<ThreadPool> Logix::threadPool()
{
std::lock_guard<std::recursive_mutex> lock(m_threadPoolMutex);
return m_threadPool;
}
inline void Logix::setThreadPool(std::shared_ptr<ThreadPool> threadPool)
{
std::lock_guard<std::recursive_mutex> lock(m_threadPoolMutex);
m_threadPool = std::move(threadPool);
}
inline std::shared_ptr<Logger> Logix::logger()
{
std::lock_guard<std::mutex> lock(m_loggerMutex);
return m_logger;
}
inline Logger* Logix::loggerPtr()
{
return m_logger.get();
}
inline void Logix::setLogger(std::shared_ptr<Logger> logger)
{
std::lock_guard<std::mutex> lock(m_loggerMutex);
m_logger = std::move(logger);
}
void Logix::finalize()
{
{
std::lock_guard<std::recursive_mutex> lock(m_threadPoolMutex);
m_threadPool.reset();
}
{
std::lock_guard<std::mutex> lock(m_loggerMutex);
m_logger.reset();
}
}
void defaultInitialization(bool async)
{
if (async)
createThreadPool();
auto fileSink = std::make_shared<FileSink>(std::string());
auto consoleSink = std::make_shared<ConsoleSink>();
SinkList sinks { fileSink, consoleSink };
auto logger = async ? std::make_shared<AsyncLogger>(Logix::instance().threadPool(), sinks) :
std::make_shared<Logger>(sinks);
Logix::instance().setLogger(logger);
}
void finalize()
{
return Logix::instance().finalize();
}
Logger* loggerPtr()
{
return Logix::instance().loggerPtr();
}
void log(const LogMessage& msg)
{
Logix::instance().logger()->log(msg);
}
LogWrapper::~LogWrapper()
{
m_logMessage.m_message = m_oss.str();
log(m_logMessage);
}
} // namespace logix