aboutsummaryrefslogtreecommitdiffstats
path: root/common/log.c
diff options
context:
space:
mode:
authorLibravatar Simon Ser <contact@emersion.fr>2020-03-05 13:54:28 +0100
committerLibravatar Drew DeVault <sir@cmpwn.com>2020-03-07 00:35:35 +0100
commite81d9fde667e7a06989b63f1091b81a12885f194 (patch)
tree6cffba12201d7ba8d3f31304f8e3f7232db26d2b /common/log.c
parentAdd adaptive_sync_status to output IPC reply (diff)
downloadsway-e81d9fde667e7a06989b63f1091b81a12885f194.tar.gz
sway-e81d9fde667e7a06989b63f1091b81a12885f194.tar.zst
sway-e81d9fde667e7a06989b63f1091b81a12885f194.zip
common/log: improve time prefix
Same as [1]. [1]: https://github.com/swaywm/wlroots/pull/2052
Diffstat (limited to 'common/log.c')
-rw-r--r--common/log.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/common/log.c b/common/log.c
index 17648e44..59cb9b71 100644
--- a/common/log.c
+++ b/common/log.c
@@ -36,6 +36,7 @@ bool _sway_assert(bool condition, const char *format, ...) {
36 36
37static bool colored = true; 37static bool colored = true;
38static sway_log_importance_t log_importance = SWAY_ERROR; 38static sway_log_importance_t log_importance = SWAY_ERROR;
39static struct timespec start_time = {-1, -1};
39 40
40static const char *verbosity_colors[] = { 41static const char *verbosity_colors[] = {
41 [SWAY_SILENT] = "", 42 [SWAY_SILENT] = "",
@@ -44,20 +45,39 @@ static const char *verbosity_colors[] = {
44 [SWAY_DEBUG ] = "\x1B[1;30m", 45 [SWAY_DEBUG ] = "\x1B[1;30m",
45}; 46};
46 47
48static void timespec_sub(struct timespec *r, const struct timespec *a,
49 const struct timespec *b) {
50 const long NSEC_PER_SEC = 1000000000;
51 r->tv_sec = a->tv_sec - b->tv_sec;
52 r->tv_nsec = a->tv_nsec - b->tv_nsec;
53 if (r->tv_nsec < 0) {
54 r->tv_sec--;
55 r->tv_nsec += NSEC_PER_SEC;
56 }
57}
58
59static void init_start_time(void) {
60 if (start_time.tv_sec >= 0) {
61 return;
62 }
63 clock_gettime(CLOCK_MONOTONIC, &start_time);
64}
65
47static void sway_log_stderr(sway_log_importance_t verbosity, const char *fmt, 66static void sway_log_stderr(sway_log_importance_t verbosity, const char *fmt,
48 va_list args) { 67 va_list args) {
68 init_start_time();
69
49 if (verbosity > log_importance) { 70 if (verbosity > log_importance) {
50 return; 71 return;
51 } 72 }
52 // prefix the time to the log message
53 struct tm result;
54 time_t t = time(NULL);
55 struct tm *tm_info = localtime_r(&t, &result);
56 char buffer[26];
57 73
58 // generate time prefix 74 struct timespec ts = {0};
59 strftime(buffer, sizeof(buffer), "%F %T - ", tm_info); 75 clock_gettime(CLOCK_MONOTONIC, &ts);
60 fprintf(stderr, "%s", buffer); 76 timespec_sub(&ts, &ts, &start_time);
77
78 fprintf(stderr, "%02d:%02d:%02d.%03ld ", (int)(ts.tv_sec / 60 / 60),
79 (int)(ts.tv_sec / 60 % 60), (int)(ts.tv_sec % 60),
80 ts.tv_nsec / 1000000);
61 81
62 unsigned c = (verbosity < SWAY_LOG_IMPORTANCE_LAST) ? verbosity : 82 unsigned c = (verbosity < SWAY_LOG_IMPORTANCE_LAST) ? verbosity :
63 SWAY_LOG_IMPORTANCE_LAST - 1; 83 SWAY_LOG_IMPORTANCE_LAST - 1;
@@ -75,6 +95,8 @@ static void sway_log_stderr(sway_log_importance_t verbosity, const char *fmt,
75} 95}
76 96
77void sway_log_init(sway_log_importance_t verbosity, terminate_callback_t callback) { 97void sway_log_init(sway_log_importance_t verbosity, terminate_callback_t callback) {
98 init_start_time();
99
78 if (verbosity < SWAY_LOG_IMPORTANCE_LAST) { 100 if (verbosity < SWAY_LOG_IMPORTANCE_LAST) {
79 log_importance = verbosity; 101 log_importance = verbosity;
80 } 102 }