Created
August 21, 2014 01:03
-
-
Save Brimizer/70642b1528e25f8070f2 to your computer and use it in GitHub Desktop.
NSLogColor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An easy way to NSLog a message with any color you'd like. | |
* Requires XcodeColors, available here: https://github.com/robbiehanson/XcodeColors | |
* MIT License | |
* Created by Daniel Brim | |
*/ | |
#define NSLogColor(color, args...) ExtendNSLogColor(color, __FILE__,__LINE__,__PRETTY_FUNCTION__,args); | |
void NSLogColor(UIColor *color, const char *file, int lineNumber, const char *functionName, NSString *format, ...); | |
@interface NSLogExtras : NSObject | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An easy way to NSLog a message with any color you'd like. | |
* Requires XcodeColors, available here: https://github.com/robbiehanson/XcodeColors | |
* MIT License | |
* Created by Daniel Brim | |
*/ | |
#import "ExtendNSLogFunctionality.h" | |
@implementation ExtendNSLogFunctionality | |
void NSLogColor(UIColor *color, const char *file, int lineNumber, const char *functionName, NSString *format, ...) | |
{ | |
char *xcode_colors = getenv(XCODE_COLORS); | |
if (!xcode_colors || !(strcmp(xcode_colors, "YES") == 0)) | |
{ | |
// XcodeColors is not installed, run a regular NSLog | |
// or do something else! | |
return; | |
} | |
// Type to hold information about variable arguments. | |
va_list ap; | |
// Initialize a variable argument list. | |
va_start (ap, format); | |
// NSLog only adds a newline to the end of the NSLog format if | |
// one is not already there. | |
// Here we are utilizing this feature of NSLog() | |
if (![format hasSuffix: @"\n"]) | |
{ | |
format = [format stringByAppendingString: @"\n"]; | |
} | |
NSString *body = [[NSString alloc] initWithFormat:format arguments:ap]; | |
// End using variable argument list. | |
va_end (ap); | |
NSString *coloredBody = wrapTextInColor(body, color); | |
fprintf(stderr, "%s[%d] %s", functionName, lineNumber, [coloredBody UTF8String]); | |
} | |
NSString* wrapTextInColor(NSString *text, UIColor *color) | |
{ | |
NSString *colorString = colorToString(color); | |
NSString *colored = [NSString stringWithFormat:@"\033[fg%@; %@ \033[;", colorString, text]; | |
return colored; | |
} | |
NSString* colorToString(UIColor *color) | |
{ | |
CGFloat red; | |
CGFloat green; | |
CGFloat blue; | |
[color getRed:&red green:&green blue:&blue alpha:nil]; | |
red *= 255; | |
blue *= 255; | |
green *= 255; | |
int redI; | |
int greenI; | |
int blueI; | |
redI = round(red); | |
greenI = round(green); | |
blueI = round(blue); | |
return [NSString stringWithFormat:@"%d,%d,%d", redI, greenI, blueI]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment