forked from syscl/maclog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaclog.m
More file actions
executable file
·290 lines (264 loc) · 8.61 KB
/
maclog.m
File metadata and controls
executable file
·290 lines (264 loc) · 8.61 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// maclog.m
// maclog
//
// Created by lighting on 1/7/17.
// Copyright (c) 2017 syscl. All rights reserved.
//
// This work is licensed under the Creative Commons Attribution-NonCommercial
// 4.0 Unported License => http://creativecommons.org/licenses/by-nc/4.0
//
//
// syscl::header files
//
#include "maclog.h"
char *gCurTime(void)
{
char *gTime = calloc(11, sizeof(char));
time_t gRawTime = time(NULL);
struct tm *gTimeInf = localtime(&gRawTime);
sprintf(
gTime,
"%d-%d-%d",
gTimeInf->tm_year + 1900,
gTimeInf->tm_mon + 1,
gTimeInf->tm_mday
);
return gTime;
}
//
// Modified from https://stackoverflow.com/questions/3269321/osx-programmatically-get-uptime#answer-11676260
//
char *gBootTime(void)
{
struct timeval gBootTime;
size_t len = sizeof(gBootTime);
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
if (sysctl(mib, 2, &gBootTime, &len, NULL, 0) < 0)
{
printf("Failed to retrieve boot time.\n");
exit(EXIT_FAILURE);
}
char *gTime = calloc(20, sizeof(char));
struct tm *gTimeInf = localtime(&gBootTime.tv_sec);
sprintf(
gTime,
"%d-%d-%d %d:%d:%d",
gTimeInf->tm_year + 1900,
gTimeInf->tm_mon + 1,
gTimeInf->tm_mday,
gTimeInf->tm_hour,
gTimeInf->tm_min,
gTimeInf->tm_sec
);
return gTime;
}
//
// Modified from PowerManagement CommonLib.h `asl_object_t open_pm_asl_store()`
// https://opensource.apple.com/source/PowerManagement/PowerManagement-637.50.9/common/CommonLib.h.auto.html
// TODO: Sierra's PowerManager still uses the old ASL logging system, that's why we can do this.
// TODO: However I don't know if this will be the case on newer macOS versions.
// TODO: It would be great if someone with High Sierra (10.13), could test this and check if it still works.
//
asl_object_t searchPowerManagerASLStore(const char *key, const char *value)
{
size_t endMessageID;
asl_object_t list = asl_new(ASL_TYPE_LIST);
asl_object_t response = NULL;
if (list != NULL)
{
asl_object_t query = asl_new(ASL_TYPE_QUERY);
if (query != NULL)
{
if (asl_set_query(query, key, value, ASL_QUERY_OP_EQUAL) == 0)
{
asl_append(list, query);
asl_object_t pmStore = asl_open_path(kPMASLStorePath, 0);
if (pmStore != NULL)
{
response = asl_match(pmStore, list, &endMessageID, 0, 0, 0, ASL_MATCH_DIRECTION_FORWARD);
}
asl_release(pmStore);
}
asl_release(query);
}
asl_release(list);
}
return response;
}
char *gPowerManagerDomainTime(const char *domain)
{
asl_object_t logMessages = searchPowerManagerASLStore(kPMASLDomainKey, domain);
// Get last message
asl_reset_iteration(logMessages, SIZE_MAX);
aslmsg last = asl_prev(logMessages);
if (last == NULL) {
printf("Failed to retrieve %s time.\n", domain);
exit(EXIT_FAILURE);
}
long gMessageTime = atol(asl_get(last, ASL_KEY_TIME));
struct tm *gTimeInf = localtime(&gMessageTime);
char *gTime = calloc(20, sizeof(char));
sprintf(
gTime,
"%d-%d-%d %d:%d:%d",
gTimeInf->tm_year + 1900,
gTimeInf->tm_mon + 1,
gTimeInf->tm_mday,
gTimeInf->tm_hour,
gTimeInf->tm_min,
gTimeInf->tm_sec
);
return gTime;
}
void prepareLogArgv(int type) {
switch (type) {
case showLogArgv:
gLogArgs[gLogCommand] = "show";
gLogArgs[7] = "--info";
gLogArgs[8] = "--start";
break;
case streamLogArgv:
gLogArgs[gLogCommand] = "stream";
gLogArgs[7] = "--level";
gLogArgs[8] = "info";
break;
default:
printf("Failed to retrieve logs.\n");
exit(EXIT_FAILURE);
}
}
void printHelpAndExit (int exitStatus) {
printf(
"USAGE: maclog [--version|-v] [--help|-h]\n"
"USAGE: maclog [logMode] [-f|--filter=<query>]\n"
"Arguments:\n"
" --help, -h Show maclog help info.\n"
" --version, -v Show maclog version info.\n"
"Log Modes:\n"
" --boot, -b Show log messages since last boot time.\n"
" --darkWake, -d Show log messages since last darkWake time.\n"
" --sleep, -s Show log messages since last sleep time.\n"
" --stream, -S Show log messages in real time.\n"
" --wake, -w Show log messages since last wake time.\n"
"Filter:\n"
" --filter, -f Show log messages filtered by the <query>.\n"
"NOTE: The default behaviour is to show all log messages of the current day.\n"
"NOTE: The messages returned by \e[1m--boot\e[0m, \e[1m--sleep\e[0m, "
"\e[1m--wake\e[0m, \e[1m--darkWake\e[0m can be from previous days,"
" depending on the last time each action occurred.\n"
"NOTE: The \e[1m--stream\e[0m option results in the maclog process being never finished,"
" due to the necessity of redirecting all logs to Console in real-time.\n"
);
exit(exitStatus);
}
int main(int argc, char **argv)
{
int mode = 0;
int filterFlag = 0;
int optionIndex = 0;
int currentOption = getopt_long (argc, argv, shortOptions, longOptions, &optionIndex);
//
// Handle arguments
//
while (currentOption != -1)
{
switch (currentOption) {
case 'f':
if (filterFlag) {
printf("ERROR: Filter argument should only appear once.\n");
exit(EXIT_FAILURE);
}
filterFlag = 1;
gLogArgs[gLogFilter] = calloc(sizeof(predicate filterConcat) + strlen(optarg), sizeof(char));
sprintf(gLogArgs[gLogFilter], predicate filterConcat "%s", optarg);
break;
case 'h':
printHelpAndExit(EXIT_SUCCESS);
case 'v':
printf("v%.1f (c) 2017 syscl/lighting/Yating Zhou\n", PROGRAM_VER);
exit(EXIT_SUCCESS);
case '?':
printHelpAndExit(EXIT_FAILURE);
default:
if (mode) {
printf("ERROR: Different modes can't be mixed.\n");
printHelpAndExit(EXIT_FAILURE);
}
mode = currentOption;
break;
}
currentOption = getopt_long (argc, argv, shortOptions, longOptions, &optionIndex);
}
pid_t rc;
if ((rc = fork()) > 0)
{
//
// parent process
//
//
// create the log file
//
int fd = open(gLogPath, O_CREAT | O_TRUNC | O_RDWR, PERMS);
switch (mode) {
case 0:
prepareLogArgv(showLogArgv);
gLogArgs[gLogTime] = gCurTime();
break;
case 'b':
prepareLogArgv(showLogArgv);
gLogArgs[gLogTime] = gBootTime();
break;
case 'd':
prepareLogArgv(showLogArgv);
gLogArgs[gLogTime] = gPowerManagerDomainTime(kPMASLDomainPMDarkWake);
break;
case 's':
prepareLogArgv(showLogArgv);
gLogArgs[gLogTime] = gPowerManagerDomainTime(kPMASLDomainPMSleep);
break;
case 'S':
prepareLogArgv(streamLogArgv);
break;
case 'w':
prepareLogArgv(showLogArgv);
gLogArgs[gLogTime] = gPowerManagerDomainTime(kPMASLDomainPMWake);
break;
default:
if(access(gLogPath, F_OK) == 0) unlink(gLogPath);
printf("ERROR: Invalid mode: %c\n", mode);
printHelpAndExit(EXIT_FAILURE);
}
//
// log the log file
//
if (fd >= 0)
{
if (dup2(fd, STDOUT_FILENO) < 0) {
printf("ERROR: Failed to retrieve logs.\n");
exit(EXIT_FAILURE);
}
}
//
// log system log now
//
execvp(gLogArgs[0], gLogArgs);
}
else if (rc == 0)
{
//
// child process
//
wait(NULL);
if(access(gLogPath, F_OK) == 0) execvp(gOpenf[0], gOpenf);
}
else
{
//
// fork failed
//
printf("ERROR: Fork failed\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}