DDLog.m 43 KB

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