-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLogging.h
82 lines (64 loc) · 2.56 KB
/
Logging.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#ifndef LOGGING_H
#define LOGGING_H
#include <string.h>
// Maximum log size (1,024,000 is 1MB aka 1024 * 1000), increase or decrease as needed
#define MAX_LOG_SIZE 1024000
#define TIME_FORMAT_STRING_LENGTH 20
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#endif
typedef void* OSCONFIG_LOG_HANDLE;
#ifdef __cplusplus
extern "C"
{
#endif
OSCONFIG_LOG_HANDLE OpenLog(const char* logFileName, const char* bakLogFileName);
void CloseLog(OSCONFIG_LOG_HANDLE* log);
void SetFullLogging(bool fullLogging);
bool IsFullLoggingEnabled(void);
FILE* GetLogFile(OSCONFIG_LOG_HANDLE log);
char* GetFormattedTime();
void TrimLog(OSCONFIG_LOG_HANDLE log);
bool IsDaemon(void);
#define __SHORT_FILE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define __LOG__(log, format, loglevel, ...) printf("[%s] [%s:%d]%s" format "\n", GetFormattedTime(), __SHORT_FILE__, __LINE__, loglevel, ## __VA_ARGS__)
#define __LOG_TO_FILE__(log, format, loglevel, ...) {\
TrimLog(log);\
fprintf(GetLogFile(log), "[%s] [%s:%d]%s" format "\n", GetFormattedTime(), __SHORT_FILE__, __LINE__, loglevel, ## __VA_ARGS__);\
}\
#define __INFO__ " "
#define __ERROR__ " [ERROR] "
#define OSCONFIG_LOG_INFO(log, format, ...) __LOG__(log, format, __INFO__, ## __VA_ARGS__)
#define OSCONFIG_LOG_ERROR(log, format, ...) __LOG__(log, format, __ERROR__, ## __VA_ARGS__)
#define OSCONFIG_FILE_LOG_INFO(log, format, ...) __LOG_TO_FILE__(log, format, __INFO__, ## __VA_ARGS__)
#define OSCONFIG_FILE_LOG_ERROR(log, format, ...) __LOG_TO_FILE__(log, format, __ERROR__, ## __VA_ARGS__)
#define OsConfigLogInfo(log, FORMAT, ...) {\
if (NULL != GetLogFile(log)) {\
OSCONFIG_FILE_LOG_INFO(log, FORMAT, ##__VA_ARGS__);\
fflush(GetLogFile(log));\
}\
if ((false == IsDaemon()) || (false == IsFullLoggingEnabled())) {\
OSCONFIG_LOG_INFO(log, FORMAT, ##__VA_ARGS__);\
}\
}\
#define OsConfigLogError(log, FORMAT, ...) {\
if (NULL != GetLogFile(log)) {\
OSCONFIG_FILE_LOG_ERROR(log, FORMAT, ##__VA_ARGS__);\
fflush(GetLogFile(log));\
}\
if ((false == IsDaemon()) || (false == IsFullLoggingEnabled())) {\
OSCONFIG_LOG_ERROR(log, FORMAT, ##__VA_ARGS__);\
}\
}\
#define LogAssert(log, CONDITION) {\
if (!(CONDITION)) {\
OsConfigLogError(log, "Assert in %s", __func__);\
assert(CONDITION);\
}\
}\
#ifdef __cplusplus
}
#endif
#endif // LOGGING_H