-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlog_module.cpp
More file actions
287 lines (252 loc) · 6.84 KB
/
Copy pathlog_module.cpp
File metadata and controls
287 lines (252 loc) · 6.84 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @file ldlidar_logger.cpp
* @author LDRobot (support@ldrobot.com)
* @brief
* @version 0.1
* @date 2022.08.10
* @note
* @copyright Copyright (c) 2022 SHENZHEN LDROBOT CO., LTD. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "log_module.h"
#include <time.h>
#include <string.h>
#ifndef LINUX
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
#else
#include <stdlib.h>
#endif
// Ubuntu 24.04 won't compile without this include:
#include <pthread.h>
//使用vswprintf会出现奔溃的情况如果,传入数据大于 VA_PARAMETER_MAX 就会出现崩溃
#define VA_PARAMETER_MAX (1024 * 2)
LogModule* LogModule::s_plog_module_ = NULL;
LogModule* LogModule::GetInstance(__in const char* filename, __in const char* funcname, __in int lineno,LogLevel level,ILogRealization*plog) {
if (s_plog_module_ == NULL) {
s_plog_module_ = new LogModule();
}
s_plog_module_->logInfo_.str_filename = filename;
s_plog_module_->logInfo_.str_funcname = funcname;
s_plog_module_->logInfo_.n_linenumber = lineno;
s_plog_module_->logInfo_.loglevel = level;
if (plog != NULL) {
s_plog_module_->p_realization_->free();
s_plog_module_->p_realization_ = plog;
}
return s_plog_module_;
}
LogModule* LogModule::GetInstance(LogLevel level, ILogRealization*plog) {
if (s_plog_module_ == NULL) {
s_plog_module_ = new LogModule();
}
s_plog_module_->logInfo_.loglevel = level;
if (plog != NULL) {
s_plog_module_->p_realization_->free();
s_plog_module_->p_realization_ = plog;
}
return s_plog_module_;
}
LogModule::LogModule() {
logInfo_.n_linenumber = -1;
logInfo_.str_filename = "";
logInfo_.str_funcname = "";
#ifndef LINUX
p_realization = new LogOutputString();
#else
p_realization_ = new LogPrint();
#endif
InitLock();
}
LogModule::~LogModule() {
RealseLock();
}
void LogModule::LogPrintInf(const char* format,...) {
Lock();
if (p_realization_) {
std::string str_temp;
// manufacture
str_temp.append("[LDS]");
//LogLevel
str_temp.append(GetLevelValue(logInfo_.loglevel));
switch (logInfo_.loglevel) {
case DEBUG_LEVEL: {
//时间戳 uint is seconds
char s_stamp[100] = {0};
uint64_t timestamp = GetCurrentLocalTimeStamp();
#ifdef __LP64__
snprintf(s_stamp, 100, "[%lu.%lu]", (timestamp/1000000000), (timestamp%1000000000));
#else
#ifdef _WIN64
snprintf(s_stamp, 100, "[%lu.%lu]", (timestamp/1000000000), (timestamp%1000000000));
#else
snprintf(s_stamp, 100, "[%llu.%llu]", (timestamp/1000000000), (timestamp%1000000000));
#endif
#endif
str_temp.append(s_stamp);
}
break;
default: {
//时间 [week month day hours:minutes:seconds year]
str_temp.append(GetFormatValue(GetCurrentTime()));
}
break;
}
//文件名称
str_temp.append(GetFormatValue(logInfo_.str_filename));
str_temp.append(GetFormatValue(logInfo_.str_funcname));
//行号
str_temp.append(GetFormatValue(logInfo_.n_linenumber));
va_list ptr;
va_start(ptr, format);
char c_value[VA_PARAMETER_MAX] = {0};
vsnprintf(c_value,sizeof(c_value),format,ptr);
va_end(ptr);
str_temp.append(GetFormatValue(c_value));
p_realization_->LogPrintInf(str_temp.c_str());
}
UnLock();
}
void LogModule::LogPrintNoLocationInf(const char* format,...) {
Lock();
if (p_realization_) {
std::string str_temp;
// manufacture
str_temp.append("[LDS]");
//LogLevel
str_temp.append(GetLevelValue(logInfo_.loglevel));
//时间戳 uint is seconds
char s_stamp[100] = {0};
uint64_t timestamp = GetCurrentLocalTimeStamp();
#ifdef __LP64__
snprintf(s_stamp, 100, "[%lu.%lu]", (timestamp/1000000000), (timestamp%1000000000));
#else
#ifdef _WIN64
snprintf(s_stamp, 100, "[%lu.%lu]", (timestamp/1000000000), (timestamp%1000000000));
#else
snprintf(s_stamp, 100, "[%llu.%llu]", (timestamp/1000000000), (timestamp%1000000000));
#endif
#endif
str_temp.append(s_stamp);
va_list ptr;
va_start(ptr, format);
char c_value[VA_PARAMETER_MAX] = {0};
vsnprintf(c_value,sizeof(c_value),format,ptr);
va_end(ptr);
str_temp.append(GetFormatValue(c_value));
p_realization_->LogPrintInf(str_temp.c_str());
}
UnLock();
}
void LogModule::InitLock() {
#ifndef LINUX
InitializeCriticalSection(&mutex_lock_);
#else
pthread_mutex_init(&mutex_lock_,NULL);
#endif
}
void LogModule::RealseLock() {
#ifndef LINUX
DeleteCriticalSection(&mutex_lock_);
#else
pthread_mutex_unlock(&mutex_lock_);
#endif
}
void LogModule::Lock() {
#ifndef LINUX
EnterCriticalSection(&mutex_lock_);
#else
pthread_mutex_lock(&mutex_lock_);
#endif
}
void LogModule::UnLock() {
#ifndef LINUX
LeaveCriticalSection(&mutex_lock_);
#else
pthread_mutex_unlock(&mutex_lock_);
#endif
}
std::string LogModule::GetCurrentTime() {
std::string curr_time;
//Current date/time based on current time
time_t now = time(0);
// Convert current time to string
curr_time.assign(ctime(&now));
// Last charactor of currentTime is "\n", so remove it
std::string current_time = curr_time.substr(0, curr_time.size()-1);
return current_time;
}
std::string LogModule::GetFormatValue(std::string str_value) {
std::string str_temp;
str_temp.append("[");
str_temp.append(str_value);
str_temp.append("]");
return str_temp;
}
std::string LogModule::GetFormatValue(int n_value) {
std::string str_temp;
str_temp.append("[");
char c_value[16];
sprintf(c_value,"%d",n_value);
str_temp.append(c_value);
str_temp.append("]");
return str_temp;
}
std::string LogModule::GetLevelValue(int level){
std::string tmp;
switch (level) {
case DEBUG_LEVEL:
tmp = "DEBUG";
break;
case WARNING_LEVEL:
tmp = "WARNING";
break;
case ERROR_LEVEL:
tmp = "ERROR";
break;
case INFO_LEVEL:
tmp = "INFO";
break;
default:
tmp = "UnKnown";
break;
}
std::string str_temp;
str_temp.append("[");
str_temp.append(tmp);
str_temp.append("]");
return str_temp;
}
void LogPrint::Initializion(const char* path) {
printf("%s", path);
return ;
}
void LogPrint::free(ILogRealization *plogger) {
LogPrint* pOutput = static_cast<LogPrint*>(plogger);
if (pOutput != NULL) {
delete pOutput;
}
}
void LogPrint::LogPrintInf(const char* str) {
#ifdef ENABLE_CONSOLE_LOG_DIS
printf("%s\r\n", str);
#endif
#ifdef ENABLE_LOG_WRITE_TO_FILE
FILE *fp = fopen(LOGFILEPATH,"a");
if(!fp) {
printf("%s open filed!\n", LOGFILEPATH);
return ;
}
printf_s(fp,str);
printf_s(fp,"\r\n");
fclose(fp);
#endif
}
/********************* (C) COPYRIGHT SHENZHEN LDROBOT CO., LTD *******END OF FILE ********/