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.
 
 

177 lines
6.6 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. /**
  16. * This class provides a logger for Terminal output or Xcode console output,
  17. * depending on where you are running your code.
  18. *
  19. * As described in the "Getting Started" page,
  20. * the traditional NSLog() function directs it's output to two places:
  21. *
  22. * - Apple System Log (so it shows up in Console.app)
  23. * - StdErr (if stderr is a TTY, so log statements show up in Xcode console)
  24. *
  25. * To duplicate NSLog() functionality you can simply add this logger and an asl logger.
  26. * However, if you instead choose to use file logging (for faster performance),
  27. * you may choose to use only a file logger and a tty logger.
  28. **/
  29. // Disable legacy macros
  30. #ifndef DD_LEGACY_MACROS
  31. #define DD_LEGACY_MACROS 0
  32. #endif
  33. #import "DDLog.h"
  34. #define LOG_CONTEXT_ALL INT_MAX
  35. #pragma clang diagnostic push
  36. #pragma clang diagnostic ignored "-Wunused-function"
  37. #if TARGET_OS_IPHONE
  38. // iOS
  39. #import <UIKit/UIColor.h>
  40. typedef UIColor DDColor;
  41. static inline DDColor* DDMakeColor(CGFloat r, CGFloat g, CGFloat b) {return [DDColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0f];}
  42. #elif __has_include(<AppKit/NSColor.h>)
  43. // OS X with AppKit
  44. #import <AppKit/NSColor.h>
  45. typedef NSColor DDColor;
  46. static inline DDColor* DDMakeColor(CGFloat r, CGFloat g, CGFloat b) {return [DDColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0f];}
  47. #else
  48. // OS X CLI
  49. #import "CLIColor.h"
  50. typedef CLIColor DDColor;
  51. static inline DDColor* DDMakeColor(CGFloat r, CGFloat g, CGFloat b) {return [DDColor colorWithCalibratedRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0f];}
  52. #endif
  53. #pragma clang diagnostic pop
  54. @interface DDTTYLogger : DDAbstractLogger <DDLogger>
  55. + (instancetype)sharedInstance;
  56. /* Inherited from the DDLogger protocol:
  57. *
  58. * Formatters may optionally be added to any logger.
  59. *
  60. * If no formatter is set, the logger simply logs the message as it is given in logMessage,
  61. * or it may use its own built in formatting style.
  62. *
  63. * More information about formatters can be found here:
  64. * Documentation/CustomFormatters.md
  65. *
  66. * The actual implementation of these methods is inherited from DDAbstractLogger.
  67. - (id <DDLogFormatter>)logFormatter;
  68. - (void)setLogFormatter:(id <DDLogFormatter>)formatter;
  69. */
  70. /**
  71. * Want to use different colors for different log levels?
  72. * Enable this property.
  73. *
  74. * If you run the application via the Terminal (not Xcode),
  75. * the logger will map colors to xterm-256color or xterm-color (if available).
  76. *
  77. * Xcode does NOT natively support colors in the Xcode debugging console.
  78. * You'll need to install the XcodeColors plugin to see colors in the Xcode console.
  79. * https://github.com/robbiehanson/XcodeColors
  80. *
  81. * The default value is NO.
  82. **/
  83. @property (readwrite, assign) BOOL colorsEnabled;
  84. /**
  85. * When using a custom formatter you can set the logMessage method not to append
  86. * '\n' character after each output. This allows for some greater flexibility with
  87. * custom formatters. Default value is YES.
  88. **/
  89. @property (nonatomic, readwrite, assign) BOOL automaticallyAppendNewlineForCustomFormatters;
  90. /**
  91. * The default color set (foregroundColor, backgroundColor) is:
  92. *
  93. * - DDLogFlagError = (red, nil)
  94. * - DDLogFlagWarning = (orange, nil)
  95. *
  96. * You can customize the colors however you see fit.
  97. * Please note that you are passing a flag, NOT a level.
  98. *
  99. * GOOD : [ttyLogger setForegroundColor:pink backgroundColor:nil forFlag:DDLogFlagInfo]; // <- Good :)
  100. * BAD : [ttyLogger setForegroundColor:pink backgroundColor:nil forFlag:DDLogLevelInfo]; // <- BAD! :(
  101. *
  102. * DDLogFlagInfo = 0...00100
  103. * DDLogLevelInfo = 0...00111 <- Would match DDLogFlagInfo and DDLogFlagWarning and DDLogFlagError
  104. *
  105. * If you run the application within Xcode, then the XcodeColors plugin is required.
  106. *
  107. * If you run the application from a shell, then DDTTYLogger will automatically map the given color to
  108. * the closest available color. (xterm-256color or xterm-color which have 256 and 16 supported colors respectively.)
  109. *
  110. * This method invokes setForegroundColor:backgroundColor:forFlag:context: and applies it to `LOG_CONTEXT_ALL`.
  111. **/
  112. - (void)setForegroundColor:(DDColor *)txtColor backgroundColor:(DDColor *)bgColor forFlag:(DDLogFlag)mask;
  113. /**
  114. * Just like setForegroundColor:backgroundColor:flag, but allows you to specify a particular logging context.
  115. *
  116. * A logging context is often used to identify log messages coming from a 3rd party framework,
  117. * although logging context's can be used for many different functions.
  118. *
  119. * Use LOG_CONTEXT_ALL to set the deafult color for all contexts that have no specific color set defined.
  120. *
  121. * Logging context's are explained in further detail here:
  122. * Documentation/CustomContext.md
  123. **/
  124. - (void)setForegroundColor:(DDColor *)txtColor backgroundColor:(DDColor *)bgColor forFlag:(DDLogFlag)mask context:(NSInteger)ctxt;
  125. /**
  126. * Similar to the methods above, but allows you to map DDLogMessage->tag to a particular color profile.
  127. * For example, you could do something like this:
  128. *
  129. * static NSString *const PurpleTag = @"PurpleTag";
  130. *
  131. * #define DDLogPurple(frmt, ...) LOG_OBJC_TAG_MACRO(NO, 0, 0, 0, PurpleTag, frmt, ##__VA_ARGS__)
  132. *
  133. * And then where you configure CocoaLumberjack:
  134. *
  135. * purple = DDMakeColor((64/255.0), (0/255.0), (128/255.0));
  136. *
  137. * or any UIColor/NSColor constructor.
  138. *
  139. * Note: For CLI OS X projects that don't link with AppKit use CLIColor objects instead
  140. *
  141. * [[DDTTYLogger sharedInstance] setForegroundColor:purple backgroundColor:nil forTag:PurpleTag];
  142. * [DDLog addLogger:[DDTTYLogger sharedInstance]];
  143. *
  144. * This would essentially give you a straight NSLog replacement that prints in purple:
  145. *
  146. * DDLogPurple(@"I'm a purple log message!");
  147. **/
  148. - (void)setForegroundColor:(DDColor *)txtColor backgroundColor:(DDColor *)bgColor forTag:(id <NSCopying>)tag;
  149. /**
  150. * Clearing color profiles.
  151. **/
  152. - (void)clearColorsForFlag:(DDLogFlag)mask;
  153. - (void)clearColorsForFlag:(DDLogFlag)mask context:(NSInteger)context;
  154. - (void)clearColorsForTag:(id <NSCopying>)tag;
  155. - (void)clearColorsForAllFlags;
  156. - (void)clearColorsForAllTags;
  157. - (void)clearAllColors;
  158. @end