-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog4p.h
75 lines (60 loc) · 1.78 KB
/
log4p.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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2021, 4Paradigm Inc. */
#ifndef LOG4P_H
#define LOG4P_H
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#include <iostream>
#include <limits>
#include <string>
#include <stdio.h>
#include <stdarg.h>
#define LOG4P_LEVEL_ERROR 0
#define LOG4P_LEVEL_WARNING 1
#define LOG4P_LEVEL_INFO 2
#define LOG4P_LEVEL_DEBUG 3
#define LOG4P_LEVEL LOG4P_LEVEL_INFO
const static char* _LOG4P_LEVEL_PREFIX[] =
{
" \033[1;31mE: ",
" \033[1;35mW: ",
" ",
" \033[1;33mD: "
};
static void _LOG4P(int logLevel, const char* file, const char* func, unsigned int line, const char* format, ...)
{
// if (logLevel > LOG4P_LEVEL)
// return;
char message[512];
va_list args;
va_start(args, format);
vsnprintf(message, 512, format, args);
va_end(args);
std::cout << file << ":" << line << " \033[1;36m" << func << "\033[0m" << _LOG4P_LEVEL_PREFIX[logLevel] << message << "\033[0m" << std::endl;
}
#define LOG4P(logLevel, format, ...) \
{_LOG4P(logLevel, __FILENAME__, __func__, __LINE__, format, ##__VA_ARGS__);}
#if (LOG4P_LEVEL>=LOG4P_LEVEL_ERROR)
#define LOG4P_ERROR(format, ...) \
LOG4P(LOG4P_LEVEL_ERROR, format, ##__VA_ARGS__)
#else
#define LOG4P_ERROR(format, ...) {}
#endif
#if (LOG4P_LEVEL>=LOG4P_LEVEL_WARNING)
#define LOG4P_WARNING(format, ...) \
LOG4P(LOG4P_LEVEL_WARNING, format, ##__VA_ARGS__)
#else
#define LOG4P_WARNING(format, ...) {}
#endif
#if (LOG4P_LEVEL>=LOG4P_LEVEL_INFO)
#define LOG4P_INFO(format, ...) \
LOG4P(LOG4P_LEVEL_INFO, format, ##__VA_ARGS__)
#else
#define LOG4P_INFO(format, ...) {}
#endif
#if (LOG4P_LEVEL>=LOG4P_LEVEL_DEBUG)
#define LOG4P_DEBUG(format, ...) \
LOG4P(LOG4P_LEVEL_DEBUG, format, ##__VA_ARGS__)
#else
#define LOG4P_DEBUG(format, ...) {}
#endif
#endif // LOG4P_H