This repository has been archived by the owner on May 5, 2019. It is now read-only.
forked from jy2wong/luakit
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlog.c
139 lines (119 loc) · 3.74 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
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
/*
* log.c - logging functions
*
* Copyright © 2016 Aidan Holm <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "globalconf.h"
#include "common/log.h"
#include "common/luaserialize.h"
#include "common/ipc.h"
#include <glib/gprintf.h>
#include <stdlib.h>
#include <unistd.h>
static log_level_t verbosity;
void
log_set_verbosity(log_level_t lvl)
{
verbosity = lvl;
}
log_level_t
log_get_verbosity(void)
{
return verbosity;
}
int
log_level_from_string(log_level_t *out, const char *str)
{
#define X(name) if (!strcmp(#name, str)) { \
*out = LOG_LEVEL_##name; \
return 0; \
}
LOG_LEVELS
#undef X
return 1;
}
void
_log(log_level_t lvl, const gchar *line, const gchar *fct, const gchar *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
va_log(lvl, line, fct, fmt, ap);
va_end(ap);
}
void
va_log(log_level_t lvl, const gchar *line, const gchar *fct, const gchar *fmt, va_list ap)
{
if (lvl > verbosity)
return;
gchar *msg = g_strdup_vprintf(fmt, ap);
gint log_fd = STDERR_FILENO;
/* Determine logging style */
/* TODO: move to X-macro generated table? */
gchar prefix_char, *style = "";
switch (lvl) {
case LOG_LEVEL_fatal: prefix_char = 'F'; style = ANSI_COLOR_BG_RED; break;
case LOG_LEVEL_error: prefix_char = 'E'; style = ANSI_COLOR_RED; break;
case LOG_LEVEL_warn: prefix_char = 'W'; style = ANSI_COLOR_YELLOW; break;
case LOG_LEVEL_info: prefix_char = 'I'; break;
case LOG_LEVEL_verbose: prefix_char = 'V'; break;
case LOG_LEVEL_debug: prefix_char = 'D'; break;
default: g_assert_not_reached();
}
/* Log format: [timestamp] prefix: fct:line msg */
#define LOG_FMT "[%#12f] %c: %s:%s: %s"
#define LOG_IND " "
/* Indent new lines within the message */
static GRegex *indent_lines_reg;
if (!indent_lines_reg) {
GError *err = NULL;
indent_lines_reg = g_regex_new("\n", G_REGEX_OPTIMIZE, 0, &err);
g_assert_no_error(err);
}
gchar *wrapped = g_regex_replace_literal(indent_lines_reg, msg, -1, 0, "\n" LOG_IND, 0, NULL);
g_free(msg);
msg = wrapped;
if (!isatty(log_fd)) {
gchar *stripped = strip_ansi_escapes(msg);
g_free(msg);
msg = stripped;
g_fprintf(stderr, LOG_FMT "\n",
l_time() - globalconf.starttime,
prefix_char, fct, line, msg);
} else {
g_fprintf(stderr, "%s" LOG_FMT ANSI_COLOR_RESET "\n",
style,
l_time() - globalconf.starttime,
prefix_char, fct, line, msg);
}
g_free(msg);
if (lvl == LOG_LEVEL_fatal)
exit(EXIT_FAILURE);
}
void
ipc_recv_log(ipc_endpoint_t *UNUSED(ipc), const guint8 *lua_msg, guint length)
{
lua_State *L = common.L;
gint n = lua_deserialize_range(L, lua_msg, length);
g_assert_cmpint(n, ==, 4);
log_level_t lvl = lua_tointeger(L, -4);
const gchar *line = lua_tostring(L, -3);
const gchar *fct = lua_tostring(L, -2);
const gchar *msg = lua_tostring(L, -1);
_log(lvl, line, fct, "%s", msg);
lua_pop(L, 4);
}
// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80