|
| 1 | +/* |
| 2 | + * WebARKitLog.cpp |
| 3 | + * WebARKit |
| 4 | + * |
| 5 | + * This file is part of WebARKit. |
| 6 | + * |
| 7 | + * WebARKit is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * WebARKit is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with WebARKit. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * As a special exception, the copyright holders of this library give you |
| 21 | + * permission to link this library with independent modules to produce an |
| 22 | + * executable, regardless of the license terms of these independent modules, and to |
| 23 | + * copy and distribute the resulting executable under terms of your choice, |
| 24 | + * provided that you also meet, for each linked independent module, the terms and |
| 25 | + * conditions of the license of that module. An independent module is a module |
| 26 | + * which is neither derived from nor based on this library. If you modify this |
| 27 | + * library, you may extend this exception to your version of the library, but you |
| 28 | + * are not obligated to do so. If you do not wish to do so, delete this exception |
| 29 | + * statement from your version. |
| 30 | + * |
| 31 | + * Copyright 2015-2016 Daqri, LLC. |
| 32 | + * Copyright 2003-2015 ARToolworks, Inc. |
| 33 | + * Copyright 2023 WebARKit. |
| 34 | + * |
| 35 | + * Author(s): Hirokazu Kato, Philip Lamb, Walter Perdan |
| 36 | + * |
| 37 | + * This code was taken from Artoolkit5 https://github.com/artoolkitx/artoolkit5 |
| 38 | + * with small modifications to adapt to existing WebARKIt code |
| 39 | + * |
| 40 | + */ |
| 41 | + |
| 42 | +#include <WebARKitLog.h> |
| 43 | + |
| 44 | +#ifndef _WIN32 |
| 45 | +# include <pthread.h> // pthread_self(), pthread_equal() |
| 46 | +# ifdef __APPLE__ |
| 47 | +# include <os/log.h> |
| 48 | +# endif |
| 49 | +#else |
| 50 | +# include <Windows.h> |
| 51 | +# define snprintf _snprintf |
| 52 | +#endif |
| 53 | + |
| 54 | +// |
| 55 | +// Global required for logging functions. |
| 56 | +// |
| 57 | +int webarkitLogLevel = WEBARKIT_LOG_LEVEL_DEFAULT; |
| 58 | +static WEBARKIT_LOG_LOGGER_CALLBACK webarkitLogLoggerCallback = NULL; |
| 59 | +static int webarkitLogLoggerCallBackOnlyIfOnSameThread = 0; |
| 60 | +#ifndef _WIN32 |
| 61 | +static pthread_t webarkitLogLoggerThread; |
| 62 | +#else |
| 63 | +static DWORD webarkitLogLoggerThreadID; |
| 64 | +#endif |
| 65 | +#define WEBARKIT_LOG_WRONG_THREAD_BUFFER_SIZE 4096 |
| 66 | +static char *webarkitLogWrongThreadBuffer = NULL; |
| 67 | +static size_t webarkitLogWrongThreadBufferSize = 0; |
| 68 | +static size_t webarkitLogWrongThreadBufferCount = 0; |
| 69 | + |
| 70 | + |
| 71 | +void webarkitLogSetLogger(WEBARKIT_LOG_LOGGER_CALLBACK callback, int callBackOnlyIfOnSameThread) |
| 72 | +{ |
| 73 | + webarkitLogLoggerCallback = callback; |
| 74 | + webarkitLogLoggerCallBackOnlyIfOnSameThread = callBackOnlyIfOnSameThread; |
| 75 | + if (callback && callBackOnlyIfOnSameThread) { |
| 76 | +#ifndef _WIN32 |
| 77 | + webarkitLogLoggerThread = pthread_self(); |
| 78 | +#else |
| 79 | + webarkitLogLoggerThreadID = GetCurrentThreadId(); |
| 80 | +#endif |
| 81 | + if (!webarkitLogWrongThreadBuffer) { |
| 82 | + if ((webarkitLogWrongThreadBuffer = static_cast<char*>(malloc(sizeof(char) * WEBARKIT_LOG_WRONG_THREAD_BUFFER_SIZE)))) { |
| 83 | + webarkitLogWrongThreadBufferSize = WEBARKIT_LOG_WRONG_THREAD_BUFFER_SIZE; |
| 84 | + } |
| 85 | + } |
| 86 | + } else { |
| 87 | + if (webarkitLogWrongThreadBuffer) { |
| 88 | + free(webarkitLogWrongThreadBuffer); |
| 89 | + webarkitLogWrongThreadBuffer = NULL; |
| 90 | + webarkitLogWrongThreadBufferSize = 0; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void webarkitLog(const char *tag, const int logLevel, const char *format, ...) |
| 96 | +{ |
| 97 | + if (logLevel < webarkitLogLevel) return; |
| 98 | + if (!format || !format[0]) return; |
| 99 | + |
| 100 | + va_list ap; |
| 101 | + va_start(ap, format); |
| 102 | + webarkitLogv(tag, logLevel, format, ap); |
| 103 | + va_end(ap); |
| 104 | +} |
| 105 | + |
| 106 | +void webarkitLogv(const char *tag, const int logLevel, const char *format, va_list ap) |
| 107 | +{ |
| 108 | + va_list ap2; |
| 109 | + char *buf = NULL; |
| 110 | + size_t len; |
| 111 | + const char *logLevelStrings[] = { |
| 112 | + "debug", |
| 113 | + "info", |
| 114 | + "warning", |
| 115 | + "error" |
| 116 | + }; |
| 117 | + const size_t logLevelStringsCount = (sizeof(logLevelStrings)/sizeof(logLevelStrings[0])); |
| 118 | + size_t logLevelStringLen; |
| 119 | + |
| 120 | + if (logLevel < webarkitLogLevel) return; |
| 121 | + if (!format || !format[0]) return; |
| 122 | + |
| 123 | + // Count length required to unpack varargs. |
| 124 | + va_copy(ap2, ap); |
| 125 | +#ifdef _WIN32 |
| 126 | + len = _vscprintf(format, ap); |
| 127 | +#else |
| 128 | + len = vsnprintf(NULL, 0, format, ap2); |
| 129 | +#endif |
| 130 | + va_end(ap2); |
| 131 | + if (len < 1) return; |
| 132 | + |
| 133 | + // Add characters required for logLevelString. |
| 134 | + if (logLevel >= 0 && logLevel < (int)logLevelStringsCount) { |
| 135 | + logLevelStringLen = 3 + strlen(logLevelStrings[logLevel]); // +3 for brackets and a space, e.g. "[debug] ". |
| 136 | + } else { |
| 137 | + logLevelStringLen = 0; |
| 138 | + } |
| 139 | + |
| 140 | + buf = (char *)malloc((logLevelStringLen + len + 1) * sizeof(char)); // +1 for nul-term. |
| 141 | + |
| 142 | + if (logLevelStringLen > 0) { |
| 143 | + snprintf(buf, logLevelStringLen + 1, "[%s] ", logLevelStrings[logLevel]); |
| 144 | + } |
| 145 | + |
| 146 | + vsnprintf(buf + logLevelStringLen, len + 1, format, ap); |
| 147 | + len += logLevelStringLen; |
| 148 | + |
| 149 | + if (webarkitLogLoggerCallback) { |
| 150 | + |
| 151 | + if (!webarkitLogLoggerCallBackOnlyIfOnSameThread) { |
| 152 | + (*webarkitLogLoggerCallback)(buf); |
| 153 | + } else { |
| 154 | +#ifndef _WIN32 |
| 155 | + if (!pthread_equal(pthread_self(), webarkitLogLoggerThread)) |
| 156 | +#else |
| 157 | + if (GetCurrentThreadId() != webarkitLogLoggerThreadID) |
| 158 | +#endif |
| 159 | + { |
| 160 | + // On non-log thread, put it into buffer if we can. |
| 161 | + if (webarkitLogWrongThreadBuffer && (webarkitLogWrongThreadBufferCount < webarkitLogWrongThreadBufferSize)) { |
| 162 | + if (len <= (webarkitLogWrongThreadBufferSize - (webarkitLogWrongThreadBufferCount + 4))) { // +4 to reserve space for "...\0". |
| 163 | + strncpy(&webarkitLogWrongThreadBuffer[webarkitLogWrongThreadBufferCount], buf, len + 1); |
| 164 | + webarkitLogWrongThreadBufferCount += len; |
| 165 | + } else { |
| 166 | + strncpy(&webarkitLogWrongThreadBuffer[webarkitLogWrongThreadBufferCount], "...", 4); |
| 167 | + webarkitLogWrongThreadBufferCount = webarkitLogWrongThreadBufferSize; // Mark buffer as full. |
| 168 | + } |
| 169 | + } |
| 170 | + } else { |
| 171 | + // On log thread, print buffer if anything was in it, then the current message. |
| 172 | + if (webarkitLogWrongThreadBufferCount > 0) { |
| 173 | + (*webarkitLogLoggerCallback)(webarkitLogWrongThreadBuffer); |
| 174 | + webarkitLogWrongThreadBufferCount = 0; |
| 175 | + } |
| 176 | + (*webarkitLogLoggerCallback)(buf); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + } else { |
| 181 | +#if defined(__ANDROID__) |
| 182 | + int logLevelA; |
| 183 | + switch (logLevel) { |
| 184 | + case WEBARKIT_LOG_LEVEL_REL_INFO: logLevelA = ANDROID_LOG_ERROR; break; |
| 185 | + case WEBARKIT_LOG_LEVEL_ERROR: logLevelA = ANDROID_LOG_ERROR; break; |
| 186 | + case WEBARKIT_LOG_LEVEL_WARN: logLevelA = ANDROID_LOG_WARN; break; |
| 187 | + case WEBARKIT_LOG_LEVEL_INFO: logLevelA = ANDROID_LOG_INFO; break; |
| 188 | + case WEBARKIT_LOG_LEVEL_DEBUG: default: logLevelA = ANDROID_LOG_DEBUG; break; |
| 189 | + } |
| 190 | + __android_log_write(logLevelA, (tag ? tag : "libAR"), buf); |
| 191 | + //#elif defined(_WINRT) |
| 192 | + // OutputDebugStringA(buf); |
| 193 | +#elif defined(__APPLE__) |
| 194 | + if (os_log_create == NULL) { // os_log only available macOS 10.12 / iOS 10.0 and later. |
| 195 | + fprintf(stderr, "%s", buf); |
| 196 | + } else { |
| 197 | + os_log_type_t type; |
| 198 | + switch (logLevel) { |
| 199 | + case WEBARKIT_LOG_LEVEL_REL_INFO: type = OS_LOG_TYPE_DEFAULT; break; |
| 200 | + case WEBARKIT_LOG_LEVEL_ERROR: type = OS_LOG_TYPE_ERROR; break; |
| 201 | + case WEBARKIT_LOG_LEVEL_WARN: type = OS_LOG_TYPE_DEFAULT; break; |
| 202 | + case WEBARKIT_LOG_LEVEL_INFO: type = OS_LOG_TYPE_INFO; break; |
| 203 | + case WEBARKIT_LOG_LEVEL_DEBUG: default: type = OS_LOG_TYPE_DEBUG; break; |
| 204 | + } |
| 205 | + os_log_with_type(OS_LOG_DEFAULT, type, "%{public}s", buf); |
| 206 | + } |
| 207 | +#else |
| 208 | + fprintf(stderr, "%s", buf); |
| 209 | +#endif |
| 210 | + } |
| 211 | + free(buf); |
| 212 | +} |
0 commit comments