-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathlogger.cpp
305 lines (266 loc) · 7.23 KB
/
logger.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (C) 2004-2021 Savoir-faire Linux Inc.
*
* Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
* Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <ctime>
#include <ciso646> // fix windows compiler bug
#include "client/ring_signal.h"
#ifdef _MSC_VER
#include <sys_time.h>
#else
#include <sys/time.h>
#endif
#include <string>
#include <sstream>
#include <iomanip>
#include <ios>
#include <mutex>
#include <thread>
#include <array>
#include "logger.h"
#ifdef __linux__
#include <syslog.h>
#include <unistd.h>
#include <sys/syscall.h>
#endif // __linux__
#ifdef __ANDROID__
#ifndef APP_NAME
#define APP_NAME "libdring"
#endif /* APP_NAME */
#endif
#define BLACK "\033[22;30m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define MAGENTA "\033[22;35m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_SCREEN "\033[01;32m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_MAGENTA "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"
#define END_COLOR "\033[0m"
#ifndef _WIN32
#define RED "\033[22;31m"
#define YELLOW "\033[01;33m"
#define CYAN "\033[22;36m"
#else
#define FOREGROUND_WHITE 0x000f
#define RED FOREGROUND_RED + 0x0008
#define YELLOW FOREGROUND_RED + FOREGROUND_GREEN + 0x0008
#define CYAN FOREGROUND_BLUE + FOREGROUND_GREEN + 0x0008
#define LIGHT_GREEN FOREGROUND_GREEN + 0x0008
#endif // _WIN32
#define LOGFILE "dring"
#ifdef RING_UWP
static constexpr auto ENDL = "";
#else
static constexpr auto ENDL = "\n";
#endif
static int consoleLog;
static int debugMode;
static std::mutex logMutex;
// extract the last component of a pathname (extract a filename from its dirname)
static const char*
stripDirName(const char* path)
{
#ifdef _MSC_VER
return strrchr(path, '\\') ? strrchr(path, '\\') + 1 : path;
#else
return strrchr(path, '/') ? strrchr(path, '/') + 1 : path;
#endif
}
static std::string
contextHeader(const char* const file, int line)
{
#ifdef __linux__
auto tid = syscall(__NR_gettid) & 0xffff;
#else
auto tid = std::this_thread::get_id();
#endif // __linux__
// Timestamp
unsigned int secs, milli;
struct timeval tv;
if (!gettimeofday(&tv, NULL)) {
secs = tv.tv_sec;
milli = tv.tv_usec / 1000; // suppose that milli < 1000
} else {
secs = time(NULL);
milli = 0;
}
std::ostringstream out;
const auto prev_fill = out.fill();
out << '[' << secs << '.' << std::right << std::setw(3) << std::setfill('0') << milli
<< std::left << '|' << std::right << std::setw(5) << std::setfill(' ') << tid << std::left;
out.fill(prev_fill);
// Context
if (file) {
#ifdef RING_UWP
constexpr auto width = 26;
#else
constexpr auto width = 18;
#endif
out << "|" << std::setw(width) << stripDirName(file) << ":" << std::setw(5)
<< std::setfill(' ') << line;
}
out << "] ";
return out.str();
}
void
setConsoleLog(int c)
{
if (c)
::closelog();
else {
#ifdef _WIN32
::openlog(LOGFILE, WINLOG_PID, WINLOG_MAIL);
#else
::openlog(LOGFILE, LOG_NDELAY, LOG_USER);
#endif /* _WIN32 */
}
consoleLog = c;
}
void
setDebugMode(int d)
{
debugMode = d;
}
int
getDebugMode(void)
{
return debugMode;
}
static const char*
check_error(int result, char* buffer)
{
switch (result) {
case 0:
return buffer;
case ERANGE: /* should never happen */
return "unknown (too big to display)";
default:
return "unknown (invalid error number)";
}
}
static const char*
check_error(char* result, char*)
{
return result;
}
void
strErr(void)
{
#ifdef __GLIBC__
JAMI_ERR("%m");
#else
char buf[1000];
JAMI_ERR("%s", check_error(strerror_r(errno, buf, sizeof(buf)), buf));
#endif
}
namespace jami {
void
Logger::log(int level, const char* file, int line, bool linefeed, const char* const format, ...)
{
#if defined(TARGET_OS_IOS) && TARGET_OS_IOS
if (!debugMode)
return;
#endif
if (!debugMode && level == LOG_DEBUG)
return;
va_list ap;
va_start(ap, format);
#ifdef __ANDROID__
__android_log_vprint(level, APP_NAME, format, ap);
#else
Logger::vlog(level, file, line, linefeed, format, ap);
#endif
va_end(ap);
}
void
Logger::vlog(
const int level, const char* file, int line, bool linefeed, const char* format, va_list ap)
{
#if defined(TARGET_OS_IOS) && TARGET_OS_IOS
if (!debugMode)
return;
#endif
if (!debugMode && level == LOG_DEBUG)
return;
// syslog is supposed to thread-safe, but not all implementations (Android?)
// follow strictly POSIX rules... so we lock our mutex in any cases.
std::lock_guard<std::mutex> lk {logMutex};
if (consoleLog) {
#ifndef _WIN32
const char* color_header = CYAN;
const char* color_prefix = "";
#else
WORD color_prefix = LIGHT_GREEN;
WORD color_header = CYAN;
#endif
#if defined(_WIN32) && !defined(RING_UWP)
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
#endif
switch (level) {
case LOG_ERR:
color_prefix = RED;
break;
case LOG_WARNING:
color_prefix = YELLOW;
break;
}
#ifndef _WIN32
fputs(color_header, stderr);
#elif !defined(RING_UWP)
GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
SetConsoleTextAttribute(hConsole, color_header);
#endif
fputs(contextHeader(file, line).c_str(), stderr);
#ifdef _MSC_VER
std::array<char, 4096> tmp;
vsnprintf(tmp.data(), tmp.size(), format, ap);
jami::emitSignal<DRing::DebugSignal::MessageSend>(contextHeader(file, line) + tmp.data());
#endif
#ifndef _WIN32
fputs(END_COLOR, stderr);
fputs(color_prefix, stderr);
#elif !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
SetConsoleTextAttribute(hConsole, color_prefix);
#endif
vfprintf(stderr, format, ap);
if (linefeed)
fputs(ENDL, stderr);
#ifndef _WIN32
fputs(END_COLOR, stderr);
#elif !defined(RING_UWP)
SetConsoleTextAttribute(hConsole, saved_attributes);
#endif
} else {
::vsyslog(level, format, ap);
}
}
} // namespace jami