Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,8 @@ set(LIBNETDATA_FILES
src/libnetdata/log/nd_log-format-json.c
src/libnetdata/log/nd_log-to-file.c
src/libnetdata/log/nd_log-to-windows-events.c
src/libnetdata/log/nd_log-queue.c
src/libnetdata/log/nd_log-queue.h
src/libnetdata/string/utf8.c
src/libnetdata/spawn_server/log-forwarder.c
src/libnetdata/spawn_server/log-forwarder.h
Expand Down
4 changes: 4 additions & 0 deletions src/daemon/daemon-shutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ static void netdata_cleanup_and_exit(EXIT_REASON reason, bool abnormal, bool exi
fprintf(stderr, "All done, exiting...\n");
#endif

// Shutdown async logging queue, flushing any remaining messages
// This must be done late in shutdown to allow logging during cleanup
nd_log_shutdown();

if(!exit_when_done) {
curl_global_cleanup();
return;
Expand Down
24 changes: 23 additions & 1 deletion src/libnetdata/log/nd_log-init.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "nd_log-internals.h"
#include "nd_log-queue.h"

// --------------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -271,13 +272,30 @@ void nd_log_initialize(void) {
#if defined(HAVE_LIBBACKTRACE)
stacktrace_init();
#endif

// Initialize async logging queue
// This starts the background logger thread
nd_log_queue_init();
}

void nd_log_shutdown(void) {
// Shutdown async logging queue, flushing pending messages
nd_log_queue_shutdown();
}

void nd_log_reopen_log_files(bool log) {
if(log)
netdata_log_info("Reopening all log files.");

nd_log_initialize();
if (nd_log_queue_enabled()) {
// Async logging is active - use REOPEN opcode
// This is handled entirely in the logger thread, so no FILE* races
nd_log_queue_reopen();
} else {
// Async logging not active - reopen directly
for(size_t i = 0 ; i < _NDLS_MAX ; i++)
nd_log_open(&nd_log.sources[i], i);
}

if(log)
netdata_log_info("Log files re-opened.");
Expand All @@ -288,6 +306,10 @@ int nd_log_systemd_journal_fd(void) {
}

void nd_log_reopen_log_files_for_spawn_server(const char *name) {
// After fork, the logger thread doesn't exist. Disable async queue
// before anything else to prevent stale uv_async_send calls.
nd_log_queue_disown_after_fork();

nd_log.fatal_hook_cb = NULL;
nd_log.fatal_final_cb = NULL;

Expand Down
1 change: 1 addition & 0 deletions src/libnetdata/log/nd_log-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ bool nd_logger_syslog(int priority, ND_LOG_FORMAT format, struct log_field *fiel

bool nd_log_journal_systemd_init(void);
bool nd_log_journal_direct_init(const char *path);
void nd_logger_journal_format(BUFFER *wb, struct log_field *fields, size_t fields_max);
bool nd_logger_journal_direct(struct log_field *fields, size_t fields_max);
bool nd_logger_journal_libsystemd(struct log_field *fields, size_t fields_max);

Expand Down
Loading
Loading