DDLog.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2018, 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. #import <Foundation/Foundation.h>
  16. // Enable 1.9.x legacy macros if imported directly
  17. #ifndef DD_LEGACY_MACROS
  18. #define DD_LEGACY_MACROS 1
  19. #endif
  20. // DD_LEGACY_MACROS is checked in the file itself
  21. #import "DDLegacyMacros.h"
  22. // Names of loggers.
  23. #import "DDLoggerNames.h"
  24. #if OS_OBJECT_USE_OBJC
  25. #define DISPATCH_QUEUE_REFERENCE_TYPE strong
  26. #else
  27. #define DISPATCH_QUEUE_REFERENCE_TYPE assign
  28. #endif
  29. @class DDLogMessage;
  30. @class DDLoggerInformation;
  31. @protocol DDLogger;
  32. @protocol DDLogFormatter;
  33. /**
  34. * Define the standard options.
  35. *
  36. * We default to only 4 levels because it makes it easier for beginners
  37. * to make the transition to a logging framework.
  38. *
  39. * More advanced users may choose to completely customize the levels (and level names) to suite their needs.
  40. * For more information on this see the "Custom Log Levels" page:
  41. * Documentation/CustomLogLevels.md
  42. *
  43. * Advanced users may also notice that we're using a bitmask.
  44. * This is to allow for custom fine grained logging:
  45. * Documentation/FineGrainedLogging.md
  46. *
  47. * -- Flags --
  48. *
  49. * Typically you will use the LOG_LEVELS (see below), but the flags may be used directly in certain situations.
  50. * For example, say you have a lot of warning log messages, and you wanted to disable them.
  51. * However, you still needed to see your error and info log messages.
  52. * You could accomplish that with the following:
  53. *
  54. * static const DDLogLevel ddLogLevel = DDLogFlagError | DDLogFlagInfo;
  55. *
  56. * When LOG_LEVEL_DEF is defined as ddLogLevel.
  57. *
  58. * Flags may also be consulted when writing custom log formatters,
  59. * as the DDLogMessage class captures the individual flag that caused the log message to fire.
  60. *
  61. * -- Levels --
  62. *
  63. * Log levels are simply the proper bitmask of the flags.
  64. *
  65. * -- Booleans --
  66. *
  67. * The booleans may be used when your logging code involves more than one line.
  68. * For example:
  69. *
  70. * if (LOG_VERBOSE) {
  71. * for (id sprocket in sprockets)
  72. * DDLogVerbose(@"sprocket: %@", [sprocket description])
  73. * }
  74. *
  75. * -- Async --
  76. *
  77. * Defines the default asynchronous options.
  78. * The default philosophy for asynchronous logging is very simple:
  79. *
  80. * Log messages with errors should be executed synchronously.
  81. * After all, an error just occurred. The application could be unstable.
  82. *
  83. * All other log messages, such as debug output, are executed asynchronously.
  84. * After all, if it wasn't an error, then it was just informational output,
  85. * or something the application was easily able to recover from.
  86. *
  87. * -- Changes --
  88. *
  89. * You are strongly discouraged from modifying this file.
  90. * If you do, you make it more difficult on yourself to merge future bug fixes and improvements from the project.
  91. * Instead, create your own MyLogging.h or ApplicationNameLogging.h or CompanyLogging.h
  92. *
  93. * For an example of customizing your logging experience, see the "Custom Log Levels" page:
  94. * Documentation/CustomLogLevels.md
  95. **/
  96. /**
  97. * Flags accompany each log. They are used together with levels to filter out logs.
  98. */
  99. typedef NS_OPTIONS(NSUInteger, DDLogFlag){
  100. /**
  101. * 0...00001 DDLogFlagError
  102. */
  103. DDLogFlagError = (1 << 0),
  104. /**
  105. * 0...00010 DDLogFlagWarning
  106. */
  107. DDLogFlagWarning = (1 << 1),
  108. /**
  109. * 0...00100 DDLogFlagInfo
  110. */
  111. DDLogFlagInfo = (1 << 2),
  112. /**
  113. * 0...01000 DDLogFlagDebug
  114. */
  115. DDLogFlagDebug = (1 << 3),
  116. /**
  117. * 0...10000 DDLogFlagVerbose
  118. */
  119. DDLogFlagVerbose = (1 << 4)
  120. };
  121. /**
  122. * Log levels are used to filter out logs. Used together with flags.
  123. */
  124. typedef NS_ENUM(NSUInteger, DDLogLevel){
  125. /**
  126. * No logs
  127. */
  128. DDLogLevelOff = 0,
  129. /**
  130. * Error logs only
  131. */
  132. DDLogLevelError = (DDLogFlagError),
  133. /**
  134. * Error and warning logs
  135. */
  136. DDLogLevelWarning = (DDLogLevelError | DDLogFlagWarning),
  137. /**
  138. * Error, warning and info logs
  139. */
  140. DDLogLevelInfo = (DDLogLevelWarning | DDLogFlagInfo),
  141. /**
  142. * Error, warning, info and debug logs
  143. */
  144. DDLogLevelDebug = (DDLogLevelInfo | DDLogFlagDebug),
  145. /**
  146. * Error, warning, info, debug and verbose logs
  147. */
  148. DDLogLevelVerbose = (DDLogLevelDebug | DDLogFlagVerbose),
  149. /**
  150. * All logs (1...11111)
  151. */
  152. DDLogLevelAll = NSUIntegerMax
  153. };
  154. NS_ASSUME_NONNULL_BEGIN
  155. /**
  156. * Extracts just the file name, no path or extension
  157. *
  158. * @param filePath input file path
  159. * @param copy YES if we want the result to be copied
  160. *
  161. * @return the file name
  162. */
  163. FOUNDATION_EXTERN NSString * __nullable DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
  164. /**
  165. * The THIS_FILE macro gives you an NSString of the file name.
  166. * For simplicity and clarity, the file name does not include the full path or file extension.
  167. *
  168. * For example: DDLogWarn(@"%@: Unable to find thingy", THIS_FILE) -> @"MyViewController: Unable to find thingy"
  169. **/
  170. #define THIS_FILE (DDExtractFileNameWithoutExtension(__FILE__, NO))
  171. /**
  172. * The THIS_METHOD macro gives you the name of the current objective-c method.
  173. *
  174. * For example: DDLogWarn(@"%@ - Requires non-nil strings", THIS_METHOD) -> @"setMake:model: requires non-nil strings"
  175. *
  176. * Note: This does NOT work in straight C functions (non objective-c).
  177. * Instead you should use the predefined __FUNCTION__ macro.
  178. **/
  179. #define THIS_METHOD NSStringFromSelector(_cmd)
  180. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  181. #pragma mark -
  182. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. /**
  184. * The main class, exposes all logging mechanisms, loggers, ...
  185. * For most of the users, this class is hidden behind the logging functions like `DDLogInfo`
  186. */
  187. @interface DDLog : NSObject
  188. /**
  189. * Returns the singleton `DDLog`.
  190. * The instance is used by `DDLog` class methods.
  191. */
  192. @property (class, nonatomic, strong, readonly) DDLog *sharedInstance;
  193. /**
  194. * Provides access to the underlying logging queue.
  195. * This may be helpful to Logger classes for things like thread synchronization.
  196. **/
  197. @property (class, nonatomic, DISPATCH_QUEUE_REFERENCE_TYPE, readonly) dispatch_queue_t loggingQueue;
  198. /**
  199. * Logging Primitive.
  200. *
  201. * This method is used by the macros or logging functions.
  202. * It is suggested you stick with the macros as they're easier to use.
  203. *
  204. * @param asynchronous YES if the logging is done async, NO if you want to force sync
  205. * @param level the log level
  206. * @param flag the log flag
  207. * @param context the context (if any is defined)
  208. * @param file the current file
  209. * @param function the current function
  210. * @param line the current code line
  211. * @param tag potential tag
  212. * @param format the log format
  213. */
  214. + (void)log:(BOOL)asynchronous
  215. level:(DDLogLevel)level
  216. flag:(DDLogFlag)flag
  217. context:(NSInteger)context
  218. file:(const char *)file
  219. function:(const char *)function
  220. line:(NSUInteger)line
  221. tag:(id __nullable)tag
  222. format:(NSString *)format, ... NS_FORMAT_FUNCTION(9,10);
  223. /**
  224. * Logging Primitive.
  225. *
  226. * This method is used by the macros or logging functions.
  227. * It is suggested you stick with the macros as they're easier to use.
  228. *
  229. * @param asynchronous YES if the logging is done async, NO if you want to force sync
  230. * @param level the log level
  231. * @param flag the log flag
  232. * @param context the context (if any is defined)
  233. * @param file the current file
  234. * @param function the current function
  235. * @param line the current code line
  236. * @param tag potential tag
  237. * @param format the log format
  238. */
  239. - (void)log:(BOOL)asynchronous
  240. level:(DDLogLevel)level
  241. flag:(DDLogFlag)flag
  242. context:(NSInteger)context
  243. file:(const char *)file
  244. function:(const char *)function
  245. line:(NSUInteger)line
  246. tag:(id __nullable)tag
  247. format:(NSString *)format, ... NS_FORMAT_FUNCTION(9,10);
  248. /**
  249. * Logging Primitive.
  250. *
  251. * This method can be used if you have a prepared va_list.
  252. * Similar to `log:level:flag:context:file:function:line:tag:format:...`
  253. *
  254. * @param asynchronous YES if the logging is done async, NO if you want to force sync
  255. * @param level the log level
  256. * @param flag the log flag
  257. * @param context the context (if any is defined)
  258. * @param file the current file
  259. * @param function the current function
  260. * @param line the current code line
  261. * @param tag potential tag
  262. * @param format the log format
  263. * @param argList the arguments list as a va_list
  264. */
  265. + (void)log:(BOOL)asynchronous
  266. level:(DDLogLevel)level
  267. flag:(DDLogFlag)flag
  268. context:(NSInteger)context
  269. file:(const char *)file
  270. function:(const char *)function
  271. line:(NSUInteger)line
  272. tag:(id __nullable)tag
  273. format:(NSString *)format
  274. args:(va_list)argList NS_SWIFT_NAME(log(asynchronous:level:flag:context:file:function:line:tag:format:arguments:));
  275. /**
  276. * Logging Primitive.
  277. *
  278. * This method can be used if you have a prepared va_list.
  279. * Similar to `log:level:flag:context:file:function:line:tag:format:...`
  280. *
  281. * @param asynchronous YES if the logging is done async, NO if you want to force sync
  282. * @param level the log level
  283. * @param flag the log flag
  284. * @param context the context (if any is defined)
  285. * @param file the current file
  286. * @param function the current function
  287. * @param line the current code line
  288. * @param tag potential tag
  289. * @param format the log format
  290. * @param argList the arguments list as a va_list
  291. */
  292. - (void)log:(BOOL)asynchronous
  293. level:(DDLogLevel)level
  294. flag:(DDLogFlag)flag
  295. context:(NSInteger)context
  296. file:(const char *)file
  297. function:(const char *)function
  298. line:(NSUInteger)line
  299. tag:(id __nullable)tag
  300. format:(NSString *)format
  301. args:(va_list)argList NS_SWIFT_NAME(log(asynchronous:level:flag:context:file:function:line:tag:format:arguments:));
  302. /**
  303. * Logging Primitive.
  304. *
  305. * This method can be used if you manually prepared DDLogMessage.
  306. *
  307. * @param asynchronous YES if the logging is done async, NO if you want to force sync
  308. * @param logMessage the log message stored in a `DDLogMessage` model object
  309. */
  310. + (void)log:(BOOL)asynchronous
  311. message:(DDLogMessage *)logMessage NS_SWIFT_NAME(log(asynchronous:message:));
  312. /**
  313. * Logging Primitive.
  314. *
  315. * This method can be used if you manually prepared DDLogMessage.
  316. *
  317. * @param asynchronous YES if the logging is done async, NO if you want to force sync
  318. * @param logMessage the log message stored in a `DDLogMessage` model object
  319. */
  320. - (void)log:(BOOL)asynchronous
  321. message:(DDLogMessage *)logMessage NS_SWIFT_NAME(log(asynchronous:message:));
  322. /**
  323. * Since logging can be asynchronous, there may be times when you want to flush the logs.
  324. * The framework invokes this automatically when the application quits.
  325. **/
  326. + (void)flushLog;
  327. /**
  328. * Since logging can be asynchronous, there may be times when you want to flush the logs.
  329. * The framework invokes this automatically when the application quits.
  330. **/
  331. - (void)flushLog;
  332. /**
  333. * Loggers
  334. *
  335. * In order for your log statements to go somewhere, you should create and add a logger.
  336. *
  337. * You can add multiple loggers in order to direct your log statements to multiple places.
  338. * And each logger can be configured separately.
  339. * So you could have, for example, verbose logging to the console, but a concise log file with only warnings & errors.
  340. **/
  341. /**
  342. * Adds the logger to the system.
  343. *
  344. * This is equivalent to invoking `[DDLog addLogger:logger withLogLevel:DDLogLevelAll]`.
  345. **/
  346. + (void)addLogger:(id <DDLogger>)logger;
  347. /**
  348. * Adds the logger to the system.
  349. *
  350. * This is equivalent to invoking `[DDLog addLogger:logger withLogLevel:DDLogLevelAll]`.
  351. **/
  352. - (void)addLogger:(id <DDLogger>)logger;
  353. /**
  354. * Adds the logger to the system.
  355. *
  356. * The level that you provide here is a preemptive filter (for performance).
  357. * That is, the level specified here will be used to filter out logMessages so that
  358. * the logger is never even invoked for the messages.
  359. *
  360. * More information:
  361. * When you issue a log statement, the logging framework iterates over each logger,
  362. * and checks to see if it should forward the logMessage to the logger.
  363. * This check is done using the level parameter passed to this method.
  364. *
  365. * For example:
  366. *
  367. * `[DDLog addLogger:consoleLogger withLogLevel:DDLogLevelVerbose];`
  368. * `[DDLog addLogger:fileLogger withLogLevel:DDLogLevelWarning];`
  369. *
  370. * `DDLogError(@"oh no");` => gets forwarded to consoleLogger & fileLogger
  371. * `DDLogInfo(@"hi");` => gets forwarded to consoleLogger only
  372. *
  373. * It is important to remember that Lumberjack uses a BITMASK.
  374. * Many developers & third party frameworks may define extra log levels & flags.
  375. * For example:
  376. *
  377. * `#define SOME_FRAMEWORK_LOG_FLAG_TRACE (1 << 6) // 0...1000000`
  378. *
  379. * So if you specify `DDLogLevelVerbose` to this method, you won't see the framework's trace messages.
  380. *
  381. * `(SOME_FRAMEWORK_LOG_FLAG_TRACE & DDLogLevelVerbose) => (01000000 & 00011111) => NO`
  382. *
  383. * Consider passing `DDLogLevelAll` to this method, which has all bits set.
  384. * You can also use the exclusive-or bitwise operator to get a bitmask that has all flags set,
  385. * except the ones you explicitly don't want. For example, if you wanted everything except verbose & debug:
  386. *
  387. * `((DDLogLevelAll ^ DDLogLevelVerbose) | DDLogLevelInfo)`
  388. **/
  389. + (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level;
  390. /**
  391. * Adds the logger to the system.
  392. *
  393. * The level that you provide here is a preemptive filter (for performance).
  394. * That is, the level specified here will be used to filter out logMessages so that
  395. * the logger is never even invoked for the messages.
  396. *
  397. * More information:
  398. * When you issue a log statement, the logging framework iterates over each logger,
  399. * and checks to see if it should forward the logMessage to the logger.
  400. * This check is done using the level parameter passed to this method.
  401. *
  402. * For example:
  403. *
  404. * `[DDLog addLogger:consoleLogger withLogLevel:DDLogLevelVerbose];`
  405. * `[DDLog addLogger:fileLogger withLogLevel:DDLogLevelWarning];`
  406. *
  407. * `DDLogError(@"oh no");` => gets forwarded to consoleLogger & fileLogger
  408. * `DDLogInfo(@"hi");` => gets forwarded to consoleLogger only
  409. *
  410. * It is important to remember that Lumberjack uses a BITMASK.
  411. * Many developers & third party frameworks may define extra log levels & flags.
  412. * For example:
  413. *
  414. * `#define SOME_FRAMEWORK_LOG_FLAG_TRACE (1 << 6) // 0...1000000`
  415. *
  416. * So if you specify `DDLogLevelVerbose` to this method, you won't see the framework's trace messages.
  417. *
  418. * `(SOME_FRAMEWORK_LOG_FLAG_TRACE & DDLogLevelVerbose) => (01000000 & 00011111) => NO`
  419. *
  420. * Consider passing `DDLogLevelAll` to this method, which has all bits set.
  421. * You can also use the exclusive-or bitwise operator to get a bitmask that has all flags set,
  422. * except the ones you explicitly don't want. For example, if you wanted everything except verbose & debug:
  423. *
  424. * `((DDLogLevelAll ^ DDLogLevelVerbose) | DDLogLevelInfo)`
  425. **/
  426. - (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level;
  427. /**
  428. * Remove the logger from the system
  429. */
  430. + (void)removeLogger:(id <DDLogger>)logger;
  431. /**
  432. * Remove the logger from the system
  433. */
  434. - (void)removeLogger:(id <DDLogger>)logger;
  435. /**
  436. * Remove all the current loggers
  437. */
  438. + (void)removeAllLoggers;
  439. /**
  440. * Remove all the current loggers
  441. */
  442. - (void)removeAllLoggers;
  443. /**
  444. * Return all the current loggers
  445. */
  446. @property (class, nonatomic, copy, readonly) NSArray<id<DDLogger>> *allLoggers;
  447. /**
  448. * Return all the current loggers
  449. */
  450. @property (nonatomic, copy, readonly) NSArray<id<DDLogger>> *allLoggers;
  451. /**
  452. * Return all the current loggers with their level (aka DDLoggerInformation).
  453. */
  454. @property (class, nonatomic, copy, readonly) NSArray<DDLoggerInformation *> *allLoggersWithLevel;
  455. /**
  456. * Return all the current loggers with their level (aka DDLoggerInformation).
  457. */
  458. @property (nonatomic, copy, readonly) NSArray<DDLoggerInformation *> *allLoggersWithLevel;
  459. /**
  460. * Registered Dynamic Logging
  461. *
  462. * These methods allow you to obtain a list of classes that are using registered dynamic logging,
  463. * and also provides methods to get and set their log level during run time.
  464. **/
  465. /**
  466. * Returns an array with the classes that are using registered dynamic logging
  467. */
  468. @property (class, nonatomic, copy, readonly) NSArray<Class> *registeredClasses;
  469. /**
  470. * Returns an array with the classes names that are using registered dynamic logging
  471. */
  472. @property (class, nonatomic, copy, readonly) NSArray<NSString*> *registeredClassNames;
  473. /**
  474. * Returns the current log level for a certain class
  475. *
  476. * @param aClass `Class` param
  477. */
  478. + (DDLogLevel)levelForClass:(Class)aClass;
  479. /**
  480. * Returns the current log level for a certain class
  481. *
  482. * @param aClassName string param
  483. */
  484. + (DDLogLevel)levelForClassWithName:(NSString *)aClassName;
  485. /**
  486. * Set the log level for a certain class
  487. *
  488. * @param level the new level
  489. * @param aClass `Class` param
  490. */
  491. + (void)setLevel:(DDLogLevel)level forClass:(Class)aClass;
  492. /**
  493. * Set the log level for a certain class
  494. *
  495. * @param level the new level
  496. * @param aClassName string param
  497. */
  498. + (void)setLevel:(DDLogLevel)level forClassWithName:(NSString *)aClassName;
  499. @end
  500. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  501. #pragma mark -
  502. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  503. /**
  504. * This protocol describes a basic logger behavior.
  505. * Basically, it can log messages, store a logFormatter plus a bunch of optional behaviors.
  506. * (i.e. flush, get its loggerQueue, get its name, ...
  507. */
  508. @protocol DDLogger <NSObject>
  509. /**
  510. * The log message method
  511. *
  512. * @param logMessage the message (model)
  513. */
  514. - (void)logMessage:(DDLogMessage *)logMessage NS_SWIFT_NAME(log(message:));
  515. /**
  516. * Formatters may optionally be added to any logger.
  517. *
  518. * If no formatter is set, the logger simply logs the message as it is given in logMessage,
  519. * or it may use its own built in formatting style.
  520. **/
  521. @property (nonatomic, strong) id <DDLogFormatter> logFormatter;
  522. @optional
  523. /**
  524. * Since logging is asynchronous, adding and removing loggers is also asynchronous.
  525. * In other words, the loggers are added and removed at appropriate times with regards to log messages.
  526. *
  527. * - Loggers will not receive log messages that were executed prior to when they were added.
  528. * - Loggers will not receive log messages that were executed after they were removed.
  529. *
  530. * These methods are executed in the logging thread/queue.
  531. * This is the same thread/queue that will execute every logMessage: invocation.
  532. * Loggers may use these methods for thread synchronization or other setup/teardown tasks.
  533. **/
  534. - (void)didAddLogger;
  535. /**
  536. * Since logging is asynchronous, adding and removing loggers is also asynchronous.
  537. * In other words, the loggers are added and removed at appropriate times with regards to log messages.
  538. *
  539. * - Loggers will not receive log messages that were executed prior to when they were added.
  540. * - Loggers will not receive log messages that were executed after they were removed.
  541. *
  542. * These methods are executed in the logging thread/queue given in parameter.
  543. * This is the same thread/queue that will execute every logMessage: invocation.
  544. * Loggers may use the queue parameter to set specific values on the queue with dispatch_set_specific() function.
  545. **/
  546. - (void)didAddLoggerInQueue:(dispatch_queue_t)queue;
  547. /**
  548. * See the above description for `didAddLogger`
  549. */
  550. - (void)willRemoveLogger;
  551. /**
  552. * Some loggers may buffer IO for optimization purposes.
  553. * For example, a database logger may only save occasionally as the disk IO is slow.
  554. * In such loggers, this method should be implemented to flush any pending IO.
  555. *
  556. * This allows invocations of DDLog's flushLog method to be propogated to loggers that need it.
  557. *
  558. * Note that DDLog's flushLog method is invoked automatically when the application quits,
  559. * and it may be also invoked manually by the developer prior to application crashes, or other such reasons.
  560. **/
  561. - (void)flush;
  562. /**
  563. * Each logger is executed concurrently with respect to the other loggers.
  564. * Thus, a dedicated dispatch queue is used for each logger.
  565. * Logger implementations may optionally choose to provide their own dispatch queue.
  566. **/
  567. @property (nonatomic, DISPATCH_QUEUE_REFERENCE_TYPE, readonly) dispatch_queue_t loggerQueue;
  568. /**
  569. * If the logger implementation does not choose to provide its own queue,
  570. * one will automatically be created for it.
  571. * The created queue will receive its name from this method.
  572. * This may be helpful for debugging or profiling reasons.
  573. **/
  574. @property (copy, nonatomic, readonly) DDLoggerName loggerName;
  575. @end
  576. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  577. #pragma mark -
  578. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  579. /**
  580. * This protocol describes the behavior of a log formatter
  581. */
  582. @protocol DDLogFormatter <NSObject>
  583. @required
  584. /**
  585. * Formatters may optionally be added to any logger.
  586. * This allows for increased flexibility in the logging environment.
  587. * For example, log messages for log files may be formatted differently than log messages for the console.
  588. *
  589. * For more information about formatters, see the "Custom Formatters" page:
  590. * Documentation/CustomFormatters.md
  591. *
  592. * The formatter may also optionally filter the log message by returning nil,
  593. * in which case the logger will not log the message.
  594. **/
  595. - (NSString * __nullable)formatLogMessage:(DDLogMessage *)logMessage NS_SWIFT_NAME(format(message:));
  596. @optional
  597. /**
  598. * A single formatter instance can be added to multiple loggers.
  599. * These methods provides hooks to notify the formatter of when it's added/removed.
  600. *
  601. * This is primarily for thread-safety.
  602. * If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers.
  603. * Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter),
  604. * it could possibly use these hooks to switch to thread-safe versions of the code.
  605. **/
  606. - (void)didAddToLogger:(id <DDLogger>)logger;
  607. /**
  608. * A single formatter instance can be added to multiple loggers.
  609. * These methods provides hooks to notify the formatter of when it's added/removed.
  610. *
  611. * This is primarily for thread-safety.
  612. * If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers.
  613. * Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter),
  614. * it could possibly use these hooks to switch to thread-safe versions of the code or use dispatch_set_specific()
  615. .* to add its own specific values.
  616. **/
  617. - (void)didAddToLogger:(id <DDLogger>)logger inQueue:(dispatch_queue_t)queue;
  618. /**
  619. * See the above description for `didAddToLogger:`
  620. */
  621. - (void)willRemoveFromLogger:(id <DDLogger>)logger;
  622. @end
  623. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  624. #pragma mark -
  625. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  626. /**
  627. * This protocol describes a dynamic logging component
  628. */
  629. @protocol DDRegisteredDynamicLogging
  630. /**
  631. * Implement these methods to allow a file's log level to be managed from a central location.
  632. *
  633. * This is useful if you'd like to be able to change log levels for various parts
  634. * of your code from within the running application.
  635. *
  636. * Imagine pulling up the settings for your application,
  637. * and being able to configure the logging level on a per file basis.
  638. *
  639. * The implementation can be very straight-forward:
  640. *
  641. * ```
  642. * + (int)ddLogLevel
  643. * {
  644. * return ddLogLevel;
  645. * }
  646. *
  647. * + (void)ddSetLogLevel:(DDLogLevel)level
  648. * {
  649. * ddLogLevel = level;
  650. * }
  651. * ```
  652. **/
  653. @property (class, nonatomic, readwrite, setter=ddSetLogLevel:) DDLogLevel ddLogLevel;
  654. @end
  655. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  656. #pragma mark -
  657. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  658. #ifndef NS_DESIGNATED_INITIALIZER
  659. #define NS_DESIGNATED_INITIALIZER
  660. #endif
  661. /**
  662. * Log message options, allow copying certain log elements
  663. */
  664. typedef NS_OPTIONS(NSInteger, DDLogMessageOptions){
  665. /**
  666. * Use this to use a copy of the file path
  667. */
  668. DDLogMessageCopyFile = 1 << 0,
  669. /**
  670. * Use this to use a copy of the function name
  671. */
  672. DDLogMessageCopyFunction = 1 << 1,
  673. /**
  674. * Use this to use avoid a copy of the message
  675. */
  676. DDLogMessageDontCopyMessage = 1 << 2
  677. };
  678. /**
  679. * The `DDLogMessage` class encapsulates information about the log message.
  680. * If you write custom loggers or formatters, you will be dealing with objects of this class.
  681. **/
  682. @interface DDLogMessage : NSObject <NSCopying>
  683. {
  684. // Direct accessors to be used only for performance
  685. @public
  686. NSString *_message;
  687. DDLogLevel _level;
  688. DDLogFlag _flag;
  689. NSInteger _context;
  690. NSString *_file;
  691. NSString *_fileName;
  692. NSString *_function;
  693. NSUInteger _line;
  694. id _tag;
  695. DDLogMessageOptions _options;
  696. NSDate *_timestamp;
  697. NSString *_threadID;
  698. NSString *_threadName;
  699. NSString *_queueLabel;
  700. }
  701. /**
  702. * Default `init` for empty messages.
  703. */
  704. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  705. /**
  706. * Standard init method for a log message object.
  707. * Used by the logging primitives. (And the macros use the logging primitives.)
  708. *
  709. * If you find need to manually create logMessage objects, there is one thing you should be aware of:
  710. *
  711. * If no flags are passed, the method expects the file and function parameters to be string literals.
  712. * That is, it expects the given strings to exist for the duration of the object's lifetime,
  713. * and it expects the given strings to be immutable.
  714. * In other words, it does not copy these strings, it simply points to them.
  715. * This is due to the fact that __FILE__ and __FUNCTION__ are usually used to specify these parameters,
  716. * so it makes sense to optimize and skip the unnecessary allocations.
  717. * However, if you need them to be copied you may use the options parameter to specify this.
  718. *
  719. * @param message the message
  720. * @param level the log level
  721. * @param flag the log flag
  722. * @param context the context (if any is defined)
  723. * @param file the current file
  724. * @param function the current function
  725. * @param line the current code line
  726. * @param tag potential tag
  727. * @param options a bitmask which supports DDLogMessageCopyFile and DDLogMessageCopyFunction.
  728. * @param timestamp the log timestamp
  729. *
  730. * @return a new instance of a log message model object
  731. */
  732. - (instancetype)initWithMessage:(NSString *)message
  733. level:(DDLogLevel)level
  734. flag:(DDLogFlag)flag
  735. context:(NSInteger)context
  736. file:(NSString *)file
  737. function:(NSString * __nullable)function
  738. line:(NSUInteger)line
  739. tag:(id __nullable)tag
  740. options:(DDLogMessageOptions)options
  741. timestamp:(NSDate * __nullable)timestamp NS_DESIGNATED_INITIALIZER;
  742. /**
  743. * Read-only properties
  744. **/
  745. /**
  746. * The log message
  747. */
  748. @property (readonly, nonatomic) NSString *message;
  749. @property (readonly, nonatomic) DDLogLevel level;
  750. @property (readonly, nonatomic) DDLogFlag flag;
  751. @property (readonly, nonatomic) NSInteger context;
  752. @property (readonly, nonatomic) NSString *file;
  753. @property (readonly, nonatomic) NSString *fileName;
  754. @property (readonly, nonatomic) NSString * __nullable function;
  755. @property (readonly, nonatomic) NSUInteger line;
  756. @property (readonly, nonatomic) id __nullable tag;
  757. @property (readonly, nonatomic) DDLogMessageOptions options;
  758. @property (readonly, nonatomic) NSDate *timestamp;
  759. @property (readonly, nonatomic) NSString *threadID; // ID as it appears in NSLog calculated from the machThreadID
  760. @property (readonly, nonatomic) NSString *threadName;
  761. @property (readonly, nonatomic) NSString *queueLabel;
  762. @end
  763. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  764. #pragma mark -
  765. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  766. /**
  767. * The `DDLogger` protocol specifies that an optional formatter can be added to a logger.
  768. * Most (but not all) loggers will want to support formatters.
  769. *
  770. * However, writing getters and setters in a thread safe manner,
  771. * while still maintaining maximum speed for the logging process, is a difficult task.
  772. *
  773. * To do it right, the implementation of the getter/setter has strict requirements:
  774. * - Must NOT require the `logMessage:` method to acquire a lock.
  775. * - Must NOT require the `logMessage:` method to access an atomic property (also a lock of sorts).
  776. *
  777. * To simplify things, an abstract logger is provided that implements the getter and setter.
  778. *
  779. * Logger implementations may simply extend this class,
  780. * and they can ACCESS THE FORMATTER VARIABLE DIRECTLY from within their `logMessage:` method!
  781. **/
  782. @interface DDAbstractLogger : NSObject <DDLogger>
  783. {
  784. // Direct accessors to be used only for performance
  785. @public
  786. id <DDLogFormatter> _logFormatter;
  787. dispatch_queue_t _loggerQueue;
  788. }
  789. @property (nonatomic, strong, nullable) id <DDLogFormatter> logFormatter;
  790. @property (nonatomic, DISPATCH_QUEUE_REFERENCE_TYPE) dispatch_queue_t loggerQueue;
  791. // For thread-safety assertions
  792. /**
  793. * Return YES if the current logger uses a global queue for logging
  794. */
  795. @property (nonatomic, readonly, getter=isOnGlobalLoggingQueue) BOOL onGlobalLoggingQueue;
  796. /**
  797. * Return YES if the current logger uses the internal designated queue for logging
  798. */
  799. @property (nonatomic, readonly, getter=isOnInternalLoggerQueue) BOOL onInternalLoggerQueue;
  800. @end
  801. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  802. #pragma mark -
  803. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  804. @interface DDLoggerInformation : NSObject
  805. @property (nonatomic, readonly) id <DDLogger> logger;
  806. @property (nonatomic, readonly) DDLogLevel level;
  807. + (DDLoggerInformation *)informationWithLogger:(id <DDLogger>)logger
  808. andLevel:(DDLogLevel)level;
  809. @end
  810. NS_ASSUME_NONNULL_END