Nenhuma Descrição

DDLog.m 39KB

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