DDLog.h 31 KB

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