Skip to content
Browse files

Adding ability to specify logging context for colors.

  • Loading branch information...
1 parent 2b2a2fb commit a9e7701ef6d737c82f43480ed53fb9d11e5b1670 @robbiehanson robbiehanson committed
Showing with 54 additions and 7 deletions.
  1. +27 −1 Lumberjack/DDTTYLogger.h
  2. +27 −6 Lumberjack/DDTTYLogger.m
View
28 Lumberjack/DDTTYLogger.h
@@ -81,6 +81,8 @@
*
* If you run the application from a shell, then DDTTYLogger will automatically try to map the given color to
* the closest available color. (xterm-256color or xterm-color which have 256 and 16 supported colors respectively.)
+ *
+ * This method invokes setForegroundColor:backgroundColor:forFlag:context:, and passes the default context (0).
**/
#if TARGET_OS_IPHONE
- (void)setForegroundColor:(UIColor *)txtColor backgroundColor:(UIColor *)bgColor forFlag:(int)mask;
@@ -89,9 +91,33 @@
#endif
/**
- * Allows you to clear color mappings, either per flag or all.
+ * Allows you to customize the color for a particular flag, within a particular logging context.
+ *
+ * A logging context may identify log messages coming from a 3rd party framework.
+ * Logging context's are explained in further detail here:
+ * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomContext
+**/
+#if TARGET_OS_IPHONE
+- (void)setForegroundColor:(UIColor *)txtColor backgroundColor:(UIColor *)bgColor forFlag:(int)mask context:(int)ctxt;
+#else
+- (void)setForegroundColor:(NSColor *)txtColor backgroundColor:(NSColor *)bgColor forFlag:(int)mask context:(int)ctxt;
+#endif
+
+/**
+ * Clears the color profiles for a particular flag.
+ *
+ * This method invokes clearColorsForFlag:context:, and passes the default context (0).
**/
- (void)clearColorsForFlag:(int)mask;
+
+/**
+ * Clears the color profiles for a particular flag, within a particular logging context.
+**/
+- (void)clearColorsForFlag:(int)mask context:(int)context;
+
+/**
+ * Clears all color profiles.
+**/
- (void)clearColorsForAllFlags;
View
33 Lumberjack/DDTTYLogger.m
@@ -96,6 +96,7 @@
@interface DDTTYLoggerColorProfile : NSObject {
@public
int mask;
+ int context;
uint8_t fg_r;
uint8_t fg_g;
@@ -122,6 +123,7 @@ @interface DDTTYLoggerColorProfile : NSObject {
}
- (id)initWithForegroundColor:(OSColor *)fgColor backgroundColor:(OSColor *)bgColor flag:(int)mask;
+- (id)initWithForegroundColor:(OSColor *)fgColor backgroundColor:(OSColor *)bgColor flag:(int)mask context:(int)ctxt;
@end
@@ -892,17 +894,25 @@ - (void)setColorsEnabled:(BOOL)newColorsEnabled
- (void)setForegroundColor:(OSColor *)txtColor backgroundColor:(OSColor *)bgColor forFlag:(int)mask
{
+ [self setForegroundColor:txtColor backgroundColor:bgColor forFlag:mask context:0];
+}
+
+- (void)setForegroundColor:(OSColor *)txtColor backgroundColor:(OSColor *)bgColor forFlag:(int)mask context:(int)ctxt
+{
dispatch_block_t block = ^{ @autoreleasepool {
DDTTYLoggerColorProfile *newColorProfile =
- [[DDTTYLoggerColorProfile alloc] initWithForegroundColor:txtColor backgroundColor:bgColor flag:mask];
+ [[DDTTYLoggerColorProfile alloc] initWithForegroundColor:txtColor
+ backgroundColor:bgColor
+ flag:mask
+ context:ctxt];
NSLogInfo(@"DDTTYLogger: newColorProfile: %@", newColorProfile);
NSUInteger i = 0;
for (DDTTYLoggerColorProfile *colorProfile in colorProfiles)
{
- if (colorProfile->mask == mask)
+ if ((colorProfile->mask == mask) && (colorProfile->context == ctxt))
{
break;
}
@@ -937,12 +947,17 @@ - (void)setForegroundColor:(OSColor *)txtColor backgroundColor:(OSColor *)bgColo
- (void)clearColorsForFlag:(int)mask
{
+ [self clearColorsForFlag:mask context:0];
+}
+
+- (void)clearColorsForFlag:(int)mask context:(int)context
+{
dispatch_block_t block = ^{ @autoreleasepool {
NSUInteger i = 0;
for (DDTTYLoggerColorProfile *colorProfile in colorProfiles)
{
- if (colorProfile->mask == mask)
+ if ((colorProfile->mask == mask) && (colorProfile->context == context))
{
break;
}
@@ -1024,7 +1039,7 @@ - (void)logMessage:(DDLogMessage *)logMessage
{
for (DDTTYLoggerColorProfile *cp in colorProfiles)
{
- if ((logMessage->logFlag & cp->mask) != 0)
+ if ((logMessage->logFlag & cp->mask) && (logMessage->logContext == cp->context))
{
colorProfile = cp;
break;
@@ -1180,9 +1195,15 @@ @implementation DDTTYLoggerColorProfile
- (id)initWithForegroundColor:(OSColor *)fgColor backgroundColor:(OSColor *)bgColor flag:(int)aMask
{
+ return [self initWithForegroundColor:fgColor backgroundColor:bgColor flag:aMask context:0];
+}
+
+- (id)initWithForegroundColor:(OSColor *)fgColor backgroundColor:(OSColor *)bgColor flag:(int)aMask context:(int)ctxt
+{
if ((self = [super init]))
{
mask = aMask;
+ context = ctxt;
CGFloat r, g, b;
@@ -1286,8 +1307,8 @@ - (id)initWithForegroundColor:(OSColor *)fgColor backgroundColor:(OSColor *)bgCo
- (NSString *)description
{
return [NSString stringWithFormat:
- @"<DDTTYLoggerColorProfile: %x mask:%i fg:%u,%u,%u bg:%u,%u,%u fgCode:%@ bgCode:%@>",
- self, mask, fg_r, fg_g, fg_b, bg_r, bg_g, bg_b, fgCodeRaw, bgCodeRaw];
+ @"<DDTTYLoggerColorProfile: %x mask:%i ctxt:%i fg:%u,%u,%u bg:%u,%u,%u fgCode:%@ bgCode:%@>",
+ self, mask, context, fg_r, fg_g, fg_b, bg_r, bg_g, bg_b, fgCodeRaw, bgCodeRaw];
}
@end

0 comments on commit a9e7701

Please sign in to comment.
Something went wrong with that request. Please try again.