-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadedLogger.cpp
More file actions
136 lines (122 loc) · 2.99 KB
/
ThreadedLogger.cpp
File metadata and controls
136 lines (122 loc) · 2.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <ThreadedLogger.hpp>
namespace vp
{
/**
* @brief Constructor for ThreadedLogger
*/
ThreadedLogger::ThreadedLogger()
{
m_bStopThread = false;
m_lMaxQueueSize = 4096;
m_ulPollingSleepUs = 1000;
m_pstQueueData = nullptr;
m_atmUsedQueueCount.store(0);
m_atmSeqence.store(0);
}
/**
* @brief Destructor for ThreadedLogger
*/
ThreadedLogger::~ThreadedLogger()
{
}
/**
* @brief Set the maximum queue size
* @param lMaxQueueSize Maximum size of the queue
*/
auto ThreadedLogger::setMaxQueueSize(long lMaxQueueSize) -> void
{
m_lMaxQueueSize = lMaxQueueSize;
}
auto ThreadedLogger::setPollingSleepUs(unsigned long ulPollingSleepUs) -> void
{
m_ulPollingSleepUs = ulPollingSleepUs;
}
/**
* @brief Initialize the logger
* @return ret_t Return status
*/
auto ThreadedLogger::initialize() -> ret_t
{
if (ALogger::initialize() != ret_t::RET_SUCCESS) {
return ret_t::RET_FAILURE;
}
m_pstQueueData = new queue_data_t[m_lMaxQueueSize];
for (long lIndex = 0; lIndex < m_lMaxQueueSize; lIndex++) {
m_pstQueueData[lIndex].bLogEnabled = false;
}
m_logThread = std::thread(&ThreadedLogger::logThreadFunction, this);
return ret_t::RET_SUCCESS;
}
/**
* @brief Finalize the logger
* @return Return status
*/
auto ThreadedLogger::finalize() -> ret_t
{
m_bStopThread = true;
if (m_logThread.joinable()) {
m_logThread.join();
}
if (ALogger::finalize() != ret_t::RET_SUCCESS) {
return ret_t::RET_FAILURE;
}
return ret_t::RET_SUCCESS;
}
/**
* @brief Write the log message
* @param pzLogString Log message
* @param nLogLength Length of the log message
*/
auto ThreadedLogger::writeLog(const char *pzLogString, int nLogLength) -> ret_t
{
long lUsedQueueCount = m_atmUsedQueueCount.fetch_add(1);
if (lUsedQueueCount >= m_lMaxQueueSize) {
m_atmUsedQueueCount.fetch_sub(1);
return ret_t::RET_SKIP;
}
long lSequence = getNextSequence();
auto &stQueueData = m_pstQueueData[lSequence % m_lMaxQueueSize];
stQueueData.strLogString = std::string(pzLogString, nLogLength);
stQueueData.bLogEnabled = true;
return ret_t::RET_SUCCESS;
}
/**
* @brief Get the next sequence number
* @return Next sequence number
*/
auto ThreadedLogger::getNextSequence() -> long
{
return m_atmSeqence.fetch_add(1);
}
/**
* @brief Log thread function
*/
auto ThreadedLogger::logThreadFunction() -> void
{
long lSequence = 0;
while (true) {
long lCurrentSequence = m_atmSeqence.load();
if (lSequence >= lCurrentSequence) {
if (m_bStopThread) {
lCurrentSequence = m_atmSeqence.load();
if (lSequence >= lCurrentSequence) {
break;
}
}
if (m_ulPollingSleepUs > 0) {
std::this_thread::sleep_for(std::chrono::microseconds(m_ulPollingSleepUs));
}
continue;
}
for (; lSequence < lCurrentSequence; lSequence++) {
auto &stQueueData = m_pstQueueData[lSequence % m_lMaxQueueSize];
if (!stQueueData.bLogEnabled) {
break;
}
write(stQueueData.strLogString.c_str(), stQueueData.strLogString.length());
stQueueData.bLogEnabled = false;
m_atmUsedQueueCount.fetch_sub(1);
}
}
}
}