Skip to content

Commit b00670a

Browse files
committed
refactor: Add file/line to tox-bootstrapd logging.
Also, allow trace logging in bootstrapd (disabled by default, but enabled on the websockify docker image so we can debug things there).
1 parent 712861f commit b00670a

File tree

14 files changed

+214
-119
lines changed

14 files changed

+214
-119
lines changed

other/bootstrap_daemon/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ RUN CC=clang cmake -B_build -H. \
4040
-DCMAKE_UNITY_BUILD=ON \
4141
-DCMAKE_BUILD_TYPE=Release \
4242
-DFULLY_STATIC=ON \
43-
-DMIN_LOGGER_LEVEL=DEBUG \
43+
-DMIN_LOGGER_LEVEL=TRACE \
4444
-DBUILD_TOXAV=OFF \
4545
-DBOOTSTRAP_DAEMON=ON && \
4646
cmake --build _build --target install
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
GIT_ROOT="$(git rev-parse --show-toplevel)"
6+
cd "$GIT_ROOT"
7+
8+
docker build -t toxchat/bootstrap-node -f other/bootstrap_daemon/docker/Dockerfile .
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9ec2993a28988bd147bf8f4f21a824c2fc5dbf7255e391b3ce517d337ebce5c1 /usr/local/bin/tox-bootstrapd
1+
a15af369870ab166f84554f04868ec27955304b2e9dea38f706ca5318f4ab6f4 /usr/local/bin/tox-bootstrapd

other/bootstrap_daemon/src/command_line_arguments.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
#include "command_line_arguments.h"
1111

12-
#include "global.h"
12+
#include "global.h" // IWYU pragma: keep
1313
#include "log.h"
1414

1515
#include "../../../toxcore/ccompat.h"
@@ -26,7 +26,7 @@ static void print_help(void)
2626
// 2 space indent
2727
// Make sure all lines fit into 80 columns
2828
// Make sure options are listed in alphabetical order
29-
log_write(LOG_LEVEL_INFO,
29+
LOG_WRITE(LOG_LEVEL_INFO,
3030
"Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
3131
"\n"
3232
"Options:\n"
@@ -43,6 +43,7 @@ static void print_help(void)
4343
" Default option when no --log-backend is\n"
4444
" specified.\n"
4545
" stdout Writes log messages to stdout/stderr.\n"
46+
" --trace Enable verbose network trace logging in toxcore.\n"
4647
" --version Print version information.\n");
4748
}
4849

