forked from fxfactorial/ondeviceconsole
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mm
250 lines (206 loc) · 6.86 KB
/
main.mm
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#import <sys/socket.h>
#import <sys/un.h>
#import <unistd.h>
#import <fcntl.h>
#import <poll.h>
// C++ libs
#include <iostream>
#include <string>
#include <tclap/CmdLine.h>
#define SOCKET_PATH "/var/run/lockdown/syslog.sock"
#define COLOR_RESET "\e[m"
#define COLOR_NORMAL "\e[0m"
#define COLOR_DARK "\e[2m"
#define COLOR_RED "\e[0;31m"
#define COLOR_DARK_RED "\e[2;31m"
#define COLOR_GREEN "\e[0;32m"
#define COLOR_DARK_GREEN "\e[2;32m"
#define COLOR_YELLOW "\e[0;33m"
#define COLOR_DARK_YELLOW "\e[2;33m"
#define COLOR_BLUE "\e[0;34m"
#define COLOR_DARK_BLUE "\e[2;34m"
#define COLOR_MAGENTA "\e[0;35m"
#define COLOR_DARK_MAGENTA "\e[2;35m"
#define COLOR_CYAN "\e[0;36m"
#define COLOR_DARK_CYAN "\e[2;36m"
#define COLOR_WHITE "\e[0;37m"
#define COLOR_DARK_WHITE "\e[0;37m"
std::string program_filter = "";
size_t atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
{
char *s = (char*)_s;
size_t pos = 0;
ssize_t res;
struct pollfd pfd;
pfd.fd = fd;
pfd.events = f == read ? POLLIN : POLLOUT;
while (n > pos) {
res = (f) (fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR)
continue;
if ((errno == EAGAIN) || (errno == ENOBUFS)) {
(void)poll(&pfd, 1, -1);
continue;
}
return 0;
case 0:
errno = EPIPE;
return pos;
default:
pos += (size_t)res;
}
}
return pos;
}
int unix_connect(char* path) {
struct sockaddr_un sun;
int s;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
return (-1);
(void)fcntl(s, F_SETFD, 1);
memset(&sun, 0, sizeof(struct sockaddr_un));
sun.sun_family = AF_UNIX;
if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
close(s);
errno = ENAMETOOLONG;
return (-1);
}
if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
close(s);
return (-1);
}
return (s);
}
#define LINE_REGEX "(\\w+\\s+\\d+\\s+\\d+:\\d+:\\d+)\\s+(\\S+|)\\s+(\\w+)\\[(\\d+)\\]\\s+\\<(\\w+)\\>:\\s(.*)"
ssize_t write_colored(int fd, void* buffer, size_t len) {
char *escapedBuffer = (char*)malloc(len + 1);
memcpy(escapedBuffer, buffer, len);
escapedBuffer[len] = '\0';
NSString *str = [NSString stringWithUTF8String:escapedBuffer];
free(escapedBuffer);
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@LINE_REGEX
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:str
options:0
range:NSMakeRange(0, [str length])];
if ([matches count] == 0)
return write(fd, buffer, len);
for (NSTextCheckingResult *match in matches) {
if ([match numberOfRanges] < 6) {
write(fd, buffer, len); // if entry doesn't match regex, print uncolored
continue;
}
NSRange processRange = [match rangeAtIndex:3];
NSString *process = [str substringWithRange:processRange];
std::string cpp_proc_string = std::string{[process UTF8String]};
if (program_filter == "" || cpp_proc_string == program_filter) {
NSRange dateRange = [match rangeAtIndex:1];
NSRange deviceRange = [match rangeAtIndex:2];
NSRange pidRange = [match rangeAtIndex:4];
NSRange typeRange = [match rangeAtIndex:5];
NSRange logRange = [match rangeAtIndex:6];
NSString *date = [str substringWithRange:dateRange];
NSString *device = [str substringWithRange:deviceRange];
NSString *pid = [str substringWithRange:pidRange];
NSString *type = [str substringWithRange:typeRange];
NSString *log = [str substringWithRange:
NSMakeRange(logRange.location,
[str length] - logRange.location)];
log = [log stringByTrimmingCharactersInSet:
[NSCharacterSet newlineCharacterSet]];
NSMutableString *build = [NSMutableString new];
[build appendString:@COLOR_DARK_WHITE];
[build appendString:date];
[build appendString:@" "];
[build appendString:device];
[build appendString:@" "];
[build appendString:@COLOR_CYAN];
[build appendString:process];
[build appendString:@"["];
[build appendString:pid];
[build appendString:@"]"];
char *typeColor = (char*)COLOR_DARK_WHITE;
char *darkTypeColor = (char*)COLOR_DARK_WHITE;
if ([type isEqualToString:@"Notice"]) {
typeColor = (char*)COLOR_GREEN;
darkTypeColor = (char*)COLOR_DARK_GREEN;
} else if ([type isEqualToString:@"Warning"]) {
typeColor = (char*)COLOR_YELLOW;
darkTypeColor = (char*)COLOR_DARK_YELLOW;
} else if ([type isEqualToString:@"Error"]) {
typeColor = (char*)COLOR_RED;
darkTypeColor = (char*)COLOR_DARK_RED;
} else if ([type isEqualToString:@"Debug"]) {
typeColor = (char*)COLOR_MAGENTA;
darkTypeColor = (char*)COLOR_DARK_MAGENTA;
}
[build appendString:@(darkTypeColor)];
[build appendString:@" <"];
[build appendString:@(typeColor)];
[build appendString:type];
[build appendString:@(darkTypeColor)];
[build appendString:@">"];
[build appendString:@COLOR_RESET];
[build appendString:@": "];
[build appendString:log];
std::cout << [build UTF8String] << std::endl;
[build release];
}
}
return len;
}
int main(int argc, char **argv, char **envp) {
try {
TCLAP::CmdLine
cmd{"ondeviceconsole lets you see the system log with colored output. "
"Filter by program name as well using the -p argument. "
"Example onconsoledevice -p CommCenter",
' ',
"1.1.0"};
TCLAP::ValueArg<std::string>
prog_filter{"p", "program", "Only show for this program", false, "", "string"};
cmd.add(prog_filter);
cmd.parse(argc, argv);
program_filter = prog_filter.getValue();
int nfd = unix_connect((char*)SOCKET_PATH);
// write "watch" command to socket to begin receiving messages
write(nfd, "watch\n", 6);
struct pollfd pfd[2];
unsigned char buf[16384];
int n = fileno(stdin);
int lfd = fileno(stdout);
int plen = 16384;
pfd[0].fd = nfd;
pfd[0].events = POLLIN;
while (pfd[0].fd != -1) {
if ((n = poll(pfd, 1, -1)) < 0) {
close(nfd);
perror("polling error");
exit(1);
}
if (pfd[0].revents & POLLIN) {
if ((n = read(nfd, buf, plen)) < 0)
/* possibly not an error, just disconnection */
perror("read error"), exit(1);
else if (n == 0) {
shutdown(nfd, SHUT_RD);
pfd[0].fd = -1;
pfd[0].events = 0;
} else {
if (atomicio(write_colored, lfd, buf, n) != n)
perror("atomicio failure"), exit(1);
}
}
}
return 0;
}
catch (TCLAP::ArgException &e) {
std::cerr << "Error: " << e.error() << std::endl;
return 0;
}
}