洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

860 lines
30 KiB

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