DDTTYLogger.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #import <Foundation/Foundation.h>
  2. #if TARGET_OS_IPHONE
  3. #import <UIKit/UIColor.h>
  4. #else
  5. #import <AppKit/NSColor.h>
  6. #endif
  7. #import "DDLog.h"
  8. /**
  9. * Welcome to Cocoa Lumberjack!
  10. *
  11. * The project page has a wealth of documentation if you have any questions.
  12. * https://github.com/robbiehanson/CocoaLumberjack
  13. *
  14. * If you're new to the project you may wish to read the "Getting Started" wiki.
  15. * https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted
  16. *
  17. *
  18. * This class provides a logger for Terminal output or Xcode console output,
  19. * depending on where you are running your code.
  20. *
  21. * As described in the "Getting Started" page,
  22. * the traditional NSLog() function directs it's output to two places:
  23. *
  24. * - Apple System Log (so it shows up in Console.app)
  25. * - StdErr (if stderr is a TTY, so log statements show up in Xcode console)
  26. *
  27. * To duplicate NSLog() functionality you can simply add this logger and an asl logger.
  28. * However, if you instead choose to use file logging (for faster performance),
  29. * you may choose to use only a file logger and a tty logger.
  30. **/
  31. @interface DDTTYLogger : DDAbstractLogger <DDLogger>
  32. {
  33. NSCalendar *calendar;
  34. NSUInteger calendarUnitFlags;
  35. NSString *appName;
  36. char *app;
  37. size_t appLen;
  38. NSString *processID;
  39. char *pid;
  40. size_t pidLen;
  41. BOOL colorsEnabled;
  42. NSMutableArray *colorProfilesArray;
  43. NSMutableDictionary *colorProfilesDict;
  44. }
  45. + (DDTTYLogger *)sharedInstance;
  46. /* Inherited from the DDLogger protocol:
  47. *
  48. * Formatters may optionally be added to any logger.
  49. *
  50. * If no formatter is set, the logger simply logs the message as it is given in logMessage,
  51. * or it may use its own built in formatting style.
  52. *
  53. * More information about formatters can be found here:
  54. * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomFormatters
  55. *
  56. * The actual implementation of these methods is inherited from DDAbstractLogger.
  57. - (id <DDLogFormatter>)logFormatter;
  58. - (void)setLogFormatter:(id <DDLogFormatter>)formatter;
  59. */
  60. /**
  61. * Want to use different colors for different log levels?
  62. * Enable this property.
  63. *
  64. * If you run the application via the Terminal (not Xcode),
  65. * the logger will map colors to xterm-256color or xterm-color (if available).
  66. *
  67. * Xcode does NOT natively support colors in the Xcode debugging console.
  68. * You'll need to install the XcodeColors plugin to see colors in the Xcode console.
  69. * https://github.com/robbiehanson/XcodeColors
  70. *
  71. * The default value if NO.
  72. **/
  73. @property (readwrite, assign) BOOL colorsEnabled;
  74. /**
  75. * The default color set (foregroundColor, backgroundColor) is:
  76. *
  77. * - LOG_FLAG_ERROR = (red, nil)
  78. * - LOG_FLAG_WARN = (orange, nil)
  79. *
  80. * You can customize the colors however you see fit.
  81. * Please note that you are passing a flag, NOT a level.
  82. *
  83. * GOOD : [ttyLogger setForegroundColor:pink backgroundColor:nil forFlag:LOG_FLAG_INFO]; // <- Good :)
  84. * BAD : [ttyLogger setForegroundColor:pink backgroundColor:nil forFlag:LOG_LEVEL_INFO]; // <- BAD! :(
  85. *
  86. * LOG_FLAG_INFO = 0...00100
  87. * LOG_LEVEL_INFO = 0...00111 <- Would match LOG_FLAG_INFO and LOG_FLAG_WARN and LOG_FLAG_ERROR
  88. *
  89. * If you run the application within Xcode, then the XcodeColors plugin is required.
  90. *
  91. * If you run the application from a shell, then DDTTYLogger will automatically map the given color to
  92. * the closest available color. (xterm-256color or xterm-color which have 256 and 16 supported colors respectively.)
  93. *
  94. * This method invokes setForegroundColor:backgroundColor:forFlag:context: and passes the default context (0).
  95. **/
  96. #if TARGET_OS_IPHONE
  97. - (void)setForegroundColor:(UIColor *)txtColor backgroundColor:(UIColor *)bgColor forFlag:(int)mask;
  98. #else
  99. - (void)setForegroundColor:(NSColor *)txtColor backgroundColor:(NSColor *)bgColor forFlag:(int)mask;
  100. #endif
  101. /**
  102. * Just like setForegroundColor:backgroundColor:flag, but allows you to specify a particular logging context.
  103. *
  104. * A logging context is often used to identify log messages coming from a 3rd party framework,
  105. * although logging context's can be used for many different functions.
  106. *
  107. * Logging context's are explained in further detail here:
  108. * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomContext
  109. **/
  110. #if TARGET_OS_IPHONE
  111. - (void)setForegroundColor:(UIColor *)txtColor backgroundColor:(UIColor *)bgColor forFlag:(int)mask context:(int)ctxt;
  112. #else
  113. - (void)setForegroundColor:(NSColor *)txtColor backgroundColor:(NSColor *)bgColor forFlag:(int)mask context:(int)ctxt;
  114. #endif
  115. /**
  116. * Similar to the methods above, but allows you to map DDLogMessage->tag to a particular color profile.
  117. * For example, you could do something like this:
  118. *
  119. * static NSString *const PurpleTag = @"PurpleTag";
  120. *
  121. * #define DDLogPurple(frmt, ...) LOG_OBJC_TAG_MACRO(NO, 0, 0, 0, PurpleTag, frmt, ##__VA_ARGS__)
  122. *
  123. * And then in your applicationDidFinishLaunching, or wherever you configure Lumberjack:
  124. *
  125. * #if TARGET_OS_IPHONE
  126. * UIColor *purple = [UIColor colorWithRed:(64/255.0) green:(0/255.0) blue:(128/255.0) alpha:1.0];
  127. * #else
  128. * NSColor *purple = [NSColor colorWithCalibratedRed:(64/255.0) green:(0/255.0) blue:(128/255.0) alpha:1.0];
  129. *
  130. * [[DDTTYLogger sharedInstance] setForegroundColor:purple backgroundColor:nil forTag:PurpleTag];
  131. * [DDLog addLogger:[DDTTYLogger sharedInstance]];
  132. *
  133. * This would essentially give you a straight NSLog replacement that prints in purple:
  134. *
  135. * DDLogPurple(@"I'm a purple log message!");
  136. **/
  137. #if TARGET_OS_IPHONE
  138. - (void)setForegroundColor:(UIColor *)txtColor backgroundColor:(UIColor *)bgColor forTag:(id <NSCopying>)tag;
  139. #else
  140. - (void)setForegroundColor:(NSColor *)txtColor backgroundColor:(NSColor *)bgColor forTag:(id <NSCopying>)tag;
  141. #endif
  142. /**
  143. * Clearing color profiles.
  144. **/
  145. - (void)clearColorsForFlag:(int)mask;
  146. - (void)clearColorsForFlag:(int)mask context:(int)context;
  147. - (void)clearColorsForTag:(id <NSCopying>)tag;
  148. - (void)clearColorsForAllFlags;
  149. - (void)clearColorsForAllTags;
  150. - (void)clearAllColors;
  151. @end