DDLog.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #import <Foundation/Foundation.h>
  2. /**
  3. * Welcome to Cocoa Lumberjack!
  4. *
  5. * The project page has a wealth of documentation if you have any questions.
  6. * https://github.com/robbiehanson/CocoaLumberjack
  7. *
  8. * If you're new to the project you may wish to read the "Getting Started" wiki.
  9. * https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted
  10. *
  11. * Otherwise, here is a quick refresher.
  12. * There are three steps to using the macros:
  13. *
  14. * Step 1:
  15. * Import the header in your implementation file:
  16. *
  17. * #import "DDLog.h"
  18. *
  19. * Step 2:
  20. * Define your logging level in your implementation file:
  21. *
  22. * // Log levels: off, error, warn, info, verbose
  23. * static const int ddLogLevel = LOG_LEVEL_VERBOSE;
  24. *
  25. * Step 3:
  26. * Replace your NSLog statements with DDLog statements according to the severity of the message.
  27. *
  28. * NSLog(@"Fatal error, no dohickey found!"); -> DDLogError(@"Fatal error, no dohickey found!");
  29. *
  30. * DDLog works exactly the same as NSLog.
  31. * This means you can pass it multiple variables just like NSLog.
  32. **/
  33. @class DDLogMessage;
  34. @protocol DDLogger;
  35. @protocol DDLogFormatter;
  36. /**
  37. * This is the single macro that all other macros below compile into.
  38. * This big multiline macro makes all the other macros easier to read.
  39. **/
  40. #define LOG_MACRO(isAsynchronous, lvl, flg, ctx, atag, fnct, frmt, ...) \
  41. [DDLog log:isAsynchronous \
  42. level:lvl \
  43. flag:flg \
  44. context:ctx \
  45. file:__FILE__ \
  46. function:fnct \
  47. line:__LINE__ \
  48. tag:atag \
  49. format:(frmt), ##__VA_ARGS__]
  50. /**
  51. * Define the Objective-C and C versions of the macro.
  52. * These automatically inject the proper function name for either an objective-c method or c function.
  53. *
  54. * We also define shorthand versions for asynchronous and synchronous logging.
  55. **/
  56. #define LOG_OBJC_MACRO(async, lvl, flg, ctx, frmt, ...) \
  57. LOG_MACRO(async, lvl, flg, ctx, nil, sel_getName(_cmd), frmt, ##__VA_ARGS__)
  58. #define LOG_C_MACRO(async, lvl, flg, ctx, frmt, ...) \
  59. LOG_MACRO(async, lvl, flg, ctx, nil, __FUNCTION__, frmt, ##__VA_ARGS__)
  60. #define SYNC_LOG_OBJC_MACRO(lvl, flg, ctx, frmt, ...) \
  61. LOG_OBJC_MACRO( NO, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  62. #define ASYNC_LOG_OBJC_MACRO(lvl, flg, ctx, frmt, ...) \
  63. LOG_OBJC_MACRO(YES, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  64. #define SYNC_LOG_C_MACRO(lvl, flg, ctx, frmt, ...) \
  65. LOG_C_MACRO( NO, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  66. #define ASYNC_LOG_C_MACRO(lvl, flg, ctx, frmt, ...) \
  67. LOG_C_MACRO(YES, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  68. /**
  69. * Define version of the macro that only execute if the logLevel is above the threshold.
  70. * The compiled versions essentially look like this:
  71. *
  72. * if (logFlagForThisLogMsg & ddLogLevel) { execute log message }
  73. *
  74. * As shown further below, Lumberjack actually uses a bitmask as opposed to primitive log levels.
  75. * This allows for a great amount of flexibility and some pretty advanced fine grained logging techniques.
  76. *
  77. * Note that when compiler optimizations are enabled (as they are for your release builds),
  78. * the log messages above your logging threshold will automatically be compiled out.
  79. *
  80. * (If the compiler sees ddLogLevel declared as a constant, the compiler simply checks to see if the 'if' statement
  81. * would execute, and if not it strips it from the binary.)
  82. *
  83. * We also define shorthand versions for asynchronous and synchronous logging.
  84. **/
  85. #define LOG_MAYBE(async, lvl, flg, ctx, fnct, frmt, ...) \
  86. do { if(lvl & flg) LOG_MACRO(async, lvl, flg, ctx, nil, fnct, frmt, ##__VA_ARGS__); } while(0)
  87. #define LOG_OBJC_MAYBE(async, lvl, flg, ctx, frmt, ...) \
  88. LOG_MAYBE(async, lvl, flg, ctx, sel_getName(_cmd), frmt, ##__VA_ARGS__)
  89. #define LOG_C_MAYBE(async, lvl, flg, ctx, frmt, ...) \
  90. LOG_MAYBE(async, lvl, flg, ctx, __FUNCTION__, frmt, ##__VA_ARGS__)
  91. #define SYNC_LOG_OBJC_MAYBE(lvl, flg, ctx, frmt, ...) \
  92. LOG_OBJC_MAYBE( NO, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  93. #define ASYNC_LOG_OBJC_MAYBE(lvl, flg, ctx, frmt, ...) \
  94. LOG_OBJC_MAYBE(YES, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  95. #define SYNC_LOG_C_MAYBE(lvl, flg, ctx, frmt, ...) \
  96. LOG_C_MAYBE( NO, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  97. #define ASYNC_LOG_C_MAYBE(lvl, flg, ctx, frmt, ...) \
  98. LOG_C_MAYBE(YES, lvl, flg, ctx, frmt, ##__VA_ARGS__)
  99. /**
  100. * Define versions of the macros that also accept tags.
  101. *
  102. * The DDLogMessage object includes a 'tag' ivar that may be used for a variety of purposes.
  103. * It may be used to pass custom information to loggers or formatters.
  104. * Or it may be used by 3rd party extensions to the framework.
  105. *
  106. * Thes macros just make it a little easier to extend logging functionality.
  107. **/
  108. #define LOG_OBJC_TAG_MACRO(async, lvl, flg, ctx, tag, frmt, ...) \
  109. LOG_MACRO(async, lvl, flg, ctx, tag, sel_getName(_cmd), frmt, ##__VA_ARGS__)
  110. #define LOG_C_TAG_MACRO(async, lvl, flg, ctx, tag, frmt, ...) \
  111. LOG_MACRO(async, lvl, flg, ctx, tag, __FUNCTION__, frmt, ##__VA_ARGS__)
  112. #define LOG_TAG_MAYBE(async, lvl, flg, ctx, tag, fnct, frmt, ...) \
  113. do { if(lvl & flg) LOG_MACRO(async, lvl, flg, ctx, tag, fnct, frmt, ##__VA_ARGS__); } while(0)
  114. #define LOG_OBJC_TAG_MAYBE(async, lvl, flg, ctx, tag, frmt, ...) \
  115. LOG_TAG_MAYBE(async, lvl, flg, ctx, tag, sel_getName(_cmd), frmt, ##__VA_ARGS__)
  116. #define LOG_C_TAG_MAYBE(async, lvl, flg, ctx, tag, frmt, ...) \
  117. LOG_TAG_MAYBE(async, lvl, flg, ctx, tag, __FUNCTION__, frmt, ##__VA_ARGS__)
  118. /**
  119. * Define the standard options.
  120. *
  121. * We default to only 4 levels because it makes it easier for beginners
  122. * to make the transition to a logging framework.
  123. *
  124. * More advanced users may choose to completely customize the levels (and level names) to suite their needs.
  125. * For more information on this see the "Custom Log Levels" page:
  126. * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomLogLevels
  127. *
  128. * Advanced users may also notice that we're using a bitmask.
  129. * This is to allow for custom fine grained logging:
  130. * https://github.com/robbiehanson/CocoaLumberjack/wiki/FineGrainedLogging
  131. *
  132. * -- Flags --
  133. *
  134. * Typically you will use the LOG_LEVELS (see below), but the flags may be used directly in certain situations.
  135. * For example, say you have a lot of warning log messages, and you wanted to disable them.
  136. * However, you still needed to see your error and info log messages.
  137. * You could accomplish that with the following:
  138. *
  139. * static const int ddLogLevel = LOG_FLAG_ERROR | LOG_FLAG_INFO;
  140. *
  141. * Flags may also be consulted when writing custom log formatters,
  142. * as the DDLogMessage class captures the individual flag that caused the log message to fire.
  143. *
  144. * -- Levels --
  145. *
  146. * Log levels are simply the proper bitmask of the flags.
  147. *
  148. * -- Booleans --
  149. *
  150. * The booleans may be used when your logging code involves more than one line.
  151. * For example:
  152. *
  153. * if (LOG_VERBOSE) {
  154. * for (id sprocket in sprockets)
  155. * DDLogVerbose(@"sprocket: %@", [sprocket description])
  156. * }
  157. *
  158. * -- Async --
  159. *
  160. * Defines the default asynchronous options.
  161. * The default philosophy for asynchronous logging is very simple:
  162. *
  163. * Log messages with errors should be executed synchronously.
  164. * After all, an error just occurred. The application could be unstable.
  165. *
  166. * All other log messages, such as debug output, are executed asynchronously.
  167. * After all, if it wasn't an error, then it was just informational output,
  168. * or something the application was easily able to recover from.
  169. *
  170. * -- Changes --
  171. *
  172. * You are strongly discouraged from modifying this file.
  173. * If you do, you make it more difficult on yourself to merge future bug fixes and improvements from the project.
  174. * Instead, create your own MyLogging.h or ApplicationNameLogging.h or CompanyLogging.h
  175. *
  176. * For an example of customizing your logging experience, see the "Custom Log Levels" page:
  177. * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomLogLevels
  178. **/
  179. #define LOG_FLAG_ERROR (1 << 0) // 0...0001
  180. #define LOG_FLAG_WARN (1 << 1) // 0...0010
  181. #define LOG_FLAG_INFO (1 << 2) // 0...0100
  182. #define LOG_FLAG_VERBOSE (1 << 3) // 0...1000
  183. #define LOG_LEVEL_OFF 0
  184. #define LOG_LEVEL_ERROR (LOG_FLAG_ERROR) // 0...0001
  185. #define LOG_LEVEL_WARN (LOG_FLAG_ERROR | LOG_FLAG_WARN) // 0...0011
  186. #define LOG_LEVEL_INFO (LOG_FLAG_ERROR | LOG_FLAG_WARN | LOG_FLAG_INFO) // 0...0111
  187. #define LOG_LEVEL_VERBOSE (LOG_FLAG_ERROR | LOG_FLAG_WARN | LOG_FLAG_INFO | LOG_FLAG_VERBOSE) // 0...1111
  188. #define LOG_ERROR (ddLogLevel & LOG_FLAG_ERROR)
  189. #define LOG_WARN (ddLogLevel & LOG_FLAG_WARN)
  190. #define LOG_INFO (ddLogLevel & LOG_FLAG_INFO)
  191. #define LOG_VERBOSE (ddLogLevel & LOG_FLAG_VERBOSE)
  192. #define LOG_ASYNC_ENABLED YES
  193. #define LOG_ASYNC_ERROR ( NO && LOG_ASYNC_ENABLED)
  194. #define LOG_ASYNC_WARN (YES && LOG_ASYNC_ENABLED)
  195. #define LOG_ASYNC_INFO (YES && LOG_ASYNC_ENABLED)
  196. #define LOG_ASYNC_VERBOSE (YES && LOG_ASYNC_ENABLED)
  197. #define DDLogError(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_ERROR, ddLogLevel, LOG_FLAG_ERROR, 0, frmt, ##__VA_ARGS__)
  198. #define DDLogWarn(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_WARN, ddLogLevel, LOG_FLAG_WARN, 0, frmt, ##__VA_ARGS__)
  199. #define DDLogInfo(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_INFO, ddLogLevel, LOG_FLAG_INFO, 0, frmt, ##__VA_ARGS__)
  200. #define DDLogVerbose(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_VERBOSE, ddLogLevel, LOG_FLAG_VERBOSE, 0, frmt, ##__VA_ARGS__)
  201. #define DDLogCError(frmt, ...) LOG_C_MAYBE(LOG_ASYNC_ERROR, ddLogLevel, LOG_FLAG_ERROR, 0, frmt, ##__VA_ARGS__)
  202. #define DDLogCWarn(frmt, ...) LOG_C_MAYBE(LOG_ASYNC_WARN, ddLogLevel, LOG_FLAG_WARN, 0, frmt, ##__VA_ARGS__)
  203. #define DDLogCInfo(frmt, ...) LOG_C_MAYBE(LOG_ASYNC_INFO, ddLogLevel, LOG_FLAG_INFO, 0, frmt, ##__VA_ARGS__)
  204. #define DDLogCVerbose(frmt, ...) LOG_C_MAYBE(LOG_ASYNC_VERBOSE, ddLogLevel, LOG_FLAG_VERBOSE, 0, frmt, ##__VA_ARGS__)
  205. /**
  206. * The THIS_FILE macro gives you an NSString of the file name.
  207. * For simplicity and clarity, the file name does not include the full path or file extension.
  208. *
  209. * For example: DDLogWarn(@"%@: Unable to find thingy", THIS_FILE) -> @"MyViewController: Unable to find thingy"
  210. **/
  211. NSString *DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
  212. #define THIS_FILE (DDExtractFileNameWithoutExtension(__FILE__, NO))
  213. /**
  214. * The THIS_METHOD macro gives you the name of the current objective-c method.
  215. *
  216. * For example: DDLogWarn(@"%@ - Requires non-nil strings", THIS_METHOD) -> @"setMake:model: requires non-nil strings"
  217. *
  218. * Note: This does NOT work in straight C functions (non objective-c).
  219. * Instead you should use the predefined __FUNCTION__ macro.
  220. **/
  221. #define THIS_METHOD NSStringFromSelector(_cmd)
  222. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223. #pragma mark -
  224. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  225. @interface DDLog : NSObject
  226. /**
  227. * Provides access to the underlying logging queue.
  228. * This may be helpful to Logger classes for things like thread synchronization.
  229. **/
  230. + (dispatch_queue_t)loggingQueue;
  231. /**
  232. * Logging Primitive.
  233. *
  234. * This method is used by the macros above.
  235. * It is suggested you stick with the macros as they're easier to use.
  236. **/
  237. + (void)log:(BOOL)synchronous
  238. level:(int)level
  239. flag:(int)flag
  240. context:(int)context
  241. file:(const char *)file
  242. function:(const char *)function
  243. line:(int)line
  244. tag:(id)tag
  245. format:(NSString *)format, ... __attribute__ ((format (__NSString__, 9, 10)));
  246. /**
  247. * Logging Primitive.
  248. *
  249. * This method can be used if you have a prepared va_list.
  250. **/
  251. + (void)log:(BOOL)asynchronous
  252. level:(int)level
  253. flag:(int)flag
  254. context:(int)context
  255. file:(const char *)file
  256. function:(const char *)function
  257. line:(int)line
  258. tag:(id)tag
  259. format:(NSString *)format
  260. args:(va_list)argList;
  261. /**
  262. * Since logging can be asynchronous, there may be times when you want to flush the logs.
  263. * The framework invokes this automatically when the application quits.
  264. **/
  265. + (void)flushLog;
  266. /**
  267. * Loggers
  268. *
  269. * If you want your log statements to go somewhere,
  270. * you should create and add a logger.
  271. **/
  272. + (void)addLogger:(id <DDLogger>)logger;
  273. + (void)removeLogger:(id <DDLogger>)logger;
  274. + (void)removeAllLoggers;
  275. /**
  276. * Registered Dynamic Logging
  277. *
  278. * These methods allow you to obtain a list of classes that are using registered dynamic logging,
  279. * and also provides methods to get and set their log level during run time.
  280. **/
  281. + (NSArray *)registeredClasses;
  282. + (NSArray *)registeredClassNames;
  283. + (int)logLevelForClass:(Class)aClass;
  284. + (int)logLevelForClassWithName:(NSString *)aClassName;
  285. + (void)setLogLevel:(int)logLevel forClass:(Class)aClass;
  286. + (void)setLogLevel:(int)logLevel forClassWithName:(NSString *)aClassName;
  287. @end
  288. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  289. #pragma mark -
  290. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  291. @protocol DDLogger <NSObject>
  292. @required
  293. - (void)logMessage:(DDLogMessage *)logMessage;
  294. /**
  295. * Formatters may optionally be added to any logger.
  296. *
  297. * If no formatter is set, the logger simply logs the message as it is given in logMessage,
  298. * or it may use its own built in formatting style.
  299. **/
  300. - (id <DDLogFormatter>)logFormatter;
  301. - (void)setLogFormatter:(id <DDLogFormatter>)formatter;
  302. @optional
  303. /**
  304. * Since logging is asynchronous, adding and removing loggers is also asynchronous.
  305. * In other words, the loggers are added and removed at appropriate times with regards to log messages.
  306. *
  307. * - Loggers will not receive log messages that were executed prior to when they were added.
  308. * - Loggers will not receive log messages that were executed after they were removed.
  309. *
  310. * These methods are executed in the logging thread/queue.
  311. * This is the same thread/queue that will execute every logMessage: invocation.
  312. * Loggers may use these methods for thread synchronization or other setup/teardown tasks.
  313. **/
  314. - (void)didAddLogger;
  315. - (void)willRemoveLogger;
  316. /**
  317. * Some loggers may buffer IO for optimization purposes.
  318. * For example, a database logger may only save occasionaly as the disk IO is slow.
  319. * In such loggers, this method should be implemented to flush any pending IO.
  320. *
  321. * This allows invocations of DDLog's flushLog method to be propogated to loggers that need it.
  322. *
  323. * Note that DDLog's flushLog method is invoked automatically when the application quits,
  324. * and it may be also invoked manually by the developer prior to application crashes, or other such reasons.
  325. **/
  326. - (void)flush;
  327. /**
  328. * Each logger is executed concurrently with respect to the other loggers.
  329. * Thus, a dedicated dispatch queue is used for each logger.
  330. * Logger implementations may optionally choose to provide their own dispatch queue.
  331. **/
  332. - (dispatch_queue_t)loggerQueue;
  333. /**
  334. * If the logger implementation does not choose to provide its own queue,
  335. * one will automatically be created for it.
  336. * The created queue will receive its name from this method.
  337. * This may be helpful for debugging or profiling reasons.
  338. **/
  339. - (NSString *)loggerName;
  340. @end
  341. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  342. #pragma mark -
  343. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  344. @protocol DDLogFormatter <NSObject>
  345. @required
  346. /**
  347. * Formatters may optionally be added to any logger.
  348. * This allows for increased flexibility in the logging environment.
  349. * For example, log messages for log files may be formatted differently than log messages for the console.
  350. *
  351. * For more information about formatters, see the "Custom Formatters" page:
  352. * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomFormatters
  353. *
  354. * The formatter may also optionally filter the log message by returning nil,
  355. * in which case the logger will not log the message.
  356. **/
  357. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
  358. @optional
  359. /**
  360. * A single formatter instance can be added to multiple loggers.
  361. * These methods provides hooks to notify the formatter of when it's added/removed.
  362. *
  363. * This is primarily for thread-safety.
  364. * If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers.
  365. * Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter),
  366. * it could possibly use these hooks to switch to thread-safe versions of the code.
  367. **/
  368. - (void)didAddToLogger:(id <DDLogger>)logger;
  369. - (void)willRemoveFromLogger:(id <DDLogger>)logger;
  370. @end
  371. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  372. #pragma mark -
  373. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  374. @protocol DDRegisteredDynamicLogging
  375. /**
  376. * Implement these methods to allow a file's log level to be managed from a central location.
  377. *
  378. * This is useful if you'd like to be able to change log levels for various parts
  379. * of your code from within the running application.
  380. *
  381. * Imagine pulling up the settings for your application,
  382. * and being able to configure the logging level on a per file basis.
  383. *
  384. * The implementation can be very straight-forward:
  385. *
  386. * + (int)ddLogLevel
  387. * {
  388. * return ddLogLevel;
  389. * }
  390. *
  391. * + (void)ddSetLogLevel:(int)logLevel
  392. * {
  393. * ddLogLevel = logLevel;
  394. * }
  395. **/
  396. + (int)ddLogLevel;
  397. + (void)ddSetLogLevel:(int)logLevel;
  398. @end
  399. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  400. #pragma mark -
  401. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  402. /**
  403. * The DDLogMessage class encapsulates information about the log message.
  404. * If you write custom loggers or formatters, you will be dealing with objects of this class.
  405. **/
  406. enum {
  407. DDLogMessageCopyFile = 1 << 0,
  408. DDLogMessageCopyFunction = 1 << 1
  409. };
  410. typedef int DDLogMessageOptions;
  411. @interface DDLogMessage : NSObject
  412. {
  413. // The public variables below can be accessed directly (for speed).
  414. // For example: logMessage->logLevel
  415. @public
  416. int logLevel;
  417. int logFlag;
  418. int logContext;
  419. NSString *logMsg;
  420. NSDate *timestamp;
  421. char *file;
  422. char *function;
  423. int lineNumber;
  424. mach_port_t machThreadID;
  425. char *queueLabel;
  426. NSString *threadName;
  427. // For 3rd party extensions to the framework, where flags and contexts aren't enough.
  428. id tag;
  429. // For 3rd party extensions that manually create DDLogMessage instances.
  430. DDLogMessageOptions options;
  431. }
  432. /**
  433. * Standard init method for a log message object.
  434. * Used by the logging primitives. (And the macros use the logging primitives.)
  435. *
  436. * If you find need to manually create logMessage objects, there is one thing you should be aware of:
  437. *
  438. * If no flags are passed, the method expects the file and function parameters to be string literals.
  439. * That is, it expects the given strings to exist for the duration of the object's lifetime,
  440. * and it expects the given strings to be immutable.
  441. * In other words, it does not copy these strings, it simply points to them.
  442. * This is due to the fact that __FILE__ and __FUNCTION__ are usually used to specify these parameters,
  443. * so it makes sense to optimize and skip the unnecessary allocations.
  444. * However, if you need them to be copied you may use the options parameter to specify this.
  445. * Options is a bitmask which supports DDLogMessageCopyFile and DDLogMessageCopyFunction.
  446. **/
  447. - (id)initWithLogMsg:(NSString *)logMsg
  448. level:(int)logLevel
  449. flag:(int)logFlag
  450. context:(int)logContext
  451. file:(const char *)file
  452. function:(const char *)function
  453. line:(int)line
  454. tag:(id)tag
  455. options:(DDLogMessageOptions)optionsMask;
  456. /**
  457. * Returns the threadID as it appears in NSLog.
  458. * That is, it is a hexadecimal value which is calculated from the machThreadID.
  459. **/
  460. - (NSString *)threadID;
  461. /**
  462. * Convenience property to get just the file name, as the file variable is generally the full file path.
  463. * This method does not include the file extension, which is generally unwanted for logging purposes.
  464. **/
  465. - (NSString *)fileName;
  466. /**
  467. * Returns the function variable in NSString form.
  468. **/
  469. - (NSString *)methodName;
  470. @end
  471. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  472. #pragma mark -
  473. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  474. /**
  475. * The DDLogger protocol specifies that an optional formatter can be added to a logger.
  476. * Most (but not all) loggers will want to support formatters.
  477. *
  478. * However, writting getters and setters in a thread safe manner,
  479. * while still maintaining maximum speed for the logging process, is a difficult task.
  480. *
  481. * To do it right, the implementation of the getter/setter has strict requiremenets:
  482. * - Must NOT require the logMessage method to acquire a lock.
  483. * - Must NOT require the logMessage method to access an atomic property (also a lock of sorts).
  484. *
  485. * To simplify things, an abstract logger is provided that implements the getter and setter.
  486. *
  487. * Logger implementations may simply extend this class,
  488. * and they can ACCESS THE FORMATTER VARIABLE DIRECTLY from within their logMessage method!
  489. **/
  490. @interface DDAbstractLogger : NSObject <DDLogger>
  491. {
  492. id <DDLogFormatter> formatter;
  493. dispatch_queue_t loggerQueue;
  494. }
  495. - (id <DDLogFormatter>)logFormatter;
  496. - (void)setLogFormatter:(id <DDLogFormatter>)formatter;
  497. // For thread-safety assertions
  498. - (BOOL)isOnGlobalLoggingQueue;
  499. - (BOOL)isOnInternalLoggerQueue;
  500. @end