diff options
author | Calvin Lee <cyrus296@gmail.com> | 2017-10-20 15:12:28 -0600 |
---|---|---|
committer | Calvin Lee <cyrus296@gmail.com> | 2017-10-20 15:12:28 -0600 |
commit | 016261fd642412dec789a0a92b7a5511d9fe09da (patch) | |
tree | 03dcc9850e583a10fb4c8cdfd11b6d7feb7bb2c1 /common/log.c | |
parent | Remove broken link to Spanish translation (diff) | |
download | sway-016261fd642412dec789a0a92b7a5511d9fe09da.tar.gz sway-016261fd642412dec789a0a92b7a5511d9fe09da.tar.zst sway-016261fd642412dec789a0a92b7a5511d9fe09da.zip |
Print log level even if STDERR is not a tty
Makes reading debug logs much easier, debug lines will start with `E`,
info lines with `I` and error lines with `E`.
Diffstat (limited to 'common/log.c')
-rw-r--r-- | common/log.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/common/log.c b/common/log.c index af1bdc3f..6dc9d743 100644 --- a/common/log.c +++ b/common/log.c | |||
@@ -22,6 +22,12 @@ static const char *verbosity_colors[] = { | |||
22 | [L_INFO ] = "\x1B[1;34m", | 22 | [L_INFO ] = "\x1B[1;34m", |
23 | [L_DEBUG ] = "\x1B[1;30m", | 23 | [L_DEBUG ] = "\x1B[1;30m", |
24 | }; | 24 | }; |
25 | static const char verbosity_chars[] = { | ||
26 | [L_SILENT] = '\0', | ||
27 | [L_ERROR ] = 'E', | ||
28 | [L_INFO ] = 'I', | ||
29 | [L_DEBUG ] = 'D', | ||
30 | }; | ||
25 | 31 | ||
26 | void init_log(log_importance_t verbosity) { | 32 | void init_log(log_importance_t verbosity) { |
27 | if (verbosity != L_DEBUG) { | 33 | if (verbosity != L_DEBUG) { |
@@ -62,6 +68,16 @@ void _sway_vlog(const char *filename, int line, log_importance_t verbosity, | |||
62 | static struct tm *tm_info; | 68 | static struct tm *tm_info; |
63 | char buffer[26]; | 69 | char buffer[26]; |
64 | 70 | ||
71 | unsigned int c = verbosity; | ||
72 | if (c > sizeof(verbosity_colors) / sizeof(char *) - 1) { | ||
73 | c = sizeof(verbosity_colors) / sizeof(char *) - 1; | ||
74 | } | ||
75 | |||
76 | // First, if not printing color, show the log level | ||
77 | if (!(colored && isatty(STDERR_FILENO)) && c != L_SILENT) { | ||
78 | fprintf(stderr, "%c: ", verbosity_chars[c]); | ||
79 | } | ||
80 | |||
65 | // get current time | 81 | // get current time |
66 | t = time(NULL); | 82 | t = time(NULL); |
67 | // convert time to local time (determined by the locale) | 83 | // convert time to local time (determined by the locale) |
@@ -70,11 +86,6 @@ void _sway_vlog(const char *filename, int line, log_importance_t verbosity, | |||
70 | strftime(buffer, sizeof(buffer), "%x %X - ", tm_info); | 86 | strftime(buffer, sizeof(buffer), "%x %X - ", tm_info); |
71 | fprintf(stderr, "%s", buffer); | 87 | fprintf(stderr, "%s", buffer); |
72 | 88 | ||
73 | unsigned int c = verbosity; | ||
74 | if (c > sizeof(verbosity_colors) / sizeof(char *) - 1) { | ||
75 | c = sizeof(verbosity_colors) / sizeof(char *) - 1; | ||
76 | } | ||
77 | |||
78 | if (colored && isatty(STDERR_FILENO)) { | 89 | if (colored && isatty(STDERR_FILENO)) { |
79 | fprintf(stderr, "%s", verbosity_colors[c]); | 90 | fprintf(stderr, "%s", verbosity_colors[c]); |
80 | } | 91 | } |
@@ -124,6 +135,8 @@ void sway_log_errno(log_importance_t verbosity, char* format, ...) { | |||
124 | 135 | ||
125 | if (colored && isatty(STDERR_FILENO)) { | 136 | if (colored && isatty(STDERR_FILENO)) { |
126 | fprintf(stderr, "%s", verbosity_colors[c]); | 137 | fprintf(stderr, "%s", verbosity_colors[c]); |
138 | } else if (c != L_SILENT) { | ||
139 | fprintf(stderr, "%c: ", verbosity_chars[c]); | ||
127 | } | 140 | } |
128 | 141 | ||
129 | va_list args; | 142 | va_list args; |