Repository navigation
Expand file tree
/
Copy pathDebugLogging.m
More file actions
73 lines (65 loc) · 2.09 KB
/
Copy pathDebugLogging.m
File metadata and controls
73 lines (65 loc) · 2.09 KB
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
//
// DebugLogging.m
// iTerm
//
// Created by George Nachman on 10/13/13.
//
//
#import "DebugLogging.h"
#import <Cocoa/Cocoa.h>
#include <sys/time.h>
NSMutableString* gDebugLogStr = nil;
NSMutableString* gDebugLogStr2 = nil;
BOOL gDebugLogging = NO;
int gDebugLogFile = -1;
static void SwapDebugLog() {
NSMutableString* temp;
temp = gDebugLogStr;
gDebugLogStr = gDebugLogStr2;
gDebugLogStr2 = temp;
}
static void FlushDebugLog() {
NSData* data = [gDebugLogStr dataUsingEncoding:NSUTF8StringEncoding];
size_t written = write(gDebugLogFile, [data bytes], [data length]);
assert(written == [data length]);
[gDebugLogStr setString:@""];
}
int DebugLogImpl(const char *file, int line, const char *function, NSString* value)
{
if (gDebugLogging) {
struct timeval tv;
gettimeofday(&tv, NULL);
[gDebugLogStr appendFormat:@"%lld.%08lld %s:%d (%s): ", (long long)tv.tv_sec, (long long)tv.tv_usec, file, line, function];
[gDebugLogStr appendString:value];
[gDebugLogStr appendString:@"\n"];
if ([gDebugLogStr length] > 100000000) {
SwapDebugLog();
[gDebugLogStr2 setString:@""];
}
}
return 1;
}
void ToggleDebugLogging() {
if (!gDebugLogging) {
NSRunAlertPanel(@"Debug Logging Enabled",
@"Writing to /tmp/debuglog.txt",
@"OK", nil, nil);
gDebugLogFile = open("/tmp/debuglog.txt", O_TRUNC | O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
gDebugLogStr = [[NSMutableString alloc] init];
gDebugLogStr2 = [[NSMutableString alloc] init];
gDebugLogging = !gDebugLogging;
} else {
gDebugLogging = !gDebugLogging;
SwapDebugLog();
FlushDebugLog();
SwapDebugLog();
FlushDebugLog();
close(gDebugLogFile);
gDebugLogFile=-1;
NSRunAlertPanel(@"Debug Logging Stopped",
@"Please compress and send /tmp/debuglog.txt to the developers.",
@"OK", nil, nil);
[gDebugLogStr release];
[gDebugLogStr2 release];
}
}