Loading...
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- * * Copyright (c) 2016 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <dispatch/dispatch.h> #include <string.h> #include <stdlib.h> #include <set> #include <assert.h> #include "mega-dylib-utils.h" #include "Logging.h" #define MAX_LOG_STR_LEN (512) //const char* kDispatchQueueLogNameKey = "kDispatchQueueLogNameKey"; const char* kDispatchWarningArrayKey = "kDispatchWarningArrayKey"; static dispatch_queue_t log_queue; static dispatch_once_t logQueueInit = 0; static dispatch_queue_t unique_queue; static dispatch_once_t uniqueQueueInit = 0; static uint32_t verbose = 0; static bool returnNonZeroIfTerminateCalled = false; static bool terminateCalled = false; static const char* warningPrefix = "WARNING: "; static const char* errorPrefix = "ERROR: "; LoggingContext::LoggingContext(const std::string& N) : _name(N) , _tainted(false) { } LoggingContext::LoggingContext(const std::string& N, WarningTargets T) : _name(N) , _warnings(T) , _tainted(false) { } void LoggingContext::taint() { _tainted = true; } bool LoggingContext::isTainted() { return _tainted; } const std::string& LoggingContext::name() { return _name; } const WarningTargets& LoggingContext::targets() { return _warnings; } pthread_key_t getLoggingContextKey(void) { static pthread_key_t logContextKey; static dispatch_once_t logContextToken; dispatch_once(&logContextToken, ^{ pthread_key_create(&logContextKey, nullptr); }); return logContextKey; } void setLoggingContext(std::shared_ptr<LoggingContext>& context) { pthread_setspecific(getLoggingContextKey(), (void*)&context); if (context && !context->name().empty()) { pthread_setname_np(context->name().substr(0, MAXTHREADNAMESIZE-1).c_str()); } } std::shared_ptr<LoggingContext> getLoggingContext() { if (void* val = pthread_getspecific(getLoggingContextKey())) return *((std::shared_ptr<LoggingContext>*)val); return nullptr; } void runBody(void* Ctx) { std::unique_ptr<std::function<void(void)>> Body(reinterpret_cast<std::function<void(void)>*>(Ctx)); (*Body)(); } static dispatch_queue_t getLogQueue() { dispatch_once(&logQueueInit, ^{ log_queue = dispatch_queue_create("com.apple.dyld.cache.logging", DISPATCH_QUEUE_SERIAL); }); return log_queue; } void setVerbose(bool level) { verbose = level; } void setWarnAnErrorPrefixes(const char* warn, const char* err) { warningPrefix = warn; errorPrefix = err; } void setReturnNonZeroOnTerminate() { returnNonZeroIfTerminateCalled = true; } void queued_print(FILE* __restrict fd, const char* str) { const char* qstr = strdup(str); dispatch_async(getLogQueue(), ^{ (void)fprintf(fd, "%s", qstr); free((void*)qstr); }); } #define VLOG(header) \ va_list list; \ va_start(list, format); \ char temp[MAX_LOG_STR_LEN]; \ vsprintf(temp, format, list); \ auto ctx = getLoggingContext(); \ char temp2[MAX_LOG_STR_LEN]; \ if (ctx && !ctx->name().empty()) { \ snprintf(temp2, MAX_LOG_STR_LEN, "[%s] %s%s\n", ctx->name().c_str(), header, \ temp); \ } else { \ snprintf(temp2, MAX_LOG_STR_LEN, "%s%s\n", header, temp); \ } \ queued_print(stderr, temp2); \ va_end(list); void log(const char* __restrict format, ...) { VLOG(""); } void verboseLog(const char* format, ...) { if (verbose) { VLOG(""); } } static std::set<std::string> warnings; void warning(const char* format, ...) { dispatch_once(&uniqueQueueInit, ^{ unique_queue = dispatch_queue_create("com.apple.dyld.cache.logging", DISPATCH_QUEUE_SERIAL); }); va_list list; va_start(list, format); char temp[MAX_LOG_STR_LEN]; vsprintf(temp, format, list); char* blockTemp = strdup(temp); auto ctx = getLoggingContext(); if (ctx) { for (auto& target : ctx->targets().second) { ctx->targets().first->configurations[target.first].architectures[target.second].results.warnings.push_back(blockTemp); } } dispatch_sync(unique_queue, ^{ if (warnings.count(blockTemp) == 0) { warnings.insert(blockTemp); } free(blockTemp); }); va_end(list); } void terminate(const char* format, ...) { VLOG(errorPrefix); terminateCalled = true; if (ctx) { // We are a work in a logging context, throw throw std::string(temp); } else { // We are in general handing, let the loggging queue darain and exit dispatch_sync(getLogQueue(), ^{ for (auto& warning : warnings) { (void)fprintf(stderr, "%s%s\n", warningPrefix, warning.c_str()); } if ( returnNonZeroIfTerminateCalled ) { exit(1); } else { time_t endtime = time(0); (void)fprintf(stderr, "Finished: %s\n", asctime(localtime(&endtime))); (void)fprintf(stderr, "Exiting\n"); exit(0); } }); } // clang can't reason out that we won't hit this due to the dispatch_sync in the exit path __builtin_unreachable(); } void dumpLogAndExit(bool logFinishTime) { dispatch_async(getLogQueue(), ^{ for (auto& warning : warnings) { (void)fprintf(stderr, "%s%s\n", warningPrefix, warning.c_str()); } if ( logFinishTime ) { time_t endtime = time(0); (void)fprintf(stderr, "Finished: %s\n", asctime(localtime(&endtime))); (void)fprintf(stderr, "Exiting\n"); } exit(returnNonZeroIfTerminateCalled && terminateCalled ? 1 : 0); }); } |