DDLog.h 24 KB

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