Skip to content

Commit fc0a7f9

Browse files
committed
Fix #63 - Add -t flag to prepend timestamps in stdout stream logs
1 parent 5273bee commit fc0a7f9

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

fsmon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct filemonitor_t {
5454
bool jsonStream;
5555
volatile sig_atomic_t running;
5656
bool fileonly;
57+
bool show_timestamps;
5758
uint64_t count;
5859
void (*control_c)();
5960
struct filemonitor_backend_t backend;

main.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ static uint64_t __sys_now(void) {
7070
return ret;
7171
}
7272

73+
static void time_ymdhms(char *buf, size_t buflen) {
74+
struct timeval now;
75+
gettimeofday (&now, NULL);
76+
struct tm *tm_info = localtime (&now.tv_sec);
77+
char time_buf[20];
78+
strftime (time_buf, buflen, "%Y%m%d-%H:%M:%S", tm_info);
79+
// Append milliseconds
80+
int millisec = now.tv_usec / 1000;
81+
snprintf(buf, buflen, "%s.%03d", time_buf, millisec);
82+
}
83+
7384
static bool callback(FileMonitor *fm, FileMonitorEvent *ev) {
7485
if (fm->child) {
7586
if (fm->pid && ev->pid != fm->pid) {
@@ -169,6 +180,11 @@ static bool callback(FileMonitor *fm, FileMonitorEvent *ev) {
169180
const char *color_begin = colorful? fm_colorstr (ev->type): "";
170181
const char *color_begin2 = colorful? Color_MAGENTA: "";
171182
const char *color_end = colorful? Color_RESET: "";
183+
if (fm->show_timestamps) {
184+
char datetime[20];
185+
time_ymdhms (datetime, sizeof (datetime));
186+
printf ("%s ", datetime);
187+
}
172188
// TODO . show event type
173189
if (ev->type == FSE_RENAME) {
174190
printf ("%s%s%s\t%d\t\"%s%s%s\"\t%s -> %s\n",
@@ -221,10 +237,11 @@ static void help (const char *argv0) {
221237
" -h show this help\n"
222238
" -j output in JSON format\n"
223239
" -J output in JSON stream format\n"
224-
" -n do not use colors\n"
225240
" -L list all filemonitor backends\n"
241+
" -n do not use colors\n"
226242
" -p [pid] only show events from this pid\n"
227243
" -P [proc] events only from process name\n"
244+
" -t show timestamps in default logs\n"
228245
" -v show version\n"
229246
" [path] only get events from this path\n"
230247
"Examples:\n"
@@ -261,7 +278,7 @@ int main (int argc, char **argv) {
261278
fm.backend = fmb_inotify;
262279
#endif
263280

264-
while ((c = getopt (argc, argv, "a:chb:B:d:fjJlLnp:P:v")) != -1) {
281+
while ((c = getopt (argc, argv, "a:chb:B:d:fjJlLnp:P:vt")) != -1) {
265282
switch (c) {
266283
case 'a':
267284
fm.alarm = atoi (optarg);
@@ -285,6 +302,9 @@ int main (int argc, char **argv) {
285302
case 'f':
286303
fm.fileonly = true;
287304
break;
305+
case 't':
306+
fm.show_timestamps = true;
307+
break;
288308
case 'j':
289309
fm.json = true;
290310
break;

0 commit comments

Comments
 (0)