DDLog.m 45 KB

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