-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlog.c
208 lines (191 loc) · 7.41 KB
/
log.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* log.c
* ARToolKit5
*
* This file is part of ARToolKit.
*
* ARToolKit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARToolKit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ARToolKit. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2015-2016 Daqri, LLC.
* Copyright 2003-2015 ARToolworks, Inc.
*
* Author(s): Hirokazu Kato, Philip Lamb
*
*/
#include <ARUtil/log.h>
#ifndef _WIN32
# include <pthread.h> // pthread_self(), pthread_equal()
# ifdef __APPLE__
# include <os/log.h>
# endif
#else
# include <Windows.h>
# define snprintf _snprintf
#endif
//
// Global required for logging functions.
//
int arLogLevel = AR_LOG_LEVEL_DEFAULT;
static AR_LOG_LOGGER_CALLBACK arLogLoggerCallback = NULL;
static int arLogLoggerCallBackOnlyIfOnSameThread = 0;
#ifndef _WIN32
static pthread_t arLogLoggerThread;
#else
static DWORD arLogLoggerThreadID;
#endif
#define AR_LOG_WRONG_THREAD_BUFFER_SIZE 4096
static char *arLogWrongThreadBuffer = NULL;
static size_t arLogWrongThreadBufferSize = 0;
static size_t arLogWrongThreadBufferCount = 0;
void arLogSetLogger(AR_LOG_LOGGER_CALLBACK callback, int callBackOnlyIfOnSameThread)
{
arLogLoggerCallback = callback;
arLogLoggerCallBackOnlyIfOnSameThread = callBackOnlyIfOnSameThread;
if (callback && callBackOnlyIfOnSameThread) {
#ifndef _WIN32
arLogLoggerThread = pthread_self();
#else
arLogLoggerThreadID = GetCurrentThreadId();
#endif
if (!arLogWrongThreadBuffer) {
if ((arLogWrongThreadBuffer = malloc(sizeof(char) * AR_LOG_WRONG_THREAD_BUFFER_SIZE))) {
arLogWrongThreadBufferSize = AR_LOG_WRONG_THREAD_BUFFER_SIZE;
}
}
} else {
if (arLogWrongThreadBuffer) {
free(arLogWrongThreadBuffer);
arLogWrongThreadBuffer = NULL;
arLogWrongThreadBufferSize = 0;
}
}
}
void arLog(const char *tag, const int logLevel, const char *format, ...)
{
if (logLevel < arLogLevel) return;
if (!format || !format[0]) return;
va_list ap;
va_start(ap, format);
arLogv(tag, logLevel, format, ap);
va_end(ap);
}
void arLogv(const char *tag, const int logLevel, const char *format, va_list ap)
{
va_list ap2;
char *buf = NULL;
size_t len;
const char *logLevelStrings[] = {
"debug",
"info",
"warning",
"error"
};
const size_t logLevelStringsCount = (sizeof(logLevelStrings)/sizeof(logLevelStrings[0]));
size_t logLevelStringLen;
if (logLevel < arLogLevel) return;
if (!format || !format[0]) return;
// Count length required to unpack varargs.
va_copy(ap2, ap);
#ifdef _WIN32
len = _vscprintf(format, ap);
#else
len = vsnprintf(NULL, 0, format, ap2);
#endif
va_end(ap2);
if (len < 1) return;
// Add characters required for logLevelString.
if (logLevel >= 0 && logLevel < (int)logLevelStringsCount) {
logLevelStringLen = 3 + strlen(logLevelStrings[logLevel]); // +3 for brackets and a space, e.g. "[debug] ".
} else {
logLevelStringLen = 0;
}
buf = (char *)malloc((logLevelStringLen + len + 1) * sizeof(char)); // +1 for nul-term.
if (logLevelStringLen > 0) {
snprintf(buf, logLevelStringLen + 1, "[%s] ", logLevelStrings[logLevel]);
}
vsnprintf(buf + logLevelStringLen, len + 1, format, ap);
len += logLevelStringLen;
if (arLogLoggerCallback) {
if (!arLogLoggerCallBackOnlyIfOnSameThread) {
(*arLogLoggerCallback)(buf);
} else {
#ifndef _WIN32
if (!pthread_equal(pthread_self(), arLogLoggerThread))
#else
if (GetCurrentThreadId() != arLogLoggerThreadID)
#endif
{
// On non-log thread, put it into buffer if we can.
if (arLogWrongThreadBuffer && (arLogWrongThreadBufferCount < arLogWrongThreadBufferSize)) {
if (len <= (arLogWrongThreadBufferSize - (arLogWrongThreadBufferCount + 4))) { // +4 to reserve space for "...\0".
strncpy(&arLogWrongThreadBuffer[arLogWrongThreadBufferCount], buf, len + 1);
arLogWrongThreadBufferCount += len;
} else {
strncpy(&arLogWrongThreadBuffer[arLogWrongThreadBufferCount], "...", 4);
arLogWrongThreadBufferCount = arLogWrongThreadBufferSize; // Mark buffer as full.
}
}
} else {
// On log thread, print buffer if anything was in it, then the current message.
if (arLogWrongThreadBufferCount > 0) {
(*arLogLoggerCallback)(arLogWrongThreadBuffer);
arLogWrongThreadBufferCount = 0;
}
(*arLogLoggerCallback)(buf);
}
}
} else {
#if defined(__ANDROID__)
int logLevelA;
switch (logLevel) {
case AR_LOG_LEVEL_REL_INFO: logLevelA = ANDROID_LOG_ERROR; break;
case AR_LOG_LEVEL_ERROR: logLevelA = ANDROID_LOG_ERROR; break;
case AR_LOG_LEVEL_WARN: logLevelA = ANDROID_LOG_WARN; break;
case AR_LOG_LEVEL_INFO: logLevelA = ANDROID_LOG_INFO; break;
case AR_LOG_LEVEL_DEBUG: default: logLevelA = ANDROID_LOG_DEBUG; break;
}
__android_log_write(logLevelA, (tag ? tag : "libAR"), buf);
//#elif defined(_WINRT)
// OutputDebugStringA(buf);
#elif defined(__APPLE__)
if (os_log_create == NULL) { // os_log only available macOS 10.12 / iOS 10.0 and later.
fprintf(stderr, "%s", buf);
} else {
os_log_type_t type;
switch (logLevel) {
case AR_LOG_LEVEL_REL_INFO: type = OS_LOG_TYPE_DEFAULT; break;
case AR_LOG_LEVEL_ERROR: type = OS_LOG_TYPE_ERROR; break;
case AR_LOG_LEVEL_WARN: type = OS_LOG_TYPE_DEFAULT; break;
case AR_LOG_LEVEL_INFO: type = OS_LOG_TYPE_INFO; break;
case AR_LOG_LEVEL_DEBUG: default: type = OS_LOG_TYPE_DEBUG; break;
}
os_log_with_type(OS_LOG_DEFAULT, type, "%{public}s", buf);
}
#else
fprintf(stderr, "%s", buf);
#endif
}
free(buf);
}