DDLog.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2023, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. #if !__has_feature(objc_arc)
  16. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  17. #endif
  18. #import <pthread.h>
  19. #import <objc/runtime.h>
  20. #import <sys/qos.h>
  21. #if TARGET_OS_IOS
  22. #import <UIKit/UIDevice.h>
  23. #import <UIKit/UIApplication.h>
  24. #elif !defined(DD_CLI) && __has_include(<AppKit/NSApplication.h>)
  25. #import <AppKit/NSApplication.h>
  26. #endif
  27. // Disable legacy macros
  28. #ifndef DD_LEGACY_MACROS
  29. #define DD_LEGACY_MACROS 0
  30. #endif
  31. #import <CocoaLumberjack/DDLog.h>
  32. // We probably shouldn't be using DDLog() statements within the DDLog implementation.
  33. // But we still want to leave our log statements for any future debugging,
  34. // and to allow other developers to trace the implementation (which is a great learning tool).
  35. //
  36. // So we use a primitive logging macro around NSLog.
  37. // We maintain the NS prefix on the macros to be explicit about the fact that we're using NSLog.
  38. #ifndef DD_DEBUG
  39. #define DD_DEBUG 0
  40. #endif
  41. #define NSLogDebug(frmt, ...) do{ if(DD_DEBUG) NSLog((frmt), ##__VA_ARGS__); } while(0)
  42. // The "global logging queue" refers to [DDLog loggingQueue].
  43. // It is the queue that all log statements go through.
  44. //
  45. // The logging queue sets a flag via dispatch_queue_set_specific using this key.
  46. // We can check for this key via dispatch_get_specific() to see if we're on the "global logging queue".
  47. static void *const GlobalLoggingQueueIdentityKey = (void *)&GlobalLoggingQueueIdentityKey;
  48. @interface DDLoggerNode : NSObject
  49. {
  50. // Direct accessors to be used only for performance
  51. @public
  52. id <DDLogger> _logger;
  53. DDLogLevel _level;
  54. dispatch_queue_t _loggerQueue;
  55. }
  56. @property (nonatomic, readonly) id <DDLogger> logger;
  57. @property (nonatomic, readonly) DDLogLevel level;
  58. @property (nonatomic, readonly) dispatch_queue_t loggerQueue;
  59. + (instancetype)nodeWithLogger:(id <DDLogger>)logger
  60. loggerQueue:(dispatch_queue_t)loggerQueue
  61. level:(DDLogLevel)level;
  62. @end
  63. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. #pragma mark -
  65. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. @interface DDLog ()
  67. // An array used to manage all the individual loggers.
  68. // The array is only modified on the loggingQueue/loggingThread.
  69. @property (nonatomic, strong) NSMutableArray *_loggers;
  70. @end
  71. @implementation DDLog
  72. // All logging statements are added to the same queue to ensure FIFO operation.
  73. static dispatch_queue_t _loggingQueue;
  74. // Individual loggers are executed concurrently per log statement.
  75. // Each logger has it's own associated queue, and a dispatch group is used for synchronization.
  76. static dispatch_group_t _loggingGroup;
  77. // Minor optimization for uniprocessor machines
  78. static NSUInteger _numProcessors;
  79. /**
  80. * Returns the singleton `DDLog`.
  81. * The instance is used by `DDLog` class methods.
  82. *
  83. * @return The singleton `DDLog`.
  84. */
  85. + (instancetype)sharedInstance {
  86. static id sharedInstance = nil;
  87. static dispatch_once_t onceToken;
  88. dispatch_once(&onceToken, ^{
  89. sharedInstance = [[self alloc] init];
  90. });
  91. return sharedInstance;
  92. }
  93. /**
  94. * The runtime sends initialize to each class in a program exactly one time just before the class,
  95. * or any class that inherits from it, is sent its first message from within the program. (Thus the
  96. * method may never be invoked if the class is not used.) The runtime sends the initialize message to
  97. * classes in a thread-safe manner. Superclasses receive this message before their subclasses.
  98. *
  99. * This method may also be called directly, hence the safety mechanism.
  100. **/
  101. + (void)initialize {
  102. static dispatch_once_t DDLogOnceToken;
  103. dispatch_once(&DDLogOnceToken, ^{
  104. NSLogDebug(@"DDLog: Using grand central dispatch");
  105. _loggingQueue = dispatch_queue_create("cocoa.lumberjack", NULL);
  106. _loggingGroup = dispatch_group_create();
  107. void *nonNullValue = GlobalLoggingQueueIdentityKey; // Whatever, just not null
  108. dispatch_queue_set_specific(_loggingQueue, GlobalLoggingQueueIdentityKey, nonNullValue, NULL);
  109. // Figure out how many processors are available.
  110. // This may be used later for an optimization on uniprocessor machines.
  111. _numProcessors = MAX([NSProcessInfo processInfo].processorCount, (NSUInteger) 1);
  112. NSLogDebug(@"DDLog: numProcessors = %@", @(_numProcessors));
  113. });
  114. }
  115. /**
  116. * The `DDLog` initializer.
  117. * Static variables are set only once.
  118. *
  119. * @return An initialized `DDLog` instance.
  120. */
  121. - (instancetype)init {
  122. self = [super init];
  123. if (self) {
  124. self._loggers = [[NSMutableArray alloc] initWithCapacity:4];
  125. #if TARGET_OS_IOS
  126. NSString *notificationName = UIApplicationWillTerminateNotification;
  127. #else
  128. NSString *notificationName = nil;
  129. // On Command Line Tool apps AppKit may not be available
  130. #if !defined(DD_CLI) && __has_include(<AppKit/NSApplication.h>)
  131. if (NSApp) {
  132. notificationName = NSApplicationWillTerminateNotification;
  133. }
  134. #endif
  135. if (!notificationName) {
  136. // If there is no NSApp -> we are running Command Line Tool app.
  137. // In this case terminate notification wouldn't be fired, so we use workaround.
  138. __weak __auto_type weakSelf = self;
  139. atexit_b (^{
  140. [weakSelf applicationWillTerminate:nil];
  141. });
  142. }
  143. #endif /* if TARGET_OS_IOS */
  144. if (notificationName) {
  145. [[NSNotificationCenter defaultCenter] addObserver:self
  146. selector:@selector(applicationWillTerminate:)
  147. name:notificationName
  148. object:nil];
  149. }
  150. }
  151. return self;
  152. }
  153. /**
  154. * Provides access to the logging queue.
  155. **/
  156. + (dispatch_queue_t)loggingQueue {
  157. return _loggingQueue;
  158. }
  159. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160. #pragma mark Notifications
  161. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. - (void)applicationWillTerminate:(NSNotification * __attribute__((unused)))notification {
  163. [self flushLog];
  164. }
  165. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. #pragma mark Logger Management
  167. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  168. + (void)addLogger:(id <DDLogger>)logger {
  169. [self.sharedInstance addLogger:logger];
  170. }
  171. - (void)addLogger:(id <DDLogger>)logger {
  172. [self addLogger:logger withLevel:DDLogLevelAll]; // DDLogLevelAll has all bits set
  173. }
  174. + (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level {
  175. [self.sharedInstance addLogger:logger withLevel:level];
  176. }
  177. - (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level {
  178. if (!logger) {
  179. return;
  180. }
  181. dispatch_async(_loggingQueue, ^{ @autoreleasepool {
  182. [self lt_addLogger:logger level:level];
  183. } });
  184. }
  185. + (void)removeLogger:(id <DDLogger>)logger {
  186. [self.sharedInstance removeLogger:logger];
  187. }
  188. - (void)removeLogger:(id <DDLogger>)logger {
  189. if (!logger) {
  190. return;
  191. }
  192. dispatch_async(_loggingQueue, ^{ @autoreleasepool {
  193. [self lt_removeLogger:logger];
  194. } });
  195. }
  196. + (void)removeAllLoggers {
  197. [self.sharedInstance removeAllLoggers];
  198. }
  199. - (void)removeAllLoggers {
  200. dispatch_async(_loggingQueue, ^{ @autoreleasepool {
  201. [self lt_removeAllLoggers];
  202. } });
  203. }
  204. + (NSArray<id<DDLogger>> *)allLoggers {
  205. return [self.sharedInstance allLoggers];
  206. }
  207. - (NSArray<id<DDLogger>> *)allLoggers {
  208. __block NSArray *theLoggers;
  209. dispatch_sync(_loggingQueue, ^{ @autoreleasepool {
  210. theLoggers = [self lt_allLoggers];
  211. } });
  212. return theLoggers;
  213. }
  214. + (NSArray<DDLoggerInformation *> *)allLoggersWithLevel {
  215. return [self.sharedInstance allLoggersWithLevel];
  216. }
  217. - (NSArray<DDLoggerInformation *> *)allLoggersWithLevel {
  218. __block NSArray *theLoggersWithLevel;
  219. dispatch_sync(_loggingQueue, ^{ @autoreleasepool {
  220. theLoggersWithLevel = [self lt_allLoggersWithLevel];
  221. } });
  222. return theLoggersWithLevel;
  223. }
  224. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. #pragma mark - Master Logging
  226. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  227. - (void)queueLogMessage:(DDLogMessage *)logMessage asynchronously:(BOOL)asyncFlag {
  228. // We have a tricky situation here...
  229. //
  230. // In the common case, when the queueSize is below the maximumQueueSize,
  231. // we want to simply enqueue the logMessage. And we want to do this as fast as possible,
  232. // which means we don't want to block and we don't want to use any locks.
  233. //
  234. // However, if the queueSize gets too big, we want to block.
  235. // But we have very strict requirements as to when we block, and how long we block.
  236. //
  237. // The following example should help illustrate our requirements:
  238. //
  239. // Imagine that the maximum queue size is configured to be 5,
  240. // and that there are already 5 log messages queued.
  241. // Let us call these 5 queued log messages A, B, C, D, and E. (A is next to be executed)
  242. //
  243. // Now if our thread issues a log statement (let us call the log message F),
  244. // it should block before the message is added to the queue.
  245. // Furthermore, it should be unblocked immediately after A has been unqueued.
  246. //
  247. // The requirements are strict in this manner so that we block only as long as necessary,
  248. // and so that blocked threads are unblocked in the order in which they were blocked.
  249. //
  250. // Returning to our previous example, let us assume that log messages A through E are still queued.
  251. // Our aforementioned thread is blocked attempting to queue log message F.
  252. // Now assume we have another separate thread that attempts to issue log message G.
  253. // It should block until log messages A and B have been unqueued.
  254. dispatch_block_t logBlock = ^{
  255. // We're now sure we won't overflow the queue.
  256. // It is time to queue our log message.
  257. @autoreleasepool {
  258. [self lt_log:logMessage];
  259. }
  260. };
  261. if (asyncFlag) {
  262. dispatch_async(_loggingQueue, logBlock);
  263. } else if (dispatch_get_specific(GlobalLoggingQueueIdentityKey)) {
  264. // We've logged an error message while on the logging queue...
  265. logBlock();
  266. } else {
  267. dispatch_sync(_loggingQueue, logBlock);
  268. }
  269. }
  270. + (void)log:(BOOL)asynchronous
  271. level:(DDLogLevel)level
  272. flag:(DDLogFlag)flag
  273. context:(NSInteger)context
  274. file:(const char *)file
  275. function:(const char *)function
  276. line:(NSUInteger)line
  277. tag:(id)tag
  278. format:(NSString *)format, ... {
  279. va_list args;
  280. if (format) {
  281. va_start(args, format);
  282. [self log:asynchronous
  283. level:level
  284. flag:flag
  285. context:context
  286. file:file
  287. function:function
  288. line:line
  289. tag:tag
  290. format:format
  291. args:args];
  292. va_end(args);
  293. }
  294. }
  295. - (void)log:(BOOL)asynchronous
  296. level:(DDLogLevel)level
  297. flag:(DDLogFlag)flag
  298. context:(NSInteger)context
  299. file:(const char *)file
  300. function:(const char *)function
  301. line:(NSUInteger)line
  302. tag:(id)tag
  303. format:(NSString *)format, ... {
  304. va_list args;
  305. if (format) {
  306. va_start(args, format);
  307. [self log:asynchronous
  308. level:level
  309. flag:flag
  310. context:context
  311. file:file
  312. function:function
  313. line:line
  314. tag:tag
  315. format:format
  316. args:args];
  317. va_end(args);
  318. }
  319. }
  320. + (void)log:(BOOL)asynchronous
  321. level:(DDLogLevel)level
  322. flag:(DDLogFlag)flag
  323. context:(NSInteger)context
  324. file:(const char *)file
  325. function:(const char *)function
  326. line:(NSUInteger)line
  327. tag:(id)tag
  328. format:(NSString *)format
  329. args:(va_list)args {
  330. [self.sharedInstance log:asynchronous level:level flag:flag context:context file:file function:function line:line tag:tag format:format args:args];
  331. }
  332. - (void)log:(BOOL)asynchronous
  333. level:(DDLogLevel)level
  334. flag:(DDLogFlag)flag
  335. context:(NSInteger)context
  336. file:(const char *)file
  337. function:(const char *)function
  338. line:(NSUInteger)line
  339. tag:(id)tag
  340. format:(NSString *)format
  341. args:(va_list)args {
  342. if (format) {
  343. // Nullity checks are handled by -initWithMessage:
  344. #pragma clang diagnostic push
  345. #pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
  346. DDLogMessage *logMessage = [[DDLogMessage alloc] initWithFormat:format
  347. args:args
  348. level:level
  349. flag:flag
  350. context:context
  351. file:@(file)
  352. function:@(function)
  353. line:line
  354. tag:tag
  355. options:(DDLogMessageOptions)0
  356. timestamp:nil];
  357. #pragma clang diagnostic pop
  358. [self queueLogMessage:logMessage asynchronously:asynchronous];
  359. }
  360. }
  361. + (void)log:(BOOL)asynchronous message:(DDLogMessage *)logMessage {
  362. [self.sharedInstance log:asynchronous message:logMessage];
  363. }
  364. - (void)log:(BOOL)asynchronous message:(DDLogMessage *)logMessage {
  365. [self queueLogMessage:logMessage asynchronously:asynchronous];
  366. }
  367. + (void)flushLog {
  368. [self.sharedInstance flushLog];
  369. }
  370. - (void)flushLog {
  371. NSAssert(!dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  372. @"This method shouldn't be run on the logging thread/queue that make flush fast enough");
  373. dispatch_sync(_loggingQueue, ^{ @autoreleasepool {
  374. [self lt_flush];
  375. } });
  376. }
  377. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  378. #pragma mark Registered Dynamic Logging
  379. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  380. + (BOOL)isRegisteredClass:(Class)class {
  381. SEL getterSel = @selector(ddLogLevel);
  382. SEL setterSel = @selector(ddSetLogLevel:);
  383. #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
  384. // Issue #6 (GoogleCode) - Crashes on iOS 4.2.1 and iPhone 4
  385. //
  386. // Crash caused by class_getClassMethod(2).
  387. //
  388. // "It's a bug with UIAccessibilitySafeCategory__NSObject so it didn't pop up until
  389. // users had VoiceOver enabled [...]. I was able to work around it by searching the
  390. // result of class_copyMethodList() instead of calling class_getClassMethod()"
  391. BOOL result = NO;
  392. unsigned int methodCount, i;
  393. Method *methodList = class_copyMethodList(object_getClass(class), &methodCount);
  394. if (methodList != NULL) {
  395. BOOL getterFound = NO;
  396. BOOL setterFound = NO;
  397. for (i = 0; i < methodCount; ++i) {
  398. SEL currentSel = method_getName(methodList[i]);
  399. if (currentSel == getterSel) {
  400. getterFound = YES;
  401. } else if (currentSel == setterSel) {
  402. setterFound = YES;
  403. }
  404. if (getterFound && setterFound) {
  405. result = YES;
  406. break;
  407. }
  408. }
  409. free(methodList);
  410. }
  411. return result;
  412. #else /* if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR */
  413. // Issue #24 (GitHub) - Crashing in in ARC+Simulator
  414. //
  415. // The method +[DDLog isRegisteredClass] will crash a project when using it with ARC + Simulator.
  416. // For running in the Simulator, it needs to execute the non-iOS code.
  417. Method getter = class_getClassMethod(class, getterSel);
  418. Method setter = class_getClassMethod(class, setterSel);
  419. if ((getter != NULL) && (setter != NULL)) {
  420. return YES;
  421. }
  422. return NO;
  423. #endif /* if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR */
  424. }
  425. + (NSArray *)registeredClasses {
  426. // We're going to get the list of all registered classes.
  427. // The Objective-C runtime library automatically registers all the classes defined in your source code.
  428. //
  429. // To do this we use the following method (documented in the Objective-C Runtime Reference):
  430. //
  431. // int objc_getClassList(Class *buffer, int bufferLen)
  432. //
  433. // We can pass (NULL, 0) to obtain the total number of
  434. // registered class definitions without actually retrieving any class definitions.
  435. // This allows us to allocate the minimum amount of memory needed for the application.
  436. NSUInteger numClasses = 0;
  437. Class *classes = NULL;
  438. while (numClasses == 0) {
  439. numClasses = (NSUInteger)MAX(objc_getClassList(NULL, 0), 0);
  440. // numClasses now tells us how many classes we have (but it might change)
  441. // So we can allocate our buffer, and get pointers to all the class definitions.
  442. NSUInteger bufferSize = numClasses;
  443. classes = numClasses ? (Class *)calloc(bufferSize, sizeof(Class)) : NULL;
  444. if (classes == NULL) {
  445. return @[]; //no memory or classes?
  446. }
  447. numClasses = (NSUInteger)MAX(objc_getClassList(classes, (int)bufferSize),0);
  448. if (numClasses > bufferSize || numClasses == 0) {
  449. //apparently more classes added between calls (or a problem); try again
  450. free(classes);
  451. classes = NULL;
  452. numClasses = 0;
  453. }
  454. }
  455. // We can now loop through the classes, and test each one to see if it is a DDLogging class.
  456. NSMutableArray *result = [NSMutableArray arrayWithCapacity:numClasses];
  457. for (NSUInteger i = 0; i < numClasses; i++) {
  458. Class class = classes[i];
  459. if ([self isRegisteredClass:class]) {
  460. [result addObject:class];
  461. }
  462. }
  463. free(classes);
  464. return result;
  465. }
  466. + (NSArray *)registeredClassNames {
  467. NSArray *registeredClasses = [self registeredClasses];
  468. NSMutableArray *result = [NSMutableArray arrayWithCapacity:[registeredClasses count]];
  469. for (Class class in registeredClasses) {
  470. [result addObject:NSStringFromClass(class)];
  471. }
  472. return result;
  473. }
  474. + (DDLogLevel)levelForClass:(Class)aClass {
  475. if ([self isRegisteredClass:aClass]) {
  476. return [aClass ddLogLevel];
  477. }
  478. return (DDLogLevel)-1;
  479. }
  480. + (DDLogLevel)levelForClassWithName:(NSString *)aClassName {
  481. Class aClass = NSClassFromString(aClassName);
  482. return [self levelForClass:aClass];
  483. }
  484. + (void)setLevel:(DDLogLevel)level forClass:(Class)aClass {
  485. if ([self isRegisteredClass:aClass]) {
  486. [aClass ddSetLogLevel:level];
  487. }
  488. }
  489. + (void)setLevel:(DDLogLevel)level forClassWithName:(NSString *)aClassName {
  490. Class aClass = NSClassFromString(aClassName);
  491. [self setLevel:level forClass:aClass];
  492. }
  493. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  494. #pragma mark Logging Thread
  495. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  496. - (void)lt_addLogger:(id <DDLogger>)logger level:(DDLogLevel)level {
  497. // Add to loggers array.
  498. // Need to create loggerQueue if loggerNode doesn't provide one.
  499. for (DDLoggerNode *node in self._loggers) {
  500. if (node->_logger == logger
  501. && node->_level == level) {
  502. // Exactly same logger already added, exit
  503. return;
  504. }
  505. }
  506. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  507. @"This method should only be run on the logging thread/queue");
  508. dispatch_queue_t loggerQueue = NULL;
  509. if ([logger respondsToSelector:@selector(loggerQueue)]) {
  510. // Logger may be providing its own queue
  511. loggerQueue = logger.loggerQueue;
  512. }
  513. if (loggerQueue == nil) {
  514. // Automatically create queue for the logger.
  515. // Use the logger name as the queue name if possible.
  516. const char *loggerQueueName = NULL;
  517. if ([logger respondsToSelector:@selector(loggerName)]) {
  518. loggerQueueName = logger.loggerName.UTF8String;
  519. }
  520. loggerQueue = dispatch_queue_create(loggerQueueName, NULL);
  521. }
  522. DDLoggerNode *loggerNode = [DDLoggerNode nodeWithLogger:logger loggerQueue:loggerQueue level:level];
  523. [self._loggers addObject:loggerNode];
  524. if ([logger respondsToSelector:@selector(didAddLoggerInQueue:)]) {
  525. dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
  526. [logger didAddLoggerInQueue:loggerNode->_loggerQueue];
  527. } });
  528. } else if ([logger respondsToSelector:@selector(didAddLogger)]) {
  529. dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
  530. [logger didAddLogger];
  531. } });
  532. }
  533. }
  534. - (void)lt_removeLogger:(id <DDLogger>)logger {
  535. // Find associated loggerNode in list of added loggers
  536. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  537. @"This method should only be run on the logging thread/queue");
  538. DDLoggerNode *loggerNode = nil;
  539. for (DDLoggerNode *node in self._loggers) {
  540. if (node->_logger == logger) {
  541. loggerNode = node;
  542. break;
  543. }
  544. }
  545. if (loggerNode == nil) {
  546. NSLogDebug(@"DDLog: Request to remove logger which wasn't added");
  547. return;
  548. }
  549. // Notify logger
  550. if ([logger respondsToSelector:@selector(willRemoveLogger)]) {
  551. dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
  552. [logger willRemoveLogger];
  553. } });
  554. }
  555. // Remove from loggers array
  556. [self._loggers removeObject:loggerNode];
  557. }
  558. - (void)lt_removeAllLoggers {
  559. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  560. @"This method should only be run on the logging thread/queue");
  561. // Notify all loggers
  562. for (DDLoggerNode *loggerNode in self._loggers) {
  563. if ([loggerNode->_logger respondsToSelector:@selector(willRemoveLogger)]) {
  564. dispatch_async(loggerNode->_loggerQueue, ^{ @autoreleasepool {
  565. [loggerNode->_logger willRemoveLogger];
  566. } });
  567. }
  568. }
  569. // Remove all loggers from array
  570. [self._loggers removeAllObjects];
  571. }
  572. - (NSArray *)lt_allLoggers {
  573. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  574. @"This method should only be run on the logging thread/queue");
  575. NSMutableArray *theLoggers = [NSMutableArray new];
  576. for (DDLoggerNode *loggerNode in self._loggers) {
  577. [theLoggers addObject:loggerNode->_logger];
  578. }
  579. return [theLoggers copy];
  580. }
  581. - (NSArray *)lt_allLoggersWithLevel {
  582. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  583. @"This method should only be run on the logging thread/queue");
  584. NSMutableArray *theLoggersWithLevel = [NSMutableArray new];
  585. for (DDLoggerNode *loggerNode in self._loggers) {
  586. [theLoggersWithLevel addObject:[DDLoggerInformation informationWithLogger:loggerNode->_logger
  587. andLevel:loggerNode->_level]];
  588. }
  589. return [theLoggersWithLevel copy];
  590. }
  591. - (void)lt_log:(DDLogMessage *)logMessage {
  592. // Execute the given log message on each of our loggers.
  593. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  594. @"This method should only be run on the logging thread/queue");
  595. if (_numProcessors > 1) {
  596. // Execute each logger concurrently, each within its own queue.
  597. // All blocks are added to same group.
  598. // After each block has been queued, wait on group.
  599. //
  600. // The waiting ensures that a slow logger doesn't end up with a large queue of pending log messages.
  601. // This would defeat the purpose of the efforts we made earlier to restrict the max queue size.
  602. for (DDLoggerNode *loggerNode in self._loggers) {
  603. // skip the loggers that shouldn't write this message based on the log level
  604. if (!(logMessage->_flag & loggerNode->_level)) {
  605. continue;
  606. }
  607. dispatch_group_async(_loggingGroup, loggerNode->_loggerQueue, ^{ @autoreleasepool {
  608. [loggerNode->_logger logMessage:logMessage];
  609. } });
  610. }
  611. dispatch_group_wait(_loggingGroup, DISPATCH_TIME_FOREVER);
  612. } else {
  613. // Execute each logger serially, each within its own queue.
  614. for (DDLoggerNode *loggerNode in self._loggers) {
  615. // skip the loggers that shouldn't write this message based on the log level
  616. if (!(logMessage->_flag & loggerNode->_level)) {
  617. continue;
  618. }
  619. #if DD_DEBUG
  620. // we must assure that we aren not on loggerNode->_loggerQueue.
  621. if (loggerNode->_loggerQueue == NULL) {
  622. // tell that we can't dispatch logger node on queue that is NULL.
  623. NSLogDebug(@"DDLog: current node has loggerQueue == NULL");
  624. }
  625. else {
  626. dispatch_async(loggerNode->_loggerQueue, ^{
  627. if (dispatch_get_specific(GlobalLoggingQueueIdentityKey)) {
  628. // tell that we somehow on logging queue?
  629. NSLogDebug(@"DDLog: current node has loggerQueue == globalLoggingQueue");
  630. }
  631. });
  632. }
  633. #endif
  634. // next, we must check that node is OK.
  635. dispatch_sync(loggerNode->_loggerQueue, ^{ @autoreleasepool {
  636. [loggerNode->_logger logMessage:logMessage];
  637. } });
  638. }
  639. }
  640. }
  641. - (void)lt_flush {
  642. // All log statements issued before the flush method was invoked have now been executed.
  643. //
  644. // Now we need to propagate the flush request to any loggers that implement the flush method.
  645. // This is designed for loggers that buffer IO.
  646. NSAssert(dispatch_get_specific(GlobalLoggingQueueIdentityKey),
  647. @"This method should only be run on the logging thread/queue");
  648. for (DDLoggerNode *loggerNode in self._loggers) {
  649. if ([loggerNode->_logger respondsToSelector:@selector(flush)]) {
  650. dispatch_group_async(_loggingGroup, loggerNode->_loggerQueue, ^{ @autoreleasepool {
  651. [loggerNode->_logger flush];
  652. } });
  653. }
  654. }
  655. dispatch_group_wait(_loggingGroup, DISPATCH_TIME_FOREVER);
  656. }
  657. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  658. #pragma mark Utilities
  659. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  660. NSString * __nullable DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy) {
  661. if (filePath == NULL) {
  662. return nil;
  663. }
  664. char *lastSlash = NULL;
  665. char *lastDot = NULL;
  666. char *p = (char *)filePath;
  667. while (*p != '\0') {
  668. if (*p == '/') {
  669. lastSlash = p;
  670. } else if (*p == '.') {
  671. lastDot = p;
  672. }
  673. p++;
  674. }
  675. char *subStr;
  676. NSUInteger subLen;
  677. if (lastSlash) {
  678. if (lastDot) {
  679. // lastSlash -> lastDot
  680. subStr = lastSlash + 1;
  681. subLen = (NSUInteger)(lastDot - subStr);
  682. } else {
  683. // lastSlash -> endOfString
  684. subStr = lastSlash + 1;
  685. subLen = (NSUInteger)(p - subStr);
  686. }
  687. } else {
  688. if (lastDot) {
  689. // startOfString -> lastDot
  690. subStr = (char *)filePath;
  691. subLen = (NSUInteger)(lastDot - subStr);
  692. } else {
  693. // startOfString -> endOfString
  694. subStr = (char *)filePath;
  695. subLen = (NSUInteger)(p - subStr);
  696. }
  697. }
  698. if (copy) {
  699. return [[NSString alloc] initWithBytes:subStr
  700. length:subLen
  701. encoding:NSUTF8StringEncoding];
  702. } else {
  703. // We can take advantage of the fact that __FILE__ is a string literal.
  704. // Specifically, we don't need to waste time copying the string.
  705. // We can just tell NSString to point to a range within the string literal.
  706. return [[NSString alloc] initWithBytesNoCopy:subStr
  707. length:subLen
  708. encoding:NSUTF8StringEncoding
  709. freeWhenDone:NO];
  710. }
  711. }
  712. @end
  713. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  714. #pragma mark -
  715. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  716. @implementation DDLoggerNode
  717. - (instancetype)initWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue level:(DDLogLevel)level {
  718. if ((self = [super init])) {
  719. _logger = logger;
  720. if (loggerQueue) {
  721. _loggerQueue = loggerQueue;
  722. #if !OS_OBJECT_USE_OBJC
  723. dispatch_retain(loggerQueue);
  724. #endif
  725. }
  726. _level = level;
  727. }
  728. return self;
  729. }
  730. + (instancetype)nodeWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue level:(DDLogLevel)level {
  731. return [[self alloc] initWithLogger:logger loggerQueue:loggerQueue level:level];
  732. }
  733. - (void)dealloc {
  734. #if !OS_OBJECT_USE_OBJC
  735. if (_loggerQueue) {
  736. dispatch_release(_loggerQueue);
  737. }
  738. #endif
  739. }
  740. @end
  741. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  742. #pragma mark -
  743. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  744. @implementation DDLogMessage
  745. - (instancetype)init {
  746. self = [super init];
  747. return self;
  748. }
  749. - (instancetype)initWithFormat:(NSString *)messageFormat
  750. formatted:(NSString *)message
  751. level:(DDLogLevel)level
  752. flag:(DDLogFlag)flag
  753. context:(NSInteger)context
  754. file:(NSString *)file
  755. function:(NSString *)function
  756. line:(NSUInteger)line
  757. tag:(id)tag
  758. options:(DDLogMessageOptions)options
  759. timestamp:(NSDate *)timestamp {
  760. NSParameterAssert(messageFormat);
  761. NSParameterAssert(message);
  762. NSParameterAssert(file);
  763. if ((self = [super init])) {
  764. BOOL copyMessage = (options & DDLogMessageDontCopyMessage) == 0;
  765. _messageFormat = copyMessage ? [messageFormat copy] : messageFormat;
  766. _message = copyMessage ? [message copy] : message;
  767. _level = level;
  768. _flag = flag;
  769. _context = context;
  770. BOOL copyFile = (options & DDLogMessageCopyFile) != 0;
  771. _file = copyFile ? [file copy] : file;
  772. BOOL copyFunction = (options & DDLogMessageCopyFunction) != 0;
  773. _function = copyFunction ? [function copy] : function;
  774. _line = line;
  775. _representedObject = tag;
  776. #if DD_LEGACY_MESSAGE_TAG
  777. #pragma clang diagnostic push
  778. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  779. _tag = tag;
  780. #pragma clang diagnostic pop
  781. #endif
  782. _options = options;
  783. _timestamp = timestamp ?: [NSDate date];
  784. __uint64_t tid;
  785. if (pthread_threadid_np(NULL, &tid) == 0) {
  786. _threadID = [[NSString alloc] initWithFormat:@"%llu", tid];
  787. } else {
  788. _threadID = @"N/A";
  789. }
  790. _threadName = NSThread.currentThread.name;
  791. // Get the file name without extension
  792. _fileName = [_file lastPathComponent];
  793. NSUInteger dotLocation = [_fileName rangeOfString:@"." options:NSBackwardsSearch].location;
  794. if (dotLocation != NSNotFound)
  795. {
  796. _fileName = [_fileName substringToIndex:dotLocation];
  797. }
  798. // Try to get the current queue's label
  799. _queueLabel = @(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL));
  800. _qos = (NSUInteger) qos_class_self();
  801. }
  802. return self;
  803. }
  804. - (instancetype)initWithFormat:(NSString *)messageFormat
  805. args:(va_list)messageArgs
  806. level:(DDLogLevel)level
  807. flag:(DDLogFlag)flag
  808. context:(NSInteger)context
  809. file:(NSString *)file
  810. function:(NSString *)function
  811. line:(NSUInteger)line
  812. tag:(id)tag
  813. options:(DDLogMessageOptions)options
  814. timestamp:(NSDate *)timestamp {
  815. BOOL copyMessage = (options & DDLogMessageDontCopyMessage) == 0;
  816. NSString *format = copyMessage ? [messageFormat copy] : messageFormat;
  817. self = [self initWithFormat:format
  818. formatted:[[NSString alloc] initWithFormat:format arguments:messageArgs]
  819. level:level
  820. flag:flag
  821. context:context
  822. file:file
  823. function:function
  824. line:line
  825. tag:tag
  826. options:options | DDLogMessageDontCopyMessage // we already did the copying if needed.
  827. timestamp:timestamp];
  828. return self;
  829. }
  830. - (instancetype)initWithMessage:(NSString *)message
  831. level:(DDLogLevel)level
  832. flag:(DDLogFlag)flag
  833. context:(NSInteger)context
  834. file:(NSString *)file
  835. function:(NSString *)function
  836. line:(NSUInteger)line
  837. tag:(id)tag
  838. options:(DDLogMessageOptions)options
  839. timestamp:(NSDate *)timestamp {
  840. self = [self initWithFormat:message
  841. formatted:message
  842. level:level
  843. flag:flag
  844. context:context
  845. file:file
  846. function:function
  847. line:line
  848. tag:tag
  849. options:options
  850. timestamp:timestamp];
  851. return self;
  852. }
  853. NS_INLINE BOOL _nullable_strings_equal(NSString* _Nullable lhs, NSString* _Nullable rhs)
  854. {
  855. if (lhs == nil) {
  856. if (rhs == nil)
  857. return YES;
  858. } else if (rhs != nil && [lhs isEqualToString:(NSString* _Nonnull)rhs])
  859. return YES;
  860. return NO;
  861. }
  862. - (BOOL)isEqual:(id)other {
  863. // Subclasses of NSObject should not call [super isEqual:] here.
  864. // See https://stackoverflow.com/questions/36593038/confused-about-the-default-isequal-and-hash-implements
  865. if (other == self) {
  866. return YES;
  867. } else if (!other || ![other isKindOfClass:[DDLogMessage class]]) {
  868. return NO;
  869. } else {
  870. __auto_type otherMsg = (DDLogMessage *)other;
  871. return [otherMsg->_message isEqualToString:_message]
  872. && [otherMsg->_messageFormat isEqualToString:_messageFormat]
  873. && otherMsg->_level == _level
  874. && otherMsg->_flag == _flag
  875. && otherMsg->_context == _context
  876. && [otherMsg->_file isEqualToString:_file]
  877. && _nullable_strings_equal(otherMsg->_function, _function)
  878. && otherMsg->_line == _line
  879. && (([otherMsg->_representedObject respondsToSelector:@selector(isEqual:)] && [otherMsg->_representedObject isEqual:_representedObject]) || otherMsg->_representedObject == _representedObject)
  880. && [otherMsg->_timestamp isEqualToDate:_timestamp]
  881. && [otherMsg->_threadID isEqualToString:_threadID] // If the thread ID is the same, the name will likely be the same as well.
  882. && [otherMsg->_queueLabel isEqualToString:_queueLabel]
  883. && otherMsg->_qos == _qos;
  884. }
  885. }
  886. - (NSUInteger)hash {
  887. // Subclasses of NSObject should not call [super hash] here.
  888. // See https://stackoverflow.com/questions/36593038/confused-about-the-default-isequal-and-hash-implements
  889. return _message.hash
  890. ^ _messageFormat.hash
  891. ^ _level
  892. ^ _flag
  893. ^ _context
  894. ^ _file.hash
  895. ^ _function.hash
  896. ^ _line
  897. ^ ([_representedObject respondsToSelector:@selector(hash)] ? [_representedObject hash] : (NSUInteger)_representedObject)
  898. ^ _timestamp.hash
  899. ^ _threadID.hash
  900. ^ _queueLabel.hash
  901. ^ _qos;
  902. }
  903. - (id)copyWithZone:(NSZone * __attribute__((unused)))zone {
  904. DDLogMessage *newMessage = [DDLogMessage new];
  905. newMessage->_messageFormat = _messageFormat;
  906. newMessage->_message = _message;
  907. newMessage->_level = _level;
  908. newMessage->_flag = _flag;
  909. newMessage->_context = _context;
  910. newMessage->_file = _file;
  911. newMessage->_fileName = _fileName;
  912. newMessage->_function = _function;
  913. newMessage->_line = _line;
  914. newMessage->_representedObject = _representedObject;
  915. #if DD_LEGACY_MESSAGE_TAG
  916. #pragma clang diagnostic push
  917. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  918. newMessage->_tag = _tag;
  919. #pragma clang diagnostic pop
  920. #endif
  921. newMessage->_options = _options;
  922. newMessage->_timestamp = _timestamp;
  923. newMessage->_threadID = _threadID;
  924. newMessage->_threadName = _threadName;
  925. newMessage->_queueLabel = _queueLabel;
  926. newMessage->_qos = _qos;
  927. return newMessage;
  928. }
  929. // ensure compatibility even when built with DD_LEGACY_MESSAGE_TAG to 0.
  930. - (id)tag {
  931. return _representedObject;
  932. }
  933. @end
  934. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  935. #pragma mark -
  936. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  937. @implementation DDAbstractLogger
  938. - (instancetype)init {
  939. if ((self = [super init])) {
  940. const char *loggerQueueName = NULL;
  941. if ([self respondsToSelector:@selector(loggerName)]) {
  942. loggerQueueName = self.loggerName.UTF8String;
  943. }
  944. _loggerQueue = dispatch_queue_create(loggerQueueName, NULL);
  945. // We're going to use dispatch_queue_set_specific() to "mark" our loggerQueue.
  946. // Later we can use dispatch_get_specific() to determine if we're executing on our loggerQueue.
  947. // The documentation states:
  948. //
  949. // > Keys are only compared as pointers and are never dereferenced.
  950. // > Thus, you can use a pointer to a static variable for a specific subsystem or
  951. // > any other value that allows you to identify the value uniquely.
  952. // > Specifying a pointer to a string constant is not recommended.
  953. //
  954. // So we're going to use the very convenient key of "self",
  955. // which also works when multiple logger classes extend this class, as each will have a different "self" key.
  956. //
  957. // This is used primarily for thread-safety assertions (via the isOnInternalLoggerQueue method below).
  958. void *key = (__bridge void *)self;
  959. void *nonNullValue = (__bridge void *)self;
  960. dispatch_queue_set_specific(_loggerQueue, key, nonNullValue, NULL);
  961. }
  962. return self;
  963. }
  964. - (void)dealloc {
  965. #if !OS_OBJECT_USE_OBJC
  966. if (_loggerQueue) {
  967. dispatch_release(_loggerQueue);
  968. }
  969. #endif
  970. }
  971. - (void)logMessage:(DDLogMessage * __attribute__((unused)))logMessage {
  972. // Override me
  973. }
  974. - (id <DDLogFormatter>)logFormatter {
  975. // This method must be thread safe and intuitive.
  976. // Therefore if somebody executes the following code:
  977. //
  978. // [logger setLogFormatter:myFormatter];
  979. // formatter = [logger logFormatter];
  980. //
  981. // They would expect formatter to equal myFormatter.
  982. // This functionality must be ensured by the getter and setter method.
  983. //
  984. // The thread safety must not come at a cost to the performance of the logMessage method.
  985. // This method is likely called sporadically, while the logMessage method is called repeatedly.
  986. // This means, the implementation of this method:
  987. // - Must NOT require the logMessage method to acquire a lock.
  988. // - Must NOT require the logMessage method to access an atomic property (also a lock of sorts).
  989. //
  990. // Thread safety is ensured by executing access to the formatter variable on the loggerQueue.
  991. // This is the same queue that the logMessage method operates on.
  992. //
  993. // Note: The last time I benchmarked the performance of direct access vs atomic property access,
  994. // direct access was over twice as fast on the desktop and over 6 times as fast on the iPhone.
  995. //
  996. // Furthermore, consider the following code:
  997. //
  998. // DDLogVerbose(@"log msg 1");
  999. // DDLogVerbose(@"log msg 2");
  1000. // [logger setFormatter:myFormatter];
  1001. // DDLogVerbose(@"log msg 3");
  1002. //
  1003. // Our intuitive requirement means that the new formatter will only apply to the 3rd log message.
  1004. // This must remain true even when using asynchronous logging.
  1005. // We must keep in mind the various queue's that are in play here:
  1006. //
  1007. // loggerQueue : Our own private internal queue that the logMessage method runs on.
  1008. // Operations are added to this queue from the global loggingQueue.
  1009. //
  1010. // globalLoggingQueue : The queue that all log messages go through before they arrive in our loggerQueue.
  1011. //
  1012. // All log statements go through the serial globalLoggingQueue before they arrive at our loggerQueue.
  1013. // Thus this method also goes through the serial globalLoggingQueue to ensure intuitive operation.
  1014. // IMPORTANT NOTE:
  1015. //
  1016. // Methods within the DDLogger implementation MUST access the formatter ivar directly.
  1017. // This method is designed explicitly for external access.
  1018. //
  1019. // Using "self." syntax to go through this method will cause immediate deadlock.
  1020. // This is the intended result. Fix it by accessing the ivar directly.
  1021. // Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.
  1022. NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
  1023. NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
  1024. dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
  1025. __block id <DDLogFormatter> result;
  1026. dispatch_sync(globalLoggingQueue, ^{
  1027. dispatch_sync(self->_loggerQueue, ^{
  1028. result = self->_logFormatter;
  1029. });
  1030. });
  1031. return result;
  1032. }
  1033. - (void)setLogFormatter:(id <DDLogFormatter>)logFormatter {
  1034. // The design of this method is documented extensively in the logFormatter message (above in code).
  1035. NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
  1036. NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
  1037. dispatch_block_t block = ^{
  1038. @autoreleasepool {
  1039. if (self->_logFormatter != logFormatter) {
  1040. if ([self->_logFormatter respondsToSelector:@selector(willRemoveFromLogger:)]) {
  1041. [self->_logFormatter willRemoveFromLogger:self];
  1042. }
  1043. self->_logFormatter = logFormatter;
  1044. if ([self->_logFormatter respondsToSelector:@selector(didAddToLogger:inQueue:)]) {
  1045. [self->_logFormatter didAddToLogger:self inQueue:self->_loggerQueue];
  1046. } else if ([self->_logFormatter respondsToSelector:@selector(didAddToLogger:)]) {
  1047. [self->_logFormatter didAddToLogger:self];
  1048. }
  1049. }
  1050. }
  1051. };
  1052. dispatch_async(DDLog.loggingQueue, ^{
  1053. dispatch_async(self->_loggerQueue, block);
  1054. });
  1055. }
  1056. - (dispatch_queue_t)loggerQueue {
  1057. return _loggerQueue;
  1058. }
  1059. - (NSString *)loggerName {
  1060. return NSStringFromClass([self class]);
  1061. }
  1062. - (BOOL)isOnGlobalLoggingQueue {
  1063. return (dispatch_get_specific(GlobalLoggingQueueIdentityKey) != NULL);
  1064. }
  1065. - (BOOL)isOnInternalLoggerQueue {
  1066. void *key = (__bridge void *)self;
  1067. return (dispatch_get_specific(key) != NULL);
  1068. }
  1069. @end
  1070. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1071. #pragma mark -
  1072. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1073. @interface DDLoggerInformation()
  1074. {
  1075. // Direct accessors to be used only for performance
  1076. @public
  1077. id <DDLogger> _logger;
  1078. DDLogLevel _level;
  1079. }
  1080. @end
  1081. @implementation DDLoggerInformation
  1082. - (instancetype)initWithLogger:(id <DDLogger>)logger andLevel:(DDLogLevel)level {
  1083. if ((self = [super init])) {
  1084. _logger = logger;
  1085. _level = level;
  1086. }
  1087. return self;
  1088. }
  1089. + (instancetype)informationWithLogger:(id <DDLogger>)logger andLevel:(DDLogLevel)level {
  1090. return [[self alloc] initWithLogger:logger andLevel:level];
  1091. }
  1092. @end