説明なし

DDTTYLogger.h 6.4KB

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