forked from matux/sikit
/
SILogging.m
134 lines (105 loc) · 4.76 KB
/
SILogging.m
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
//
// SILogging.m
// SIKit
//
// Created by Matias Pequeno on 7/8/12.
// Copyright (c) 2012 Silicon Illusions, Inc. All rights reserved.
//
#import "SILogging.h"
#import "SIUtil.h"
#import <stdio.h>
void compactLog(NSString *format, ...)
{
NSMutableString *s;
if (format) {
// Get a reference to the arguments that follow the format parameter
va_list argList;
va_start(argList, format);
// Perform format string argument substitution, reinstate %% escapes, then print
s = [[NSMutableString alloc] initWithFormat:format arguments:argList];
[s replaceOccurrencesOfString:@"%%" withString:@"%%%%" options:0 range:NSMakeRange(0, [s length])];
va_end(argList);
} else {
s = [[NSMutableString alloc] initWithString:@"\n"];
}
if( !LOGGING_DISABLE_LOCAL_OUTPUT ) {
//printf("%s\n", [s UTF8String]);
fprintf(stderr, "%s\n", [s UTF8String]);
}
#if LOGGING_FORWARD_TO_TEST_FLIGHT
TFLog(@"%@", s);
#endif
#if LOGGING_FORWARD_TO_CRASHLYTICS
CLSLog(@"%@", s);
#endif
[s release];
}
void compactLogAltFormat(NSString *namespace, int indentLevel, NSString *functionName, NSString *format, ...)
{
// Showing namespace + level + thread
NSString *prefix = [NSString stringWithFormat:@"[%@] %02d (%-*s", namespace, indentLevel, ((indentLevel*2)+3),
[[NSString stringWithFormat:@"%@):", [[NSThread currentThread] name]] cStringUsingEncoding:NSASCIIStringEncoding]];
// Parse function name
// if it starts with __nn, it means this is a block callback
if( [functionName characterAtIndex:0] == '_' ) {
// Parse callee
NSRange functionRange = [functionName rangeOfString:@"]" options:NSBackwardsSearch];
if( functionRange.length ) {
functionName = [functionName substringWithRange:NSMakeRange(4, functionRange.location - 3)];
}
// Callback caller will be the 2nd object in the array
NSString *callerName = [NSThread callStackSymbols][2];
// Parse the caller call stack symbol
BOOL isCCall = NO;
NSRange callerRange = [callerName rangeOfString:@"[" options:NSBackwardsSearch];
// If [ not found, it may be a C call
if( !callerRange.length ) {
callerRange = [callerName rangeOfString:@"_"];
isCCall = callerRange.length; // != 0 == YES;
}
// If it is either an ObjC or a C call, parse it
if( callerRange.length ) {
callerName = [callerName substringWithRange:NSMakeRange(callerRange.location - 1, ([callerName length] - callerRange.location))];
callerRange = [callerName rangeOfString:(isCCall?@"+":@"]") options:NSBackwardsSearch];
if( callerRange.length )
callerName = [callerName substringWithRange:NSMakeRange(0, callerRange.location + (isCCall?-1:1))];
}
// block in
NSString *blockInString = isCCall ? @"()__block_in" : @"__block_in";
// Build the absolute function name
functionName = [NSString stringWithFormat:@"%@%@(%@)", callerName, blockInString, functionName];
}
if( format ) {
// Get a reference to the arguments that follow the format parameter
va_list argList;
va_start(argList, format);
// Perform format string argument substitution, reinstate %% escapes, then print
NSMutableString *formattedString = [[NSMutableString alloc] initWithFormat:format arguments:argList];
[formattedString replaceOccurrencesOfString:@"%%" withString:@"%%%%" options:0 range:NSMakeRange(0, [formattedString length])];
va_end(argList);
if( !LOGGING_DISABLE_LOCAL_OUTPUT ) {
//printf("%s%s -> %s\n", [prefix UTF8String], [functionName UTF8String], [formattedString UTF8String]);
fprintf(stderr, "%s%s -> %s\n", [prefix UTF8String], [functionName UTF8String], [formattedString UTF8String]);
}
#if LOGGING_FORWARD_TO_TEST_FLIGHT
TFLog(@"%@%@ -> %@", prefix, functionName, formattedString);
#endif
#if LOGGING_FORWARD_TO_CRASHLYTICS
CLSLog(@"%@%@ -> %@", prefix, functionName, formattedString);
#endif
[formattedString release];
}
else
{
if( !LOGGING_DISABLE_LOCAL_OUTPUT ) {
//printf("%s%s\n", [prefix UTF8String], [functionName UTF8String]);
fprintf(stderr, "%s%s\n", [prefix UTF8String], [functionName UTF8String]);
}
#if LOGGING_FORWARD_TO_TEST_FLIGHT
TFLog(@"%@%@", prefix, functionName);
#endif
#if LOGGING_FORWARD_TO_CRASHLYTICS
CLSLog(@"%@%@", prefix, functionName);
#endif
}
}