暂无描述

AWSDDTTYLogger.h 6.9KB

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