-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlog_module.cpp
More file actions
351 lines (308 loc) · 8.59 KB
/
Copy pathlog_module.cpp
File metadata and controls
351 lines (308 loc) · 8.59 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* @file log_module.cpp
* @author David Hu (hmd_hubei_cn@163.com)
* @brief
* @version 0.1
* @date 2022.08.10
* @note
* @copyright Copyright (c) 2022 DAVID HU 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 "ldlidar_driver/log_module.h"
#include <time.h>
#include <string.h>
#include <pthread.h>
#ifndef __linux__
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
#else
#include <stdlib.h>
#endif
/* 使用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::GetInstancePrintOriginData(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;
str_temp.append("[LOG]");
// Time [week month day hours:minutes:seconds year]
str_temp.append(GetFormatValue(GetCurrentISOTime()));
// Stamp uint is ns
str_temp.append(GetCurrentLocalTimeStamp());
// LogLevel
str_temp.append(GetLevelValue(logInfo_.loglevel));
// File name
str_temp.append(GetFormatValue(logInfo_.str_filename));
// Function name
str_temp.append(GetFormatValue(logInfo_.str_funcname));
// Line number
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("[LOG]");
// time [week month day hours:minutes:seconds year]
str_temp.append(GetFormatValue(GetCurrentISOTime()));
// stamp uint is ns
str_temp.append(GetCurrentLocalTimeStamp());
//LogLevel
str_temp.append(GetLevelValue(logInfo_.loglevel));
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::LogPrintOriginData(const char* format,...) {
Lock();
if (p_realization_) {
std::string str_temp;
switch (logInfo_.loglevel) {
case INFO_LEVEL: {
str_temp.append("[LOG]");
str_temp.append(GetFormatValue(GetCurrentISOTime()));
str_temp.append(GetCurrentLocalTimeStamp());
str_temp.append(GetLevelValue(logInfo_.loglevel));
break;
}
default: {
break;
}
}
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(c_value);
p_realization_->LogPrintData(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::GetCurrentISOTime() {
std::string curr_time;
#if 0
//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;
#else
char stdtime_str[50] = {0};
time_t std_time = 0;
struct tm* local_time = NULL;
std_time = time(NULL);
local_time = localtime(&std_time);
snprintf(stdtime_str, 50, "%d-%d-%d,%d:%d:%d",
local_time->tm_year+1900, local_time->tm_mon+1, local_time->tm_mday,
local_time->tm_hour, local_time->tm_min, local_time->tm_sec);
curr_time.assign(stdtime_str);
return curr_time;
#endif
}
std::string LogModule::GetCurrentLocalTimeStamp() {
std::string stamp_str;
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp =
std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now());
auto tmp = std::chrono::duration_cast<std::chrono::nanoseconds>(tp.time_since_epoch());
uint64_t timestamp = (uint64_t)tmp.count();
char s_stamp[100] = {0};
#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
stamp_str = s_stamp;
return stamp_str;
}
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 = "WARN";
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_DISPALY
printf("%s\r\n", str);
#endif
#ifdef ENABLE_LOG_WRITE_TO_FILE
std::string log_filename = GetLogFilePathName();
FILE *fp = fopen(log_filename.c_str() ,"a");
if(!fp) {
printf("%s open filed!\n", log_filename.c_str());
return ;
}
printf_s(fp,str);
printf_s(fp,"\r\n");
fclose(fp);
#endif
}
void LogPrint::LogPrintData(const char* str) {
#ifdef ENABLE_CONSOLE_LOG_DISPALY
printf("%s", str);
#endif
#ifdef ENABLE_LOG_WRITE_TO_FILE
std::string log_filename = GetOriginDataFilePathName();
FILE *fp = fopen(log_filename.c_str() ,"a");
if(!fp) {
printf("%s open filed!\n", log_filename.c_str());
return ;
}
printf_s(fp,str);
fclose(fp);
#endif
}
/********************* (C) COPYRIGHT SHENZHEN LDROBOT CO., LTD *******END OF FILE ********/