forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
71 lines (57 loc) · 1.36 KB
/
log.c
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
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
static void log_stack_trace(void)
{
#if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
void *array[24];
size_t size;
char **strings;
size_t i;
size = backtrace(array, sizeof(array)/sizeof(array[0]));
strings = backtrace_symbols(array, size);
w_log(W_LOG_ERR, "Fatal error detected at:\n");
for (i = 0; i < size; i++) {
w_log(W_LOG_ERR, "%s\n", strings[i]);
}
free(strings);
#endif
}
int log_level = W_LOG_ERR;
void w_log(int level, const char *fmt, ...)
{
char buf[4096];
va_list ap;
int len;
uint32_t tid = (uint32_t)(uintptr_t)pthread_self();
bool fatal = false;
if (level == W_LOG_FATAL) {
level = W_LOG_ERR;
fatal = true;
}
len = snprintf(buf, sizeof(buf),
"%d: tid=%" PRIu32 " ", (int)time(NULL), tid);
va_start(ap, fmt);
vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
va_end(ap);
len = strlen(buf);
if (buf[len - 1] != '\n') {
if (len < (int)sizeof(buf) - 1) {
buf[len] = '\n';
buf[len + 1] = 0;
len++;
} else {
buf[len - 1] = '\n';
}
}
if (level <= log_level) {
ignore_result(write(STDERR_FILENO, buf, len));
}
w_log_to_clients(level, buf);
if (fatal) {
log_stack_trace();
abort();
}
}
/* vim:ts=2:sw=2:et:
*/