-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.h
86 lines (67 loc) · 1.97 KB
/
Log.h
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
/*
* Copyright (c) 2024 Quantag IT Solutions GmbH
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <stdarg.h>
#include <string>
typedef unsigned char byte;
/**
* Logger output mask
*/
#define LO_CONSOLE ( 1 )
#define LO_FILE ( 2 )
#define LO_DEBUGGER ( 8 )
/**
* Macros for global logger
*/
#define LOG_INIT Logger::logger.init
#define LOG(...) Logger::logger.log(__FUNCTION__,__VA_ARGS__)
#define LOG_DATA(...) Logger::logger.data(__FUNCTION__,__VA_ARGS__)
#define LOGE(...) LOG(Log::eErr, __VA_ARGS__)
#define LOGW(...) LOG(Log::eInfo, __VA_ARGS__)
#define LOGI(...) LOG(Log::eInfo, __VA_ARGS__)
#define LOGS(...) LOG(Log::eStream, __VA_ARGS__)
#define LOGSD(...) LOG(Log::eStreamDbg, __VA_ARGS__)
#define LOGD(...) LOG(Log::eStream, __VA_ARGS__)
namespace Log {
enum {
eErr = 1,
eInfo,
eStream,
eStreamDbg
};
};
/**
* class Logger - to log in file
*/
class Logger
{
public:
// Global logger
static Logger logger;
// Constructor/Destructor
Logger();
virtual ~Logger();
// Implementation
void init(int level, const std::string& filename);
void log(const std::string &funcName, int level, const char* format, ...);
void data(const std::string &funcName, int level, const char *title, const byte* buf, unsigned int len);
void setLogLevel(int level) { _level = level; }
void setMode(int mode) { _mode = mode; }
const std::string& getBaseFileName() const { return baseFileName; }
const std::string& getFileName() const { return _filepath; }
void setFileName(const std::string& fileName);
protected:
void logArgs(int level, const std::string &funcName, const char* format, va_list args);
std::string buildLogLine(const std::string& funcName, const std::string& buf);
std::string getCurrentTimestamp();
private:
// Members
int _mode;
int _level;
std::string _filepath; // real full file name
std::string baseFileName; // core of file name
};