-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathColorLog.h
220 lines (156 loc) · 8.77 KB
/
ColorLog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// COMMON FILE: Common
//
// ColorLog.h
// ColorLog
//
// Created by Uncle MiF on 9/15/10.
// Copyright 2010 Deep IT. All rights reserved.
//
// First of all, the SIMBL must be installed: http://culater.net/software/SIMBL/SIMBL.php
// Next, XcodeColors.bundle - plugin must be installed to ~/Library/Application Support/SIMBL/Plugins/
// Xcode must be restarted to apply changes.
// Supported Xcode 3 and 4. Leopard 10.5 or Snow Leopard 10.6. Autorelease Pool or GC environment.
/*
Usage demo:
#import "ColorLog.h"
...
NSLog(@"You can simple log " LCL_BLUE "and " LCL_GREEN "use " LCL_RED "colors" LCL_RESET);
// or use safe function that strip colors if no plugn found:
NSLogColor(@"Hello Standard world!\n"
LBCL_WHITE
LCL_RED "Hello Red world!\n"
LCL_GREEN "Hello Green world!\n"
LCL_YELLOW "Hello Yellow world!\n"
LCL_BLUE "Hello Blue world!\n"
LCL_MAGENTA "Hello Magenta world!\n"
LCL_CYAN "Hello Cyan world!\n"
LCL_WHITE "Hello White world!\n"
LCL_RESET "Hello Standard world again!");
NSLogColor(@"New Standard line");
NSLogColor(LBCL_GREEN @"New Green-Br line");// background and color continues until next color set
NSLogColor(@"Next Green-Bg line");
NSLogColor(@"Next Green-Bg line");
NSLogColor(LBCL_GREEN LCL_BLUE @"Next Blue Green-Bg line");
NSLogColor(@"Last Green-Bg line" LCL_RESET);
NSLogColor(LBCL_YELLOW @"New line on Yellow-Bg");
NSLogReset();// it works as split line also
NSLogRed(LBCL_BLUE @"I'm Red line on Blue-Bg");
NSLogGreen(@"I'm Green line");
NSLogYellow(@"I'm Yellow line");
NSLogBlue(LBCL_GREEN @"I'm Blue line on Green-Bg");
NSLogMagenta(@"I'm Magenta line");
NSLogCyan(LBCL_BLACK @"I'm Cyan line on Black-Bg");
NSLogWhite(LBCL_RED @"I'm White line on Red-Bg " LCL_BLUE @"Blue cut " LCL_GREEN @"Green cut");
*/
/*
There are known issues:
1)
To prevent Xcode crash, we don't delete special \033[..m (ESC-sequences, or NBSP for iOS) from the console text internal storage,
however we replace all of them to \035 - logical group marker instead, that does nothing and invisible.
So, you actually don't see special characters on colored output but you may invisble-touch them while copy-paste to TextEdit.
Here is solution if you need to full clear output: copy and paste a log to the vi, then run one command:
:%s/Ctrl-v]//g
Where Ctrl-v] means you press Ctrl-v then Ctrl-]
Or you can even disable XcodeColors module to see results as is:
setenv("XcodeColors", "NO", 1);
or by executable environment configuration
2)
Sometimes, rarely, colors may be shown not as need (ignored).
It may happen due to limited lazy console refresh.
*/
#import <Foundation/Foundation.h>
// You can use simple NSLogColor (as NSLog replacement) to automatically strip color specific sequences from the format string if it needs
#if TARGET_OS_IPHONE
#define LC_ESC @"\xC2\xA0"
#else
#define LC_ESC @"\033"
#endif
// Only restricted ANSI colors set available now:
// Foreground
#define LCL_BLACK LC_ESC @"[0;30m"
#define LCL_RED LC_ESC @"[0;31m"
#define LCL_GREEN LC_ESC @"[0;32m"
#define LCL_YELLOW LC_ESC @"[0;33m"
#define LCL_BLUE LC_ESC @"[0;34m"
#define LCL_MAGENTA LC_ESC @"[0;35m"
#define LCL_CYAN LC_ESC @"[0;36m"
#define LCL_WHITE LC_ESC @"[0;37m"
// Background
#define LBCL_BLACK LC_ESC @"[0;40m"
#define LBCL_RED LC_ESC @"[0;41m"
#define LBCL_GREEN LC_ESC @"[0;42m"
#define LBCL_YELLOW LC_ESC @"[0;43m"
#define LBCL_BLUE LC_ESC @"[0;44m"
#define LBCL_MAGENTA LC_ESC @"[0;45m"
#define LBCL_CYAN LC_ESC @"[0;46m"
#define LBCL_WHITE LC_ESC @"[0;47m"
// Reset colors
#define LCL_RESET LC_ESC @"[0m"
/* To adjust colors
There are only 7 basic colors for the Foreground and 7 for the Background but you can adjust all of them.
Color names are black, red, green etc - as defined in appropriate define macros.
To define a custom color for predefined colors use the following command format:
defaults write com.apple.Xcode ColorLog_blackColor -TYPE YOUR_VALUE
where TYPE maybe simple 'string'
then you can define value as +method name of std NSColor class
like 'blackColor' method:
defaults write com.apple.Xcode ColorLog_blackColor -string blackColor
or in RGBA format with 0..1 floating point number for every component:
defaults write com.apple.Xcode ColorLog_greenBackgroundColor -string 0.1,0.9,0.1,0.5
defaults write com.apple.Xcode ColorLog_redBackgroundColor -string 0.9,0.1,0.1,0.5
or even set color option as NSColor archived as standard NSData.
*/
BOOL IsXcodeColorsEnabled();
NSString* StripXcodeColors(NSString* str,...) __attribute__((format(__NSString__, 1, 2)));// format checking is enabled
NSString* AllowXcodeColors(NSString* str,...) __attribute__((format(__NSString__, 1, 2)));// just returns the same string, it needs for format chacking
extern void (*__ColorLog_NSLog)(NSString * fmt,...);// to prevent false warning about format string
#define _LCL(str,...) (IsXcodeColorsEnabled() ? AllowXcodeColors((str),##__VA_ARGS__) : StripXcodeColors((str),##__VA_ARGS__))
#if (defined DINSLogFilter) && (defined NSLog)
#define NSLogColor(fmt,...) NSLog(_LCL((fmt),##__VA_ARGS__),##__VA_ARGS__)
// DINSLogFilter extensions:
#define NSLogColorZone(zone,fmt,...) NSLogZone(zone,_LCL((fmt),##__VA_ARGS__),##__VA_ARGS__)
#define NSLogColorDebug(fmt,...) NSLogDebug(_LCL((fmt),##__VA_ARGS__),##__VA_ARGS__)
#define NSLogColorZoneDebug(zone,fmt,...) NSLogZoneDebug(zone,_LCL((fmt),##__VA_ARGS__),##__VA_ARGS__)
#else
#define NSLogColor(fmt,...) __ColorLog_NSLog(_LCL((fmt),##__VA_ARGS__),##__VA_ARGS__)
#endif
// Useful pre-colored logs:
#define NSLogBlack(fmt,...) NSLogColor((LCL_BLACK fmt LCL_RESET),##__VA_ARGS__)
#define NSLogRed(fmt,...) NSLogColor((LCL_RED fmt LCL_RESET),##__VA_ARGS__)
#define NSLogGreen(fmt,...) NSLogColor((LCL_GREEN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogYellow(fmt,...) NSLogColor((LCL_YELLOW fmt LCL_RESET),##__VA_ARGS__)
#define NSLogBlue(fmt,...) NSLogColor((LCL_BLUE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogMagenta(fmt,...) NSLogColor((LCL_MAGENTA fmt LCL_RESET),##__VA_ARGS__)
#define NSLogCyan(fmt,...) NSLogColor((LCL_CYAN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogWhite(fmt,...) NSLogColor((LCL_WHITE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogReset(...) NSLogColor(LCL_RESET)
// DINSLogFilter extensions:
#if (defined DINSLogFilter) && (defined NSLog)
#define NSLogBlackZone(zone,fmt,...) NSLogColorZone(zone,(LCL_BLACK fmt LCL_RESET),##__VA_ARGS__)
#define NSLogRedZone(zone,fmt,...) NSLogColorZone(zone,(LCL_RED fmt LCL_RESET),##__VA_ARGS__)
#define NSLogGreenZone(zone,fmt,...) NSLogColorZone(zone,(LCL_GREEN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogYellowZone(zone,fmt,...) NSLogColorZone(zone,(LCL_YELLOW fmt LCL_RESET),##__VA_ARGS__)
#define NSLogBlueZone(zone,fmt,...) NSLogColorZone(zone,(LCL_BLUE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogMagentaZone(zone,fmt,...) NSLogColor(zone,(LCL_MAGENTA fmt LCL_RESET),##__VA_ARGS__)
#define NSLogCyanZone(zone,fmt,...) NSLogColorZone(zone,(LCL_CYAN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogWhiteZone(zone,fmt,...) NSLogColorZone(zone,(LCL_WHITE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogResetZone(zone,...) NSLogColorZone(zone,LCL_RESET)
#define NSLogBlackDebug(fmt,...) NSLogColorDebug((LCL_BLACK fmt LCL_RESET),##__VA_ARGS__)
#define NSLogRedDebug(fmt,...) NSLogColorDebug((LCL_RED fmt LCL_RESET),##__VA_ARGS__)
#define NSLogGreenDebug(fmt,...) NSLogColorDebug((LCL_GREEN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogYellowDebug(fmt,...) NSLogColorDebug((LCL_YELLOW fmt LCL_RESET),##__VA_ARGS__)
#define NSLogBlueDebug(fmt,...) NSLogColorDebug((LCL_BLUE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogMagentaDebug(fmt,...) NSLogColorDebug((LCL_MAGENTA fmt LCL_RESET),##__VA_ARGS__)
#define NSLogCyanDebug(fmt,...) NSLogColorDebug((LCL_CYAN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogWhiteDebug(fmt,...) NSLogColorDebug((LCL_WHITE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogResetDebug(...) NSLogColorDebug(LCL_RESET)
#define NSLogBlackZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_BLACK fmt LCL_RESET),##__VA_ARGS__)
#define NSLogRedZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_RED fmt LCL_RESET),##__VA_ARGS__)
#define NSLogGreenZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_GREEN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogYellowZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_YELLOW fmt LCL_RESET),##__VA_ARGS__)
#define NSLogBlueZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_BLUE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogMagentaZoneDebug(zone,fmt,...) NSLogColorDebug(zone,(LCL_MAGENTA fmt LCL_RESET),##__VA_ARGS__)
#define NSLogCyanZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_CYAN fmt LCL_RESET),##__VA_ARGS__)
#define NSLogWhiteZoneDebug(zone,fmt,...) NSLogColorZoneDebug(zone,(LCL_WHITE fmt LCL_RESET),##__VA_ARGS__)
#define NSLogResetZoneDebug(zone,...) NSLogColorZoneDebug(zone,LCL_RESET)
#endif