logger changes from global static variables to one instance per stellar

This commit is contained in:
luwenpeng
2024-08-23 18:44:17 +08:00
parent 8db2e70c38
commit 4e524a8781
23 changed files with 306 additions and 252 deletions

View File

@@ -8,21 +8,21 @@
#include <pthread.h>
#include <sys/prctl.h>
#include "log.h"
#include "utils.h"
#include "packet_io.h"
#include "packet_private.h"
#include "session_private.h"
#include "snowflake.h"
#include "packet_io.h"
#include "log_private.h"
#include "stellar_stat.h"
#include "stellar_core.h"
#include "stellar_config.h"
#include "packet_private.h"
#include "plugin_manager.h"
#include "session_private.h"
#include "session_manager.h"
#define STELLAR_LOG_FATAL(format, ...) LOG_FATAL("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
#define CORE_LOG_FATAL(format, ...) STELLAR_LOG_FATAL(__thread_local_logger, "core", format, ##__VA_ARGS__)
#define CORE_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "core", format, ##__VA_ARGS__)
#define CORE_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "core", format, ##__VA_ARGS__)
#ifdef STELLAR_GIT_VERSION
static __attribute__((__used__)) const char *version = STELLAR_GIT_VERSION;
@@ -69,6 +69,7 @@ struct stellar_runtime
{
uint64_t need_exit;
uint64_t stat_last_output_ts;
struct logger *logger;
struct stellar_stat *stat;
struct packet_io *packet_io;
struct plugin_manager_schema *plug_mgr;
@@ -197,6 +198,7 @@ static void *work_thread(void *arg)
uint16_t thr_idx = thread->idx;
__current_thread_idx = thr_idx;
__thread_local_logger = runtime->logger;
__current_thread_snowflake = thread->snowflake;
memset(packets, 0, sizeof(packets));
@@ -211,12 +213,12 @@ static void *work_thread(void *arg)
if (packet_io_init(packet_io, thr_idx) != 0)
{
STELLAR_LOG_ERROR("unable to init marsio thread");
CORE_LOG_ERROR("unable to init marsio thread");
return NULL;
}
ATOMIC_SET(&thread->is_runing, 1);
STELLAR_LOG_FATAL("worker thread %d runing", thr_idx);
CORE_LOG_FATAL("worker thread %d runing", thr_idx);
while (ATOMIC_READ(&runtime->need_exit) == 0)
{
@@ -358,7 +360,7 @@ static void *work_thread(void *arg)
}
ATOMIC_SET(&thread->is_runing, 0);
STELLAR_LOG_FATAL("worker thread %d exit", thr_idx);
CORE_LOG_FATAL("worker thread %d exit", thr_idx);
return NULL;
}
@@ -416,20 +418,20 @@ static int stellar_thread_init(struct stellar *st)
thread->snowflake = snowflake_new(i, config->snowflake_opts.snowflake_base, config->snowflake_opts.snowflake_offset);
if (thread->snowflake == NULL)
{
STELLAR_LOG_ERROR("unable to create snowflake id generator");
CORE_LOG_ERROR("unable to create snowflake id generator");
return -1;
}
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms);
if (thread->sess_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create session manager");
CORE_LOG_ERROR("unable to create session manager");
return -1;
}
session_manager_set_session_id_generator(thread->sess_mgr, stellar_generate_session_id);
thread->ip_mgr = ip_reassembly_new(&config->ip_reass_opts);
if (thread->ip_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create ip reassemble manager");
CORE_LOG_ERROR("unable to create ip reassemble manager");
return -1;
}
thread->st = st;
@@ -443,7 +445,7 @@ static void stellar_thread_clean(struct stellar *st)
struct stellar_runtime *runtime = &st->runtime;
struct stellar_config *config = &st->config;
STELLAR_LOG_FATAL("cleaning worker thread context ...");
CORE_LOG_FATAL("cleaning worker thread context ...");
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &runtime->threads[i];
@@ -454,7 +456,7 @@ static void stellar_thread_clean(struct stellar *st)
snowflake_free(thread->snowflake);
}
}
STELLAR_LOG_FATAL("worker thread context cleaned");
CORE_LOG_FATAL("worker thread context cleaned");
}
static int stellar_thread_run(struct stellar *st)
@@ -467,7 +469,7 @@ static int stellar_thread_run(struct stellar *st)
struct stellar_thread *thread = &runtime->threads[i];
if (pthread_create(&thread->tid, NULL, work_thread, (void *)thread) < 0)
{
STELLAR_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
CORE_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
return -1;
}
}
@@ -480,7 +482,7 @@ static void stellar_thread_join(struct stellar *st)
struct stellar_runtime *runtime = &st->runtime;
struct stellar_config *config = &st->config;
STELLAR_LOG_FATAL("waiting worker thread stop ...");
CORE_LOG_FATAL("waiting worker thread stop ...");
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &runtime->threads[i];
@@ -489,7 +491,7 @@ static void stellar_thread_join(struct stellar *st)
usleep(1000); // 1ms
}
}
STELLAR_LOG_FATAL("all worker thread stoped");
CORE_LOG_FATAL("all worker thread stoped");
}
struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg_file, const char *log_cfg_file)
@@ -523,19 +525,21 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg
struct stellar_runtime *runtime = &st->runtime;
struct stellar_config *config = &st->config;
if (log_init(st->log_cfg_file) != 0)
runtime->logger = log_new(st->log_cfg_file);
if (runtime->logger == NULL)
{
STELLAR_LOG_ERROR("unable to init log");
printf("unable to create logger");
goto error_out;
}
STELLAR_LOG_FATAL("stellar start (version: %s)\n %s", version, logo_str);
STELLAR_LOG_FATAL("stellar config file : %s", st->stellar_cfg_file);
STELLAR_LOG_FATAL("plugin config file : %s", st->plugin_cfg_file);
STELLAR_LOG_FATAL("log config file : %s", st->log_cfg_file);
__thread_local_logger = runtime->logger;
CORE_LOG_FATAL("stellar start (version: %s)\n %s", version, logo_str);
CORE_LOG_FATAL("stellar config file : %s", st->stellar_cfg_file);
CORE_LOG_FATAL("plugin config file : %s", st->plugin_cfg_file);
CORE_LOG_FATAL("log config file : %s", st->log_cfg_file);
if (stellar_config_load(config, st->stellar_cfg_file) != 0)
{
STELLAR_LOG_ERROR("unable to load config file");
CORE_LOG_ERROR("unable to load config file");
goto error_out;
}
stellar_config_print(config);
@@ -543,26 +547,26 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg
runtime->stat = stellar_stat_new(config->pkt_io_opts.nr_threads);
if (runtime->stat == NULL)
{
STELLAR_LOG_ERROR("unable to create stellar stat");
CORE_LOG_ERROR("unable to create stellar stat");
goto error_out;
}
runtime->plug_mgr = plugin_manager_init(st, st->plugin_cfg_file);
if (runtime->plug_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create plugin manager");
CORE_LOG_ERROR("unable to create plugin manager");
goto error_out;
}
runtime->packet_io = packet_io_new(&config->pkt_io_opts);
if (runtime->packet_io == NULL)
{
STELLAR_LOG_ERROR("unable to create packet io");
CORE_LOG_ERROR("unable to create packet io");
goto error_out;
}
if (stellar_thread_init(st) != 0)
{
STELLAR_LOG_ERROR("unable to init thread context");
CORE_LOG_ERROR("unable to init thread context");
goto error_out;
}
@@ -590,7 +594,7 @@ void stellar_run(struct stellar *st)
if (stellar_thread_run(st) != 0)
{
STELLAR_LOG_ERROR("unable to create worker thread");
CORE_LOG_ERROR("unable to create worker thread");
return;
}
@@ -607,7 +611,7 @@ void stellar_run(struct stellar *st)
// only available in dump file mode, automatically exits when all sessions have been released
if (packet_io_isbreak(runtime->packet_io) && all_session_have_freed(runtime, config))
{
STELLAR_LOG_FATAL("all sessions have been released, notify threads to exit");
CORE_LOG_FATAL("all sessions have been released, notify threads to exit");
stellar_stat_output(runtime->stat); // flush stat
ATOMIC_SET(&runtime->need_exit, 1);
}
@@ -625,8 +629,8 @@ void stellar_free(struct stellar *st)
packet_io_free(runtime->packet_io);
plugin_manager_exit(runtime->plug_mgr);
stellar_stat_free(runtime->stat);
STELLAR_LOG_FATAL("stellar exit\n");
log_free();
CORE_LOG_FATAL("stellar exit\n");
log_free(runtime->logger);
free(st);
st = NULL;
@@ -646,7 +650,7 @@ void stellar_reload_log_level(struct stellar *st)
{
if (st)
{
log_level_reload(st->log_cfg_file);
log_level_reload(st->runtime.logger);
}
}
@@ -726,4 +730,16 @@ void stellar_send_build_packet(struct stellar *st, struct packet *pkt)
int stellar_get_worker_thread_num(struct stellar *st)
{
return st->config.pkt_io_opts.nr_threads;
}
struct logger *stellar_get_logger(struct stellar *st)
{
if (st)
{
return st->runtime.logger;
}
else
{
return NULL;
}
}