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.
 
 

546 lines
19 KiB

  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. typedef NS_OPTIONS(NSUInteger, DDLogFlag) {
  94. DDLogFlagError = (1 << 0), // 0...00001
  95. DDLogFlagWarning = (1 << 1), // 0...00010
  96. DDLogFlagInfo = (1 << 2), // 0...00100
  97. DDLogFlagDebug = (1 << 3), // 0...01000
  98. DDLogFlagVerbose = (1 << 4) // 0...10000
  99. };
  100. typedef NS_ENUM(NSUInteger, DDLogLevel) {
  101. DDLogLevelOff = 0,
  102. DDLogLevelError = (DDLogFlagError), // 0...00001
  103. DDLogLevelWarning = (DDLogLevelError | DDLogFlagWarning), // 0...00011
  104. DDLogLevelInfo = (DDLogLevelWarning | DDLogFlagInfo), // 0...00111
  105. DDLogLevelDebug = (DDLogLevelInfo | DDLogFlagDebug), // 0...01111
  106. DDLogLevelVerbose = (DDLogLevelDebug | DDLogFlagVerbose), // 0...11111
  107. DDLogLevelAll = NSUIntegerMax // 1111....11111 (DDLogLevelVerbose plus any other flags)
  108. };
  109. /**
  110. * The THIS_FILE macro gives you an NSString of the file name.
  111. * For simplicity and clarity, the file name does not include the full path or file extension.
  112. *
  113. * For example: DDLogWarn(@"%@: Unable to find thingy", THIS_FILE) -> @"MyViewController: Unable to find thingy"
  114. **/
  115. NSString * DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
  116. #define THIS_FILE (DDExtractFileNameWithoutExtension(__FILE__, NO))
  117. /**
  118. * The THIS_METHOD macro gives you the name of the current objective-c method.
  119. *
  120. * For example: DDLogWarn(@"%@ - Requires non-nil strings", THIS_METHOD) -> @"setMake:model: requires non-nil strings"
  121. *
  122. * Note: This does NOT work in straight C functions (non objective-c).
  123. * Instead you should use the predefined __FUNCTION__ macro.
  124. **/
  125. #define THIS_METHOD NSStringFromSelector(_cmd)
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. #pragma mark -
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129. @interface DDLog : NSObject
  130. /**
  131. * Provides access to the underlying logging queue.
  132. * This may be helpful to Logger classes for things like thread synchronization.
  133. **/
  134. + (dispatch_queue_t)loggingQueue;
  135. /**
  136. * Logging Primitive.
  137. *
  138. * This method is used by the macros above.
  139. * It is suggested you stick with the macros as they're easier to use.
  140. **/
  141. + (void)log:(BOOL)asynchronous
  142. level:(DDLogLevel)level
  143. flag:(DDLogFlag)flag
  144. context:(NSInteger)context
  145. file:(const char *)file
  146. function:(const char *)function
  147. line:(NSUInteger)line
  148. tag:(id)tag
  149. format:(NSString *)format, ... NS_FORMAT_FUNCTION(9,10);
  150. /**
  151. * Logging Primitive.
  152. *
  153. * This method can be used if you have a prepared va_list.
  154. **/
  155. + (void)log:(BOOL)asynchronous
  156. level:(DDLogLevel)level
  157. flag:(DDLogFlag)flag
  158. context:(NSInteger)context
  159. file:(const char *)file
  160. function:(const char *)function
  161. line:(NSUInteger)line
  162. tag:(id)tag
  163. format:(NSString *)format
  164. args:(va_list)argList;
  165. /**
  166. * Logging Primitive.
  167. **/
  168. + (void)log:(BOOL)asynchronous
  169. message:(NSString *)message
  170. level:(DDLogLevel)level
  171. flag:(DDLogFlag)flag
  172. context:(NSInteger)context
  173. file:(const char *)file
  174. function:(const char *)function
  175. line:(NSUInteger)line
  176. tag:(id)tag;
  177. /**
  178. * Logging Primitive.
  179. *
  180. * This method can be used if you manualy prepared DDLogMessage.
  181. **/
  182. + (void)log:(BOOL)asynchronous
  183. message:(DDLogMessage *)logMessage;
  184. /**
  185. * Since logging can be asynchronous, there may be times when you want to flush the logs.
  186. * The framework invokes this automatically when the application quits.
  187. **/
  188. + (void)flushLog;
  189. /**
  190. * Loggers
  191. *
  192. * In order for your log statements to go somewhere, you should create and add a logger.
  193. *
  194. * You can add multiple loggers in order to direct your log statements to multiple places.
  195. * And each logger can be configured separately.
  196. * So you could have, for example, verbose logging to the console, but a concise log file with only warnings & errors.
  197. **/
  198. /**
  199. * Adds the logger to the system.
  200. *
  201. * This is equivalent to invoking [DDLog addLogger:logger withLogLevel:DDLogLevelAll].
  202. **/
  203. + (void)addLogger:(id <DDLogger>)logger;
  204. /**
  205. * Adds the logger to the system.
  206. *
  207. * The level that you provide here is a preemptive filter (for performance).
  208. * That is, the level specified here will be used to filter out logMessages so that
  209. * the logger is never even invoked for the messages.
  210. *
  211. * More information:
  212. * When you issue a log statement, the logging framework iterates over each logger,
  213. * and checks to see if it should forward the logMessage to the logger.
  214. * This check is done using the level parameter passed to this method.
  215. *
  216. * For example:
  217. *
  218. * [DDLog addLogger:consoleLogger withLogLevel:DDLogLevelVerbose];
  219. * [DDLog addLogger:fileLogger withLogLevel:DDLogLevelWarning];
  220. *
  221. * DDLogError(@"oh no"); => gets forwarded to consoleLogger & fileLogger
  222. * DDLogInfo(@"hi"); => gets forwarded to consoleLogger only
  223. *
  224. * It is important to remember that Lumberjack uses a BITMASK.
  225. * Many developers & third party frameworks may define extra log levels & flags.
  226. * For example:
  227. *
  228. * #define SOME_FRAMEWORK_LOG_FLAG_TRACE (1 << 6) // 0...1000000
  229. *
  230. * So if you specify DDLogLevelVerbose to this method, you won't see the framework's trace messages.
  231. *
  232. * (SOME_FRAMEWORK_LOG_FLAG_TRACE & DDLogLevelVerbose) => (01000000 & 00011111) => NO
  233. *
  234. * Consider passing DDLogLevelAll to this method, which has all bits set.
  235. * You can also use the exclusive-or bitwise operator to get a bitmask that has all flags set,
  236. * except the ones you explicitly don't want. For example, if you wanted everything except verbose & debug:
  237. *
  238. * ((DDLogLevelAll ^ DDLogLevelVerbose) | DDLogLevelInfo)
  239. **/
  240. + (void)addLogger:(id <DDLogger>)logger withLevel:(DDLogLevel)level;
  241. + (void)removeLogger:(id <DDLogger>)logger;
  242. + (void)removeAllLoggers;
  243. + (NSArray *)allLoggers;
  244. /**
  245. * Registered Dynamic Logging
  246. *
  247. * These methods allow you to obtain a list of classes that are using registered dynamic logging,
  248. * and also provides methods to get and set their log level during run time.
  249. **/
  250. + (NSArray *)registeredClasses;
  251. + (NSArray *)registeredClassNames;
  252. + (DDLogLevel)levelForClass:(Class)aClass;
  253. + (DDLogLevel)levelForClassWithName:(NSString *)aClassName;
  254. + (void)setLevel:(DDLogLevel)level forClass:(Class)aClass;
  255. + (void)setLevel:(DDLogLevel)level forClassWithName:(NSString *)aClassName;
  256. @end
  257. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  258. #pragma mark -
  259. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  260. @protocol DDLogger <NSObject>
  261. - (void)logMessage:(DDLogMessage *)logMessage;
  262. /**
  263. * Formatters may optionally be added to any logger.
  264. *
  265. * If no formatter is set, the logger simply logs the message as it is given in logMessage,
  266. * or it may use its own built in formatting style.
  267. **/
  268. @property (nonatomic, strong) id <DDLogFormatter> logFormatter;
  269. @optional
  270. /**
  271. * Since logging is asynchronous, adding and removing loggers is also asynchronous.
  272. * In other words, the loggers are added and removed at appropriate times with regards to log messages.
  273. *
  274. * - Loggers will not receive log messages that were executed prior to when they were added.
  275. * - Loggers will not receive log messages that were executed after they were removed.
  276. *
  277. * These methods are executed in the logging thread/queue.
  278. * This is the same thread/queue that will execute every logMessage: invocation.
  279. * Loggers may use these methods for thread synchronization or other setup/teardown tasks.
  280. **/
  281. - (void)didAddLogger;
  282. - (void)willRemoveLogger;
  283. /**
  284. * Some loggers may buffer IO for optimization purposes.
  285. * For example, a database logger may only save occasionaly as the disk IO is slow.
  286. * In such loggers, this method should be implemented to flush any pending IO.
  287. *
  288. * This allows invocations of DDLog's flushLog method to be propogated to loggers that need it.
  289. *
  290. * Note that DDLog's flushLog method is invoked automatically when the application quits,
  291. * and it may be also invoked manually by the developer prior to application crashes, or other such reasons.
  292. **/
  293. - (void)flush;
  294. /**
  295. * Each logger is executed concurrently with respect to the other loggers.
  296. * Thus, a dedicated dispatch queue is used for each logger.
  297. * Logger implementations may optionally choose to provide their own dispatch queue.
  298. **/
  299. @property (nonatomic, DISPATCH_QUEUE_REFERENCE_TYPE, readonly) dispatch_queue_t loggerQueue;
  300. /**
  301. * If the logger implementation does not choose to provide its own queue,
  302. * one will automatically be created for it.
  303. * The created queue will receive its name from this method.
  304. * This may be helpful for debugging or profiling reasons.
  305. **/
  306. @property (nonatomic, readonly) NSString *loggerName;
  307. @end
  308. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  309. #pragma mark -
  310. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  311. @protocol DDLogFormatter <NSObject>
  312. @required
  313. /**
  314. * Formatters may optionally be added to any logger.
  315. * This allows for increased flexibility in the logging environment.
  316. * For example, log messages for log files may be formatted differently than log messages for the console.
  317. *
  318. * For more information about formatters, see the "Custom Formatters" page:
  319. * Documentation/CustomFormatters.md
  320. *
  321. * The formatter may also optionally filter the log message by returning nil,
  322. * in which case the logger will not log the message.
  323. **/
  324. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
  325. @optional
  326. /**
  327. * A single formatter instance can be added to multiple loggers.
  328. * These methods provides hooks to notify the formatter of when it's added/removed.
  329. *
  330. * This is primarily for thread-safety.
  331. * If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers.
  332. * Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter),
  333. * it could possibly use these hooks to switch to thread-safe versions of the code.
  334. **/
  335. - (void)didAddToLogger:(id <DDLogger>)logger;
  336. - (void)willRemoveFromLogger:(id <DDLogger>)logger;
  337. @end
  338. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  339. #pragma mark -
  340. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  341. @protocol DDRegisteredDynamicLogging
  342. /**
  343. * Implement these methods to allow a file's log level to be managed from a central location.
  344. *
  345. * This is useful if you'd like to be able to change log levels for various parts
  346. * of your code from within the running application.
  347. *
  348. * Imagine pulling up the settings for your application,
  349. * and being able to configure the logging level on a per file basis.
  350. *
  351. * The implementation can be very straight-forward:
  352. *
  353. * + (int)ddLogLevel
  354. * {
  355. * return ddLogLevel;
  356. * }
  357. *
  358. * + (void)ddSetLogLevel:(DDLogLevel)level
  359. * {
  360. * ddLogLevel = level;
  361. * }
  362. **/
  363. + (DDLogLevel)ddLogLevel;
  364. + (void)ddSetLogLevel:(DDLogLevel)level;
  365. @end
  366. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  367. #pragma mark -
  368. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  369. #ifndef NS_DESIGNATED_INITIALIZER
  370. #define NS_DESIGNATED_INITIALIZER
  371. #endif
  372. typedef NS_OPTIONS(NSInteger, DDLogMessageOptions) {
  373. DDLogMessageCopyFile = 1 << 0,
  374. DDLogMessageCopyFunction = 1 << 1
  375. };
  376. /**
  377. * The DDLogMessage class encapsulates information about the log message.
  378. * If you write custom loggers or formatters, you will be dealing with objects of this class.
  379. **/
  380. @interface DDLogMessage : NSObject <NSCopying>
  381. {
  382. // Direct accessors to be used only for performance
  383. @public
  384. NSString *_message;
  385. DDLogLevel _level;
  386. DDLogFlag _flag;
  387. NSInteger _context;
  388. NSString *_file;
  389. NSString *_fileName;
  390. NSString *_function;
  391. NSUInteger _line;
  392. id _tag;
  393. DDLogMessageOptions _options;
  394. NSDate *_timestamp;
  395. NSString *_threadID;
  396. NSString *_threadName;
  397. NSString *_queueLabel;
  398. }
  399. /**
  400. * Standard init method for a log message object.
  401. * Used by the logging primitives. (And the macros use the logging primitives.)
  402. *
  403. * If you find need to manually create logMessage objects, there is one thing you should be aware of:
  404. *
  405. * If no flags are passed, the method expects the file and function parameters to be string literals.
  406. * That is, it expects the given strings to exist for the duration of the object's lifetime,
  407. * and it expects the given strings to be immutable.
  408. * In other words, it does not copy these strings, it simply points to them.
  409. * This is due to the fact that __FILE__ and __FUNCTION__ are usually used to specify these parameters,
  410. * so it makes sense to optimize and skip the unnecessary allocations.
  411. * However, if you need them to be copied you may use the options parameter to specify this.
  412. * Options is a bitmask which supports DDLogMessageCopyFile and DDLogMessageCopyFunction.
  413. **/
  414. - (instancetype)initWithMessage:(NSString *)message
  415. level:(DDLogLevel)level
  416. flag:(DDLogFlag)flag
  417. context:(NSInteger)context
  418. file:(NSString *)file
  419. function:(NSString *)function
  420. line:(NSUInteger)line
  421. tag:(id)tag
  422. options:(DDLogMessageOptions)options
  423. timestamp:(NSDate *)timestamp;
  424. /**
  425. * Read-only properties
  426. **/
  427. @property (readonly, nonatomic) NSString *message;
  428. @property (readonly, nonatomic) DDLogLevel level;
  429. @property (readonly, nonatomic) DDLogFlag flag;
  430. @property (readonly, nonatomic) NSInteger context;
  431. @property (readonly, nonatomic) NSString *file;
  432. @property (readonly, nonatomic) NSString *fileName;
  433. @property (readonly, nonatomic) NSString *function;
  434. @property (readonly, nonatomic) NSUInteger line;
  435. @property (readonly, nonatomic) id tag;
  436. @property (readonly, nonatomic) DDLogMessageOptions options;
  437. @property (readonly, nonatomic) NSDate *timestamp;
  438. @property (readonly, nonatomic) NSString *threadID; // ID as it appears in NSLog calculated from the machThreadID
  439. @property (readonly, nonatomic) NSString *threadName;
  440. @property (readonly, nonatomic) NSString *queueLabel;
  441. @end
  442. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  443. #pragma mark -
  444. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  445. /**
  446. * The DDLogger protocol specifies that an optional formatter can be added to a logger.
  447. * Most (but not all) loggers will want to support formatters.
  448. *
  449. * However, writting getters and setters in a thread safe manner,
  450. * while still maintaining maximum speed for the logging process, is a difficult task.
  451. *
  452. * To do it right, the implementation of the getter/setter has strict requiremenets:
  453. * - Must NOT require the logMessage method to acquire a lock.
  454. * - Must NOT require the logMessage method to access an atomic property (also a lock of sorts).
  455. *
  456. * To simplify things, an abstract logger is provided that implements the getter and setter.
  457. *
  458. * Logger implementations may simply extend this class,
  459. * and they can ACCESS THE FORMATTER VARIABLE DIRECTLY from within their logMessage method!
  460. **/
  461. @interface DDAbstractLogger : NSObject <DDLogger>
  462. {
  463. // Direct accessors to be used only for performance
  464. @public
  465. id <DDLogFormatter> _logFormatter;
  466. dispatch_queue_t _loggerQueue;
  467. }
  468. @property (nonatomic, strong) id <DDLogFormatter> logFormatter;
  469. @property (nonatomic, DISPATCH_QUEUE_REFERENCE_TYPE) dispatch_queue_t loggerQueue;
  470. // For thread-safety assertions
  471. @property (nonatomic, readonly, getter=isOnGlobalLoggingQueue) BOOL onGlobalLoggingQueue;
  472. @property (nonatomic, readonly, getter=isOnInternalLoggerQueue) BOOL onInternalLoggerQueue;
  473. @end