@@ -51,7 +52,7 @@ Cli_Status handle_command_line_arguments(
5152
bool *run_in_foreground)
5253
{
5354
if (argc < 2) {
54-
log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
55+
LOG_WRITE(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
5556
print_help();
5657
return CLI_STATUS_ERROR;
5758
}
@@ -64,6 +65,7 @@ Cli_Status handle_command_line_arguments(
6465
{"help", no_argument, nullptr, 'h'},
6566
{"log-backend", required_argument, nullptr, 'l'}, // optional, defaults to syslog
6667
{"version", no_argument, nullptr, 'v'},
68+
{"trace", no_argument, nullptr, 't'},
6769
{nullptr, 0, nullptr, 0 }
6870
};
6971

@@ -99,24 +101,29 @@ Cli_Status handle_command_line_arguments(
99101
*log_backend = LOG_BACKEND_STDOUT;
100102
log_backend_set = true;
101103
} else {
102-
log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
104+
LOG_WRITE(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
103105
print_help();
104106
return CLI_STATUS_ERROR;
105107
}
106108

107109
break;
108110

109111
case 'v':
110-
log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
112+
LOG_WRITE(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
111113
return CLI_STATUS_DONE;
112114

115+
case 't':
116+
LOG_WRITE(LOG_LEVEL_INFO, "Enabling trace logging in toxcore.\n");
117+
log_enable_trace(true);
118+
break;
119+
113120
case '?':
114-
log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
121+
LOG_WRITE(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]);
115122
print_help();
116123
return CLI_STATUS_ERROR;
117124

118125
case ':':
119-
log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
126+
LOG_WRITE(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]);
120127
print_help();
121128
return CLI_STATUS_ERROR;
122129
}
@@ -127,7 +134,7 @@ Cli_Status handle_command_line_arguments(
127134
}
128135

129136
if (!cfg_file_path_set) {
130-
log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
137+
LOG_WRITE(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
131138
print_help();
132139
return CLI_STATUS_ERROR;
133140
}

other/bootstrap_daemon/src/config.c

Lines changed: 56 additions & 56 deletions
Large diffs are not rendered by default.

other/bootstrap_daemon/src/log.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
#define INVALID_BACKEND ((LOG_BACKEND)-1u)
1717
static LOG_BACKEND current_backend = INVALID_BACKEND;
18+
static bool log_toxcore_trace = false;
19+
20+
void log_enable_trace(bool enable)
21+
{
22+
log_toxcore_trace = enable;
23+
}
1824

1925
bool log_open(LOG_BACKEND backend)
2026
{
@@ -58,22 +64,27 @@ bool log_close(void)
5864
return true;
5965
}
6066

61-
bool log_write(LOG_LEVEL level, const char *format, ...)
67+
bool log_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, ...)
6268
{
6369
if (current_backend == INVALID_BACKEND) {
6470
return false;
6571
}
6672

73+
if (level == LOG_LEVEL_TRACE && !log_toxcore_trace) {
74+
// By default, no trace logging.
75+
return true;
76+
}
77+
6778
va_list args;
6879
va_start(args, format);
6980

7081
switch (current_backend) {
7182
case LOG_BACKEND_STDOUT:
72-
log_backend_stdout_write(level, format, args);
83+
log_backend_stdout_write(level, category, file, line, format, args);
7384
break;
7485

7586
case LOG_BACKEND_SYSLOG:
76-
log_backend_syslog_write(level, format, args);
87+
log_backend_syslog_write(level, category, file, line, format, args);
7788
break;
7889
}
7990

other/bootstrap_daemon/src/log.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ typedef enum LOG_BACKEND {
2020
} LOG_BACKEND;
2121

2222
typedef enum LOG_LEVEL {
23+
LOG_LEVEL_TRACE,
2324
LOG_LEVEL_INFO,
2425
LOG_LEVEL_WARNING,
2526
LOG_LEVEL_ERROR
2627
} LOG_LEVEL;
2728

29+
/**
30+
* Enables or disables logging of trace messages from toxcore.
31+
* @param enable true to enable, false to disable.
32+
*/
33+
void log_enable_trace(bool enable);
34+
2835
/**
2936
* Initializes logger.
3037
* @param backend Specifies which backend to use.
@@ -45,6 +52,13 @@ bool log_close(void);
4552
* @param ... Zero or more arguments, similar to printf function.
4653
* @return true on success, false if log is closed.
4754
*/
48-
bool log_write(LOG_LEVEL level, const char *format, ...) GNU_PRINTF(2, 3);
55+
bool log_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, ...) GNU_PRINTF(5, 6);
56+
57+
enum {
58+
LOG_PATH_PREFIX = sizeof(__FILE__) - sizeof("log.h")
59+
};
60+
61+
#define LOG_WRITEC(level, category, ...) log_write(level, category, &__FILE__[LOG_PATH_PREFIX], __LINE__, __VA_ARGS__)
62+
#define LOG_WRITE(level, ...) LOG_WRITEC(level, "tox.bootstrap", __VA_ARGS__)
4963

5064
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_H

other/bootstrap_daemon/src/log_backend_stdout.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
#include <stdarg.h>
1313
#include <stdio.h>
14+
#include <sys/time.h>
1415

16+
#include "../../../toxcore/ccompat.h"
1517
#include "log.h"
1618

1719
static FILE *log_backend_stdout_level(LOG_LEVEL level)
1820
{
1921
switch (level) {
22+
case LOG_LEVEL_TRACE: // intentional fallthrough
2023
case LOG_LEVEL_INFO:
2124
return stdout;
2225

@@ -28,8 +31,36 @@ static FILE *log_backend_stdout_level(LOG_LEVEL level)
2831
return stdout;
2932
}
3033

31-
void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args)
34+
static const char *log_level_string(LOG_LEVEL level)
3235
{
33-
vfprintf(log_backend_stdout_level(level), format, args);
34-
fflush(log_backend_stdout_level(level));
36+
switch (level) {
37+
case LOG_LEVEL_TRACE:
38+
return "Debug";
39+
40+
case LOG_LEVEL_INFO:
41+
return "Info";
42+
43+
case LOG_LEVEL_WARNING:
44+
return "Warning";
45+
46+
case LOG_LEVEL_ERROR:
47+
return "Critical"; // Qt-compatible.
48+
}
49+
50+
return "Debug"; // Just in case. Shouldn't happen.
51+
}
52+
53+
// Output bootstrap node log messages in the standard Tox log format:
54+
// [15:02:46.433 UTC] (tox.bootstrap) config.c:444 : Info: Successfully added bootstrap node ...
55+
void log_backend_stdout_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args)
56+
{
57+
struct timeval tv = {0};
58+
gettimeofday(&tv, nullptr);
59+
60+
FILE *stream = log_backend_stdout_level(level);
61+
fprintf(stream, "[%02d:%02d:%02d.%03d UTC] (%s) %s:%d : %s: ",
62+
(int)(tv.tv_sec / 3600 % 24), (int)(tv.tv_sec / 60 % 60), (int)(tv.tv_sec % 60), (int)(tv.tv_usec / 1000),
63+
category, file, line, log_level_string(level));
64+
vfprintf(stream, format, args);
65+
fflush(stream);
3566
}

other/bootstrap_daemon/src/log_backend_stdout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
#include "../../../toxcore/attributes.h"
1616
#include "log.h"
1717

18-
void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0);
18+
void log_backend_stdout_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args) GNU_PRINTF(2, 0);
1919

2020
#endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H

other/bootstrap_daemon/src/log_backend_syslog.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ void log_backend_syslog_close(void)
3333
static int log_backend_syslog_level(LOG_LEVEL level)
3434
{
3535
switch (level) {
36+
case LOG_LEVEL_TRACE:
37+
return LOG_DEBUG;
38+
3639
case LOG_LEVEL_INFO:
3740
return LOG_INFO;
3841

@@ -46,8 +49,13 @@ static int log_backend_syslog_level(LOG_LEVEL level)
4649
return LOG_INFO;
4750
}
4851

49-
void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args)
52+
void log_backend_syslog_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args)
5053
{
54+
if (level == LOG_LEVEL_TRACE) {
55+
// Don't write trace messages to syslog.
56+
return;
57+
}
58+
5159
va_list args2;
5260

5361
va_copy(args2, args);

0 commit comments

Comments
 (0)