-
Notifications
You must be signed in to change notification settings - Fork 30
/
macrologger.h
126 lines (101 loc) · 3.68 KB
/
macrologger.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2012 David Rodrigues
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef __MACROLOGGER_H__
#define __MACROLOGGER_H__
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#else
#include <time.h>
#include <string.h>
#endif
// === auxiliar functions
static inline char *timenow();
#define _FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__
#define NO_LOG 0x00
#define ERROR_LEVEL 0x01
#define INFO_LEVEL 0x02
#define DEBUG_LEVEL 0x03
#ifndef LOG_LEVEL
#define LOG_LEVEL DEBUG_LEVEL
#endif
#ifdef __OBJC__
#if __has_feature(objc_arc)
#define AUTORELEASEPOOL_BEGIN @autoreleasepool {
#define AUTORELEASEPOOL_END }
#define RELEASE(OBJ) OBJ = nil
#else
#define AUTORELEASEPOOL_BEGIN NSAutoreleasePool *_pool = [[NSAutoreleasePool alloc] init];
#define AUTORELEASEPOOL_END [_pool release];
#define RELEASE(OBJ) [OBJ release];
#endif
#define PRINTFUNCTION(format, ...) objc_print(@format, __VA_ARGS__)
#else
#define PRINTFUNCTION(format, ...) fprintf(stderr, format, __VA_ARGS__)
#endif
#define LOG_FMT "%s | %-7s | %-15s | %s:%d | "
#define LOG_ARGS(LOG_TAG) timenow(), LOG_TAG, _FILE, __FUNCTION__, __LINE__
#define NEWLINE "\n"
#define ERROR_TAG "ERROR"
#define INFO_TAG "INFO"
#define DEBUG_TAG "DEBUG"
#if LOG_LEVEL >= DEBUG_LEVEL
#define LOG_DEBUG(message, args...) PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(DEBUG_TAG), ## args)
#else
#define LOG_DEBUG(message, args...)
#endif
#if LOG_LEVEL >= INFO_LEVEL
#define LOG_INFO(message, args...) PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(INFO_TAG), ## args)
#else
#define LOG_INFO(message, args...)
#endif
#if LOG_LEVEL >= ERROR_LEVEL
#define LOG_ERROR(message, args...) PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(ERROR_TAG), ## args)
#else
#define LOG_ERROR(message, args...)
#endif
#if LOG_LEVEL >= NO_LOGS
#define LOG_IF_ERROR(condition, message, args...) if (condition) PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(ERROR_TAG), ## args)
#else
#define LOG_IF_ERROR(condition, message, args...)
#endif
static inline char *timenow() {
static char buffer[64];
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 64, "%Y-%m-%d %H:%M:%S", timeinfo);
return buffer;
}
#ifdef __OBJC__
static inline void objc_print(NSString *format, ...) {
AUTORELEASEPOOL_BEGIN
va_list args;
va_start(args, format);
NSString *logStr = [[NSString alloc] initWithFormat:format arguments:args];
fprintf(stderr, "%s", [logStr UTF8String]);
RELEASE(logStr);
va_end(args);
AUTORELEASEPOOL_END
}
#endif
#endif