1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300 |
- #ifndef DD_LEGACY_MACROS
- #define DD_LEGACY_MACROS 0
- #endif
- #import "DDLog.h"
- #import <pthread.h>
- #import <objc/runtime.h>
- #if TARGET_OS_IOS
- #import <UIKit/UIDevice.h>
- #import <UIKit/UIApplication.h>
- #elif !defined(DD_CLI) && __has_include(<AppKit/NSApplication.h>)
- #import <AppKit/NSApplication.h>
- #endif
- #if !__has_feature(objc_arc)
- #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
- #endif
- #ifndef DD_DEBUG
- #define DD_DEBUG NO
- #endif
- #define NSLogDebug(frmt, ...) do{ if(DD_DEBUG) NSLog((frmt), ##__VA_ARGS__); } while(0)
- #ifndef DDLOG_MAX_QUEUE_SIZE
- #define DDLOG_MAX_QUEUE_SIZE 1000
- #endif
- static void *const GlobalLoggingQueueIdentityKey = (void *)&GlobalLoggingQueueIdentityKey;
- @interface DDLoggerNode : NSObject
- {
-
- @public
- id <DDLogger> _logger;
- DDLogLevel _level;
- dispatch_queue_t _loggerQueue;
- }
- @property (nonatomic, readonly) id <DDLogger> logger;
- @property (nonatomic, readonly) DDLogLevel level;
- @property (nonatomic, readonly) dispatch_queue_t loggerQueue;
- + (DDLoggerNode *)nodeWithLogger:(id <DDLogger>)logger
- loggerQueue:(dispatch_queue_t)loggerQueue
- level:(DDLogLevel)level;
- @end
- #pragma mark -
- @interface DDLog ()
- @property (nonatomic, strong) NSMutableArray *_loggers;
- @end
- @implementation DDLog
- static dispatch_queue_t _loggingQueue;
- static dispatch_group_t _loggingGroup;
- static dispatch_semaphore_t _queueSemaphore;
- static NSUInteger _numProcessors;
- + (instancetype)sharedInstance {
- static id sharedInstance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedInstance = [[self alloc] init];
- });
- return sharedInstance;
- }
- + (void)initialize {
- static dispatch_once_t DDLogOnceToken;
- dispatch_once(&DDLogOnceToken, ^{
- NSLogDebug(@"DDLog: Using grand central dispatch");
- _loggingQueue = dispatch_queue_create("cocoa.lumberjack", NULL);
- _loggingGroup = dispatch_group_create();
- void *nonNullValue = GlobalLoggingQueueIdentityKey;
- dispatch_queue_set_specific(_loggingQueue, GlobalLoggingQueueIdentityKey, nonNullValue, NULL);
- _queueSemaphore = dispatch_semaphore_create(DDLOG_MAX_QUEUE_SIZE);
-
-
- _numProcessors = MAX([NSProcessInfo processInfo].processorCount, (NSUInteger) 1);
- NSLogDebug(@"DDLog: numProcessors = %@", @(_numProcessors));
- });
- }
- - (id)init {
- self = [super init];
- if (self) {
- self._loggers = [[NSMutableArray alloc] initWithCapacity:4];
- #if TARGET_OS_IOS
- NSString *notificationName = UIApplicationWillTerminateNotification;
- #else
- NSString *notificationName = nil;
-
- #if !defined(DD_CLI) && __has_include(<AppKit/NSApplication.h>)
- if (NSApp) {
- notificationName = NSApplicationWillTerminateNotification;
- }
- #endif
- if (!notificationName) {
-
-
- __weak __auto_type weakSelf = self;
- atexit_b (^{
- [weakSelf applicationWillTerminate:nil];
- });
- }
- #endif
- if (notificationName) {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationWillTerminate:)
- name:notificationName
- object:nil];
- }
- }
- return self;
- }
- + (dispatch_queue_t)loggingQueue {
- return _loggingQueue;
- }
- #pragma mark Notifications
- - (void)applicationWillTerminate:(NSNotification * __attribute__((unused)))notification {
- [self flushLog];
- }
- #pragma mark Logger Management
- + (void)addLogger:(id <DDLogger>)logger {
- [self.sharedInstance addLogger:logger];
- }
- - (void)addLogger:(id <DDLogger>)logger {
- [self addLogger:logger withLevel:DDLogLevelAll];
- }
- + (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level {
- [self.sharedInstance addLogger:logger withLevel:level];
- }
- - (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level {
- if (!logger) {
- return;
- }
- dispatch_async(_loggingQueue, ^{ @autoreleasepool {
- [self lt_addLogger:logger level:level];
- } });
- }
- + (void)removeLogger:(id <DDLogger>)logger {
- [self.sharedInstance removeLogger:logger];
- }
- - (void)removeLogger:(id <DDLogger>)logger {
- if (!logger) {
- return;
- }
- dispatch_async(_loggingQueue, ^{ @autoreleasepool {
- [self lt_removeLogger:logger];
- } });
- }
- + (void)removeAllLoggers {
- [self.sharedInstance removeAllLoggers];
- }
- - (void)removeAllLoggers {
- dispatch_async(_loggingQueue, ^{ @autoreleasepool {
- [self lt_removeAllLoggers];
- } });
- }
- + (NSArray<id<DDLogger>> *)allLoggers {
- return [self.sharedInstance allLoggers];
- }
- - (NSArray<id<DDLogger>> *)allLoggers {
- __block NSArray *theLoggers;
- dispatch_sync(_loggingQueue, ^{ @autoreleasepool {
- theLoggers = [self lt_allLoggers];
- } });
- return theLoggers;
- }
- + (NSArray<DDLoggerInformation *> *)allLoggersWithLevel {
- return [self.sharedInstance allLoggersWithLevel];
- }
- - (NSArray<DDLoggerInformation *> *)allLoggersWithLevel {
- __block NSArray *theLoggersWithLevel;
- dispatch_sync(_loggingQueue, ^{ @autoreleasepool {
- theLoggersWithLevel = [self lt_allLoggersWithLevel];
- } });
- return theLoggersWithLevel;
- }
- #pragma mark - Master Logging
- - (void)queueLogMessage:(DDLogMessage *)logMessage asynchronously:(BOOL)asyncFlag {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- dispatch_block_t logBlock = ^{
- dispatch_semaphore_wait(_queueSemaphore, DISPATCH_TIME_FOREVER);
-
-
- @autoreleasepool {
- [self lt_log:logMessage];
- }
- };
- if (asyncFlag) {
- dispatch_async(_loggingQueue, logBlock);
- } else if (dispatch_get_specific(GlobalLoggingQueueIdentityKey)) {
-
- logBlock();
- } else {
- dispatch_sync(_loggingQueue, logBlock);
- }
- }
- + (void)log:(BOOL)asynchronous
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(const char *)file
- function:(const char *)function
- line:(NSUInteger)line
- tag:(id)tag
- format:(NSString *)format, ... {
- va_list args;
- if (format) {
- va_start(args, format);
- NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
- va_end(args);
- va_start(args, format);
- [self log:asynchronous
- message:message
- level:level
- flag:flag
- context:context
- file:file
- function:function
- line:line
- tag:tag];
- va_end(args);
- }
- }
- - (void)log:(BOOL)asynchronous
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(const char *)file
- function:(const char *)function
- line:(NSUInteger)line
- tag:(id)tag
- format:(NSString *)format, ... {
- va_list args;
- if (format) {
- va_start(args, format);
- NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
- va_end(args);
- va_start(args, format);
- [self log:asynchronous
- message:message
- level:level
- flag:flag
- context:context
- file:file
- function:function
- line:line
- tag:tag];
- va_end(args);
- }
- }
- + (void)log:(BOOL)asynchronous
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(const char *)file
- function:(const char *)function
- line:(NSUInteger)line
- tag:(id)tag
- format:(NSString *)format
- args:(va_list)args {
- [self.sharedInstance log:asynchronous level:level flag:flag context:context file:file function:function line:line tag:tag format:format args:args];
- }
- - (void)log:(BOOL)asynchronous
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(const char *)file
- function:(const char *)function
- line:(NSUInteger)line
- tag:(id)tag
- format:(NSString *)format
- args:(va_list)args {
- if (format) {
- NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
- [self log:asynchronous
- message:message
- level:level
- flag:flag
- context:context
- file:file
- function:function
- line:line
- tag:tag];
- }
- }
- + (void)log:(BOOL)asynchronous
- message:(NSString *)message
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(const char *)file
- function:(const char *)function
- line:(NSUInteger)line
- tag:(id)tag {
- [self.sharedInstance log:asynchronous message:message level:level flag:flag context:context file:file function:function line:line tag:tag];
- }
- - (void)log:(BOOL)asynchronous
- message:(NSString *)message
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(const char *)file
- function:(const char *)function
- line:(NSUInteger)line
- tag:(id)tag {
- DDLogMessage *logMessage = [[DDLogMessage alloc] initWithMessage:message
- level:level
- flag:flag
- context:context
- file:[NSString stringWithFormat:@"%s", file]
- function:[NSString stringWithFormat:@"%s", function]
- line:line
- tag:tag
- options:(DDLogMessageOptions)0
- timestamp:nil];
- [self queueLogMessage:logMessage asynchronously:asynchronous];
- }
- + (void)log:(BOOL)asynchronous
- message:(DDLogMessage *)logMessage {
- [self.sharedInstance log:asynchronous message:logMessage];
- }
- - (void)log:(BOOL)asynchronous
- message:(DDLogMessage *)logMessage {
- [self queueLogMessage:logMessage asynchronously:asynchronous];
- }
- + (void)flushLog {
- [self.sharedInstance flushLog];
- }
- - (void)flushLog {
- dispatch_sync(_loggingQueue, ^{ @autoreleasepool {
- [self lt_flush];
- } });
- }
- #pragma mark Registered Dynamic Logging
- + (BOOL)isRegisteredClass:(Class)class {
- SEL getterSel = @selector(ddLogLevel);
- SEL setterSel = @selector(ddSetLogLevel:);
- #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
-
-
-
-
-
-
-
- BOOL result = NO;
- unsigned int methodCount, i;
- Method *methodList = class_copyMethodList(object_getClass(class), &methodCount);
- if (methodList != NULL) {
- BOOL getterFound = NO;
- BOOL setterFound = NO;
- for (i = 0; i < methodCount; ++i) {
- SEL currentSel = method_getName(methodList[i]);
- if (currentSel == getterSel) {
- getterFound = YES;
- } else if (currentSel == setterSel) {
- setterFound = YES;
- }
- if (getterFound && setterFound) {
- result = YES;
- break;
- }
- }
- free(methodList);
- }
- return result;
- #else
-
-
-
-
- Method getter = class_getClassMethod(class, getterSel);
- Method setter = class_getClassMethod(class, setterSel);
- if ((getter != NULL) && (setter != NULL)) {
- return YES;
- }
- return NO;
- #endif
- }
- + (NSArray *)registeredClasses {
-
-
-
-
-
-
-
-
-
-
- NSUInteger numClasses = 0;
- Class *classes = NULL;
- while (numClasses == 0) {
- numClasses = (NSUInteger)MAX(objc_getClassList(NULL, 0), 0);
-
-
- NSUInteger bufferSize = numClasses;
- classes = numClasses ? (Class *)malloc(sizeof(Class) * bufferSize) : NULL;
- if (classes == NULL) {
- return @[];
- }
- numClasses = (NSUInteger)MAX(objc_getClassList(classes, (int)bufferSize),0);
- if (numClasses > bufferSize || numClasses == 0) {
-
- free(classes);
- classes = NULL;
- numClasses = 0;
- }
- }
-
- NSMutableArray *result = [NSMutableArray arrayWithCapacity:numClasses];
- for (NSUInteger i = 0; i < numClasses; i++) {
- Class class = classes[i];
- if ([self isRegisteredClass:class]) {
- [result addObject:class];
- }
- }
- free(classes);
- return result;
- }
- + (NSArray *)registeredClassNames {
- NSArray *registeredClasses = [self registeredClasses];
- NSMutableArray *result = [NSMutableArray arrayWithCapacity:[registeredClasses count]];
- for (Class class in registeredClasses) {
- [result addObject:NSStringFromClass(class)];
- }
- return result;
- }
- + (DDLogLevel)levelForClass:(Class)aClass {
- if ([self isRegisteredClass:aClass]) {
- return [aClass ddLogLevel];
- }
- return (DDLogLevel)-1;
- }
- + (DDLogLevel)levelForClassWithName:(NSString *)aClassName {
- Class aClass = NSClassFromString(aClassName);
- return [self levelForClass:aClass];
- }
- + (void)setLevel:(DDLogLevel)level forClass:(Class)aClass {
- if ([self isRegisteredClass:aClass]) {
- [aClass ddSetLogLevel:level];
- }
- }
- + (void)setLevel:(DDLogLevel)level forClassWithName:(NSString *)aClassName {
- Class aClass = NSClassFromString(aClassName);
- [self setLevel:level forClass:aClass];
- }
- #pragma mark Logging Thread
- - (void)lt_addLogger:(id <DDLogger>)logger level:(DDLogLevel)level {
-
-
- for (DDLoggerNode* node in self._loggers) {
- if (node->_logger == logger
- && node->_level == level) {
-
- return;
- }
- }
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
- dispatch_queue_t loggerQueue = NULL;
- if ([logger respondsToSelector:@selector(loggerQueue)]) {
-
- loggerQueue = [logger loggerQueue];
- }
- if (loggerQueue == nil) {
-
-
- const char *loggerQueueName = NULL;
- if ([logger respondsToSelector:@selector(loggerName)]) {
- loggerQueueName = [[logger loggerName] UTF8String];
- }
- loggerQueue = dispatch_queue_create(loggerQueueName, NULL);
- }
- DDLoggerNode *loggerNode = [DDLoggerNode nodeWithLogger:logger loggerQueue:loggerQueue level:level];
- [self._loggers addObject:loggerNode];
- if ([logger respondsToSelector:@selector(didAddLoggerInQueue:)]) {
- dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [logger didAddLoggerInQueue:loggerNode->_loggerQueue];
- } });
- } else if ([logger respondsToSelector:@selector(didAddLogger)]) {
- dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [logger didAddLogger];
- } });
- }
- }
- - (void)lt_removeLogger:(id <DDLogger>)logger {
-
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
- DDLoggerNode *loggerNode = nil;
- for (DDLoggerNode *node in self._loggers) {
- if (node->_logger == logger) {
- loggerNode = node;
- break;
- }
- }
- if (loggerNode == nil) {
- NSLogDebug(@"DDLog: Request to remove logger which wasn't added");
- return;
- }
-
- if ([logger respondsToSelector:@selector(willRemoveLogger)]) {
- dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [logger willRemoveLogger];
- } });
- }
-
- [self._loggers removeObject:loggerNode];
- }
- - (void)lt_removeAllLoggers {
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
-
- for (DDLoggerNode *loggerNode in self._loggers) {
- if ([loggerNode->_logger respondsToSelector:@selector(willRemoveLogger)]) {
- dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [loggerNode->_logger willRemoveLogger];
- } });
- }
- }
-
- [self._loggers removeAllObjects];
- }
- - (NSArray *)lt_allLoggers {
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
- NSMutableArray *theLoggers = [NSMutableArray new];
- for (DDLoggerNode *loggerNode in self._loggers) {
- [theLoggers addObject:loggerNode->_logger];
- }
- return [theLoggers copy];
- }
- - (NSArray *)lt_allLoggersWithLevel {
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
- NSMutableArray *theLoggersWithLevel = [NSMutableArray new];
- for (DDLoggerNode *loggerNode in self._loggers) {
- [theLoggersWithLevel addObject:[DDLoggerInformation informationWithLogger:loggerNode->_logger
- andLevel:loggerNode->_level]];
- }
- return [theLoggersWithLevel copy];
- }
- - (void)lt_log:(DDLogMessage *)logMessage {
-
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
- if (_numProcessors > 1) {
-
-
-
-
-
-
- for (DDLoggerNode *loggerNode in self._loggers) {
-
- if (!(logMessage->_flag & loggerNode->_level)) {
- continue;
- }
- dispatch_group_async(_loggingGroup, loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [loggerNode->_logger logMessage:logMessage];
- } });
- }
- dispatch_group_wait(_loggingGroup, DISPATCH_TIME_FOREVER);
- } else {
-
- for (DDLoggerNode *loggerNode in self._loggers) {
-
- if (!(logMessage->_flag & loggerNode->_level)) {
- continue;
- }
- #if DD_DEBUG
-
- if (loggerNode->_loggerQueue == NULL) {
-
- NSLogDebug(@"DDLog: current node has loggerQueue == NULL");
- }
- else {
- dispatch_async(loggerNode->_loggerQueue, ^{
- if (dispatch_get_specific(GlobalLoggingQueueIdentityKey)) {
-
- NSLogDebug(@"DDLog: current node has loggerQueue == globalLoggingQueue");
- }
- });
- }
- #endif
-
- dispatch_sync(loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [loggerNode->_logger logMessage:logMessage];
- } });
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- dispatch_semaphore_signal(_queueSemaphore);
- }
- - (void)lt_flush {
-
-
-
-
- NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
- @"This method should only be run on the logging thread/queue");
- for (DDLoggerNode *loggerNode in self._loggers) {
- if ([loggerNode->_logger respondsToSelector:@selector(flush)]) {
- dispatch_group_async(_loggingGroup, loggerNode->_loggerQueue, ^{ @autoreleasepool {
- [loggerNode->_logger flush];
- } });
- }
- }
- dispatch_group_wait(_loggingGroup, DISPATCH_TIME_FOREVER);
- }
- #pragma mark Utilities
- NSString * __nullable DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy) {
- if (filePath == NULL) {
- return nil;
- }
- char *lastSlash = NULL;
- char *lastDot = NULL;
- char *p = (char *)filePath;
- while (*p != '\0') {
- if (*p == '/') {
- lastSlash = p;
- } else if (*p == '.') {
- lastDot = p;
- }
- p++;
- }
- char *subStr;
- NSUInteger subLen;
- if (lastSlash) {
- if (lastDot) {
-
- subStr = lastSlash + 1;
- subLen = (NSUInteger)(lastDot - subStr);
- } else {
-
- subStr = lastSlash + 1;
- subLen = (NSUInteger)(p - subStr);
- }
- } else {
- if (lastDot) {
-
- subStr = (char *)filePath;
- subLen = (NSUInteger)(lastDot - subStr);
- } else {
-
- subStr = (char *)filePath;
- subLen = (NSUInteger)(p - subStr);
- }
- }
- if (copy) {
- return [[NSString alloc] initWithBytes:subStr
- length:subLen
- encoding:NSUTF8StringEncoding];
- } else {
-
-
-
- return [[NSString alloc] initWithBytesNoCopy:subStr
- length:subLen
- encoding:NSUTF8StringEncoding
- freeWhenDone:NO];
- }
- }
- @end
- #pragma mark -
- @implementation DDLoggerNode
- - (instancetype)initWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue level:(DDLogLevel)level {
- if ((self = [super init])) {
- _logger = logger;
- if (loggerQueue) {
- _loggerQueue = loggerQueue;
- #if !OS_OBJECT_USE_OBJC
- dispatch_retain(loggerQueue);
- #endif
- }
- _level = level;
- }
- return self;
- }
- + (DDLoggerNode *)nodeWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue level:(DDLogLevel)level {
- return [[DDLoggerNode alloc] initWithLogger:logger loggerQueue:loggerQueue level:level];
- }
- - (void)dealloc {
- #if !OS_OBJECT_USE_OBJC
- if (_loggerQueue) {
- dispatch_release(_loggerQueue);
- }
- #endif
- }
- @end
- #pragma mark -
- @implementation DDLogMessage
- - (instancetype)init {
- self = [super init];
- return self;
- }
- - (instancetype)initWithMessage:(NSString *)message
- level:(DDLogLevel)level
- flag:(DDLogFlag)flag
- context:(NSInteger)context
- file:(NSString *)file
- function:(NSString *)function
- line:(NSUInteger)line
- tag:(id)tag
- options:(DDLogMessageOptions)options
- timestamp:(NSDate *)timestamp {
- if ((self = [super init])) {
- BOOL copyMessage = (options & DDLogMessageDontCopyMessage) == 0;
- _message = copyMessage ? [message copy] : message;
- _level = level;
- _flag = flag;
- _context = context;
- BOOL copyFile = (options & DDLogMessageCopyFile) != 0;
- _file = copyFile ? [file copy] : file;
- BOOL copyFunction = (options & DDLogMessageCopyFunction) != 0;
- _function = copyFunction ? [function copy] : function;
- _line = line;
- _tag = tag;
- _options = options;
- _timestamp = timestamp ?: [NSDate new];
- __uint64_t tid;
- if (pthread_threadid_np(NULL, &tid) == 0) {
- _threadID = [[NSString alloc] initWithFormat:@"%llu", tid];
- } else {
- _threadID = @"missing threadId";
- }
- _threadName = NSThread.currentThread.name;
-
- _fileName = [_file lastPathComponent];
- NSUInteger dotLocation = [_fileName rangeOfString:@"." options:NSBackwardsSearch].location;
- if (dotLocation != NSNotFound)
- {
- _fileName = [_fileName substringToIndex:dotLocation];
- }
-
- _queueLabel = [[NSString alloc] initWithFormat:@"%s", dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
- }
- return self;
- }
- - (id)copyWithZone:(NSZone * __attribute__((unused)))zone {
- DDLogMessage *newMessage = [DDLogMessage new];
- newMessage->_message = _message;
- newMessage->_level = _level;
- newMessage->_flag = _flag;
- newMessage->_context = _context;
- newMessage->_file = _file;
- newMessage->_fileName = _fileName;
- newMessage->_function = _function;
- newMessage->_line = _line;
- newMessage->_tag = _tag;
- newMessage->_options = _options;
- newMessage->_timestamp = _timestamp;
- newMessage->_threadID = _threadID;
- newMessage->_threadName = _threadName;
- newMessage->_queueLabel = _queueLabel;
- return newMessage;
- }
- @end
- #pragma mark -
- @implementation DDAbstractLogger
- - (instancetype)init {
- if ((self = [super init])) {
- const char *loggerQueueName = NULL;
- if ([self respondsToSelector:@selector(loggerName)]) {
- loggerQueueName = [[self loggerName] UTF8String];
- }
- _loggerQueue = dispatch_queue_create(loggerQueueName, NULL);
-
-
-
-
-
-
-
-
-
-
-
-
-
- void *key = (__bridge void *)self;
- void *nonNullValue = (__bridge void *)self;
- dispatch_queue_set_specific(_loggerQueue, key, nonNullValue, NULL);
- }
- return self;
- }
- - (void)dealloc {
- #if !OS_OBJECT_USE_OBJC
- if (_loggerQueue) {
- dispatch_release(_loggerQueue);
- }
- #endif
- }
- - (void)logMessage:(DDLogMessage * __attribute__((unused)))logMessage {
-
- }
- - (id <DDLogFormatter>)logFormatter {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
- NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
- dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
- __block id <DDLogFormatter> result;
- dispatch_sync(globalLoggingQueue, ^{
- dispatch_sync(self->_loggerQueue, ^{
- result = self->_logFormatter;
- });
- });
- return result;
- }
- - (void)setLogFormatter:(id <DDLogFormatter>)logFormatter {
-
- NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
- NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
- dispatch_block_t block = ^{
- @autoreleasepool {
- if (self->_logFormatter != logFormatter) {
- if ([self->_logFormatter respondsToSelector:@selector(willRemoveFromLogger:)]) {
- [self->_logFormatter willRemoveFromLogger:self];
- }
- self->_logFormatter = logFormatter;
- if ([self->_logFormatter respondsToSelector:@selector(didAddToLogger:inQueue:)]) {
- [self->_logFormatter didAddToLogger:self inQueue:self->_loggerQueue];
- } else if ([self->_logFormatter respondsToSelector:@selector(didAddToLogger:)]) {
- [self->_logFormatter didAddToLogger:self];
- }
- }
- }
- };
- dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
- dispatch_async(globalLoggingQueue, ^{
- dispatch_async(self->_loggerQueue, block);
- });
- }
- - (dispatch_queue_t)loggerQueue {
- return _loggerQueue;
- }
- - (NSString *)loggerName {
- return NSStringFromClass([self class]);
- }
- - (BOOL)isOnGlobalLoggingQueue {
- return (dispatch_get_specific(GlobalLoggingQueueIdentityKey) != NULL);
- }
- - (BOOL)isOnInternalLoggerQueue {
- void *key = (__bridge void *)self;
- return (dispatch_get_specific(key) != NULL);
- }
- @end
- #pragma mark -
- @interface DDLoggerInformation()
- {
-
- @public
- id <DDLogger> _logger;
- DDLogLevel _level;
- }
- @end
- @implementation DDLoggerInformation
- - (instancetype)initWithLogger:(id <DDLogger>)logger andLevel:(DDLogLevel)level {
- if ((self = [super init])) {
- _logger = logger;
- _level = level;
- }
- return self;
- }
- + (DDLoggerInformation *)informationWithLogger:(id <DDLogger>)logger andLevel:(DDLogLevel)level {
- return [[DDLoggerInformation alloc] initWithLogger:logger andLevel:level];
- }
- @end
|