|
23 | 23 | const NSString *MJPrintColorError = @"\033[1;31m";
|
24 | 24 | const NSString *MJPrintColorStrong = @"\033[1;32m";
|
25 | 25 |
|
| 26 | +#define MJBeginFormat \ |
| 27 | +if (!format) return; \ |
| 28 | +va_list args; \ |
| 29 | +va_start(args, format); \ |
| 30 | +format = [[NSString alloc] initWithFormat:format arguments:args]; |
| 31 | + |
| 32 | +#define MJEndFormat va_end(args); |
| 33 | + |
26 | 34 | @implementation MJPrintTools
|
27 | 35 |
|
28 | 36 | + (void)printError:(NSString *)format, ...
|
29 | 37 | {
|
| 38 | + MJBeginFormat; |
| 39 | + format = [@"Error: " stringByAppendingString:format]; |
30 | 40 | [self printColor:(NSString *)MJPrintColorError format:format];
|
31 | 41 | }
|
32 | 42 |
|
33 | 43 | + (void)printWarning:(NSString *)format, ...
|
34 | 44 | {
|
| 45 | + MJBeginFormat; |
| 46 | + format = [@"Warning: " stringByAppendingString:format]; |
35 | 47 | [self printColor:(NSString *)MJPrintColorWarning format:format];
|
| 48 | + MJEndFormat; |
36 | 49 | }
|
37 | 50 |
|
38 | 51 | + (void)printStrong:(NSString *)format, ...
|
39 | 52 | {
|
| 53 | + MJBeginFormat; |
40 | 54 | [self printColor:(NSString *)MJPrintColorStrong format:format];
|
| 55 | + MJEndFormat; |
41 | 56 | }
|
42 | 57 |
|
43 | 58 | + (void)print:(NSString *)format, ...
|
44 | 59 | {
|
| 60 | + MJBeginFormat; |
45 | 61 | [self printColor:nil format:format];
|
| 62 | + MJEndFormat; |
46 | 63 | }
|
47 | 64 |
|
48 | 65 | + (void)printColor:(NSString *)color format:(NSString *)format, ...
|
49 | 66 | {
|
50 |
| - if (!format) return; |
| 67 | + MJBeginFormat; |
51 | 68 |
|
52 |
| - va_list args; |
53 |
| - va_start(args, format); |
54 |
| - NSString *formatStr = [[NSString alloc] initWithFormat:format arguments:args]; |
55 | 69 | NSString *printStr = nil;
|
56 |
| - |
57 |
| - if (color) { |
58 |
| - printStr = [color stringByAppendingString:formatStr]; |
| 70 | + if (color && ![color isEqual:MJPrintColorDefault]) { |
| 71 | + printStr = [color stringByAppendingFormat:@"%@%@", format, MJPrintColorDefault]; |
59 | 72 | } else {
|
60 |
| - printStr = [MJPrintColorDefault stringByAppendingString:formatStr]; |
| 73 | + printStr = [MJPrintColorDefault stringByAppendingString:format]; |
61 | 74 | }
|
62 |
| - |
63 |
| - printStr = [printStr stringByAppendingString:(NSString *)MJPrintColorDefault]; |
64 |
| - |
65 | 75 | printf("%s", printStr.UTF8String);
|
66 |
| - va_end(args); |
| 76 | + |
| 77 | + MJEndFormat; |
67 | 78 | }
|
68 | 79 |
|
69 | 80 | @end
|
0 commit comments