This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.h
109 lines (91 loc) · 3.39 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
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
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Stephen Foulds
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#pragma once
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define HAVE_GNU_STRERROR_R 1
#ifndef __FILENAME__
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
#define LEVEL_DEBUG 3
#define LEVEL_INFO 2
#define LEVEL_WARN 1
#define LEVEL_ERROR 0
#ifndef NDEBUG
#define LOG_DEBUG(fmt, ...) \
__LOG(LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#else
#define LOG_DEBUG(fmt, ...)
#endif
#define LOG_INFO(fmt, ...) \
__LOG(LEVEL_INFO, fmt, ##__VA_ARGS__)
#define LOG_WARN(fmt, ...) \
__LOG(LEVEL_WARN, fmt, ##__VA_ARGS__)
#define LOG_SYS_WARN(err, fmt, ...) \
__LOG_SYS(err, LEVEL_WARN, fmt, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) \
__LOG(LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define LOG_SYS_ERROR(err, fmt, ...) \
__LOG_SYS(err, LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define __LOG(level, fmt, ...) \
do \
{ \
fprintf(stderr, "%s[%s:%d](%s): " fmt "\n", getLogLevel(level), __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
} while (0)
#define __LOG_SYS(err, level, fmt, ...) \
do \
{ \
fprintf(stderr, "%s[%s:%d](%s): " fmt " (%d - %s)\n", getLogLevel(level), __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__, err, getErrString(err)); \
} while (0)
inline const char *getLogLevel(int level)
{
switch (level) {
case LEVEL_DEBUG:
return "[DBG]";
case LEVEL_INFO:
return "[NFO]";
case LEVEL_WARN:
return "[WRN]";
case LEVEL_ERROR:
return "[ERR]";
default:
return "";
}
}
inline const char *getErrString(int err)
{
char errbuf[64];
const char *errmsg = nullptr;
#ifdef HAVE_GNU_STRERROR_R
errmsg = strerror_r(err, errbuf, sizeof(errbuf));
#else
if (strerror_r(err, errbuf, sizeof(errbuf)) != 0)
errmsg = "Unknown error";
else
errmsg = errbuf;
#endif
return errmsg;
}
#ifdef __cplusplus
}
#endif