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

62
include/stellar/log.h Normal file
View File

@@ -0,0 +1,62 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
enum log_level
{
LOG_TRACE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
};
#define STELLAR_LOG_TRACE(logger, module, format, ...) \
if ((logger) && log_level_check((logger), LOG_TRACE)) \
{ \
log_print((logger), LOG_TRACE, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_DEBUG(logger, module, format, ...) \
if ((logger) && log_level_check((logger), LOG_DEBUG)) \
{ \
log_print((logger), LOG_DEBUG, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_INFO(logger, module, format, ...) \
if ((logger) && log_level_check((logger), LOG_INFO)) \
{ \
log_print((logger), LOG_INFO, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_WARN(logger, module, format, ...) \
if ((logger) && log_level_check((logger), LOG_WARN)) \
{ \
log_print((logger), LOG_WARN, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_ERROR(logger, module, format, ...) \
if ((logger) && log_level_check((logger), LOG_ERROR)) \
{ \
log_print((logger), LOG_ERROR, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_FATAL(logger, module, format, ...) \
if ((logger) && log_level_check((logger), LOG_FATAL)) \
{ \
log_print((logger), LOG_FATAL, (module), (format), ##__VA_ARGS__); \
}
struct logger;
struct logger *stellar_get_logger(struct stellar *st);
int log_level_check(struct logger *logger, enum log_level level);
void log_print(struct logger *logger, enum log_level level, const char *module, const char *fmt, ...);
#ifdef __cplusplus
}
#endif

View File

@@ -2,12 +2,12 @@
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "toml.h"
#include "log_private.h"
#include "stellar_config.h"
#define CONFIG_LOG_ERROR(format, ...) LOG_ERROR("config", format, ##__VA_ARGS__)
#define CONFIG_LOG_DEBUG(format, ...) LOG_DEBUG("config", format, ##__VA_ARGS__)
#define CONFIG_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "config", format, ##__VA_ARGS__)
#define CONFIG_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "config", format, ##__VA_ARGS__)
// return 0: success
// retuun -1: failed

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);
}
}
@@ -727,3 +731,15 @@ 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;
}
}

View File

@@ -5,12 +5,12 @@
#include <string.h>
#include <assert.h>
#include "log.h"
#include "log_private.h"
#include "stellar_stat.h"
#include "fieldstat/fieldstat_easy.h"
#include "fieldstat/fieldstat_exporter.h"
#define STAT_LOG_ERROR(format, ...) LOG_ERROR("stat", format, ##__VA_ARGS__)
#define STAT_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "stat", format, ##__VA_ARGS__)
#define IS_FREE 0
#define IS_BUSY 0xf

View File

@@ -61,6 +61,7 @@ global:
stellar_run;
stellar_free;
stellar_loopbreak;
stellar_get_logger;
http_message_*;
http_decoder_init;

View File

@@ -3,16 +3,16 @@
#include <sys/queue.h>
#include <assert.h>
#include "log.h"
#include "checksum.h"
#include "crc32_hash.h"
#include "packet_private.h"
#include "log_private.h"
#include "ip_reassembly.h"
#include "packet_parser.h"
#include "packet_helper.h"
#include "ip_reassembly.h"
#include "packet_private.h"
#define IP_REASSEMBLE_DEBUG(format, ...) LOG_DEBUG("ip_reassembly", format, ##__VA_ARGS__)
#define IP_REASSEMBLE_ERROR(format, ...) LOG_ERROR("ip_reassembly", format, ##__VA_ARGS__)
#define IP_REASSEMBLE_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "ip_reassembly", format, ##__VA_ARGS__)
#define IP_REASSEMBLE_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "ip_reassembly", format, ##__VA_ARGS__)
#define IPV4_KEYLEN 1
#define IPV6_KEYLEN 4

View File

@@ -1,5 +1,7 @@
add_library(log log.cpp)
target_include_directories(log PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(log PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(log toml)
add_subdirectory(test)

View File

@@ -1,5 +1,7 @@
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
@@ -7,8 +9,8 @@
#include <stdlib.h>
#include <pthread.h>
#include "log.h"
#include "toml.h"
#include "log_private.h"
enum log_output
{
@@ -24,39 +26,38 @@ struct log_config
char log_file[PATH_MAX];
};
struct log_context
struct logger
{
char config_file[PATH_MAX];
struct log_config config;
int log_fd;
int log_file_reopen_day;
int log_file_opened_day;
};
struct log_context g_log_context = {
.config = {},
.log_fd = -1,
.log_file_reopen_day = 0,
};
struct log_context *g_log_ctx = &g_log_context;
static unsigned char level_str[6][6] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static unsigned char level_str[7][6] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "STATE"};
static inline void local_time(struct tm *local)
thread_local struct logger *__thread_local_logger;
/******************************************************************************
* Private API
******************************************************************************/
static void local_time(struct tm *local)
{
time_t t;
time(&t);
localtime_r(&t, local);
}
static inline enum log_level check_level(const char *level)
static int str_to_level(const char *level)
{
if (level == NULL)
{
return LOG_NONE;
return -1;
}
if (strcasecmp(level, "TRACE") == 0)
{
return LOG_TRACE;
@@ -83,13 +84,11 @@ static inline enum log_level check_level(const char *level)
}
else
{
return LOG_NONE;
return -1;
}
}
// return 0: success
// return -1: failed
static int parse_config(struct log_config *config, const char *cfg_file)
static int config_parse(struct log_config *config, const char *config_file)
{
int ret = -1;
FILE *fp = NULL;
@@ -101,32 +100,31 @@ static int parse_config(struct log_config *config, const char *cfg_file)
toml_table_t *section = NULL;
toml_table_t *table = NULL;
fp = fopen(cfg_file, "r");
fp = fopen(config_file, "r");
if (fp == NULL)
{
fprintf(stderr, "open config file %s failed, %s\n", cfg_file, strerror(errno));
fprintf(stderr, "(logger) open config file %s failed, %s\n", config_file, strerror(errno));
goto error_out;
}
table = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (table == NULL)
{
fprintf(stderr, "parse config file %s failed, %s\n", cfg_file, errbuf);
fprintf(stderr, "(logger) parse config file %s failed, %s\n", config_file, errbuf);
goto error_out;
}
section = toml_table_in(table, "log");
if (section == NULL)
{
fprintf(stderr, "config file %s missing log section\n", cfg_file);
fprintf(stderr, "(logger) config file %s missing log section\n", config_file);
goto error_out;
}
// output
ptr = toml_raw_in(section, "output");
if (ptr == NULL || toml_rtos(ptr, &ptr_output) != 0)
{
fprintf(stderr, "config file %s missing log.output\n", cfg_file);
fprintf(stderr, "(logger) config file %s missing log.output\n", config_file);
goto error_out;
}
if (strcasecmp(ptr_output, "stderr") == 0)
@@ -143,7 +141,7 @@ static int parse_config(struct log_config *config, const char *cfg_file)
}
else
{
fprintf(stderr, "config file %s invalid log.output\n", cfg_file);
fprintf(stderr, "(logger) config file %s invalid log.output\n", config_file);
goto error_out;
}
@@ -152,30 +150,28 @@ static int parse_config(struct log_config *config, const char *cfg_file)
ptr = toml_raw_in(section, "file");
if (ptr == NULL || toml_rtos(ptr, &ptr_file) != 0)
{
fprintf(stderr, "config file %s missing log.file\n", cfg_file);
fprintf(stderr, "(logger) config file %s missing log.file\n", config_file);
goto error_out;
}
strcpy(config->log_file, ptr_file);
}
// level
ptr = toml_raw_in(section, "level");
if (ptr == NULL || toml_rtos(ptr, &ptr_level) != 0)
{
fprintf(stderr, "config file %s missing log.level\n", cfg_file);
fprintf(stderr, "(logger) config file %s missing log.level\n", config_file);
goto error_out;
}
config->level = check_level(ptr_level);
if (config->level == LOG_NONE)
config->level = (enum log_level)str_to_level(ptr_level);
if (config->level == -1)
{
fprintf(stderr, "config file %s invalid log.level\n", cfg_file);
fprintf(stderr, "config file %s invalid log.level\n", config_file);
goto error_out;
}
ret = 0;
error_out:
if (ptr_output)
{
free(ptr_output);
@@ -200,27 +196,25 @@ error_out:
return ret;
}
// return 0: success
// return -1: failed
static int log_reopen()
static int log_file_reopen(struct logger *logger)
{
int new_fd;
int old_fd;
struct tm local;
char buff[PATH_MAX * 2] = {0};
local_time(&local);
snprintf(buff, sizeof(buff), "%s.%d-%02d-%02d", g_log_ctx->config.log_file, local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
snprintf(buff, sizeof(buff), "%s.%d-%02d-%02d", logger->config.log_file, local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
new_fd = open(buff, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (new_fd == -1)
{
fprintf(stderr, "open() \"%s\" failed, %s\n", buff, strerror(errno));
fprintf(stderr, "(logger) open() \"%s\" failed, %s\n", buff, strerror(errno));
return -1;
}
g_log_ctx->log_file_reopen_day = local.tm_mday;
old_fd = g_log_ctx->log_fd;
g_log_ctx->log_fd = new_fd;
logger->log_file_opened_day = local.tm_mday;
old_fd = logger->log_fd;
logger->log_fd = new_fd;
if (old_fd > 0)
{
@@ -234,56 +228,74 @@ static int log_reopen()
* Public API
******************************************************************************/
// return 0: success
// return -1: failed
int log_init(const char *config_file)
struct logger *log_new(const char *config_file)
{
memset(g_log_ctx, 0, sizeof(struct log_context));
if (parse_config(&g_log_ctx->config, config_file) != 0)
struct logger *logger = (struct logger *)calloc(1, sizeof(struct logger));
if (logger == NULL)
{
return -1;
fprintf(stderr, "(logger) logger calloc() failed, %s\n", strerror(errno));
return NULL;
}
if (g_log_context.config.output == LOG_OUTPUT_FILE || g_log_context.config.output == LOG_OUTPUT_BOTH)
memcpy(&logger->config_file, config_file, strlen(config_file));
if (config_parse(&logger->config, config_file) != 0)
{
if (log_reopen() != 0)
goto error_out;
}
if (logger->config.output == LOG_OUTPUT_FILE || logger->config.output == LOG_OUTPUT_BOTH)
{
return -1;
if (log_file_reopen(logger) != 0)
{
goto error_out;
}
}
return 0;
return logger;
error_out:
log_free(logger);
return NULL;
}
void log_free()
void log_free(struct logger *logger)
{
if (g_log_ctx->config.output == LOG_OUTPUT_FILE || g_log_ctx->config.output == LOG_OUTPUT_BOTH)
if (logger)
{
if (g_log_ctx->log_fd > 0)
if (logger->config.output == LOG_OUTPUT_FILE || logger->config.output == LOG_OUTPUT_BOTH)
{
close(g_log_ctx->log_fd);
g_log_ctx->log_fd = -1;
if (logger->log_fd > 0)
{
close(logger->log_fd);
logger->log_fd = -1;
}
}
free(logger);
logger = NULL;
}
}
int log_level_check(enum log_level level)
int log_level_check(struct logger *logger, enum log_level level)
{
return level >= g_log_ctx->config.level;
return level >= logger->config.level;
}
void log_level_reload(const char *config_file)
void log_level_reload(struct logger *logger)
{
struct log_config config;
if (parse_config(&config, config_file) != 0)
struct log_config config = {};
if (config_parse(&config, logger->config_file) == 0)
{
return;
logger->config.level = config.level;
fprintf(stderr, "(logger) logger level reload to %s\n", level_str[config.level]);
}
else
{
fprintf(stderr, "(logger) logger level reload failed\n");
}
g_log_context.config.level = config.level;
}
void log_print(enum log_level level, const char *module, const char *fmt, ...)
void log_print(struct logger *logger, enum log_level level, const char *module, const char *fmt, ...)
{
int nwrite;
char buf[4096] = {0};
@@ -309,20 +321,20 @@ void log_print(enum log_level level, const char *module, const char *fmt, ...)
// add end of line
p += snprintf(p, end - p, "\n");
if (g_log_ctx->config.output == LOG_OUTPUT_STDERR || g_log_ctx->config.output == LOG_OUTPUT_BOTH)
if (logger->config.output == LOG_OUTPUT_STDERR || logger->config.output == LOG_OUTPUT_BOTH)
{
fprintf(stderr, "%s", buf);
}
if (g_log_ctx->config.output == LOG_OUTPUT_FILE || g_log_ctx->config.output == LOG_OUTPUT_BOTH)
if (logger->config.output == LOG_OUTPUT_FILE || logger->config.output == LOG_OUTPUT_BOTH)
{
if (g_log_ctx->log_file_reopen_day != local.tm_mday)
if (logger->log_file_opened_day != local.tm_mday)
{
log_reopen();
log_file_reopen(logger);
}
do
{
nwrite = write(g_log_ctx->log_fd, buf, p - buf);
nwrite = write(logger->log_fd, buf, p - buf);
} while (nwrite == -1 && errno == EINTR);
}
}

View File

@@ -1,66 +0,0 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
enum log_level
{
LOG_NONE = -1,
LOG_TRACE = 0,
LOG_DEBUG = 1,
LOG_INFO = 2,
LOG_WARN = 3,
LOG_ERROR = 4,
LOG_FATAL = 5,
};
#define LOG_TRACE(module, format, ...) \
if (log_level_check(LOG_TRACE)) \
{ \
log_print(LOG_TRACE, module, format, ##__VA_ARGS__); \
}
#define LOG_DEBUG(module, format, ...) \
if (log_level_check(LOG_DEBUG)) \
{ \
log_print(LOG_DEBUG, module, format, ##__VA_ARGS__); \
}
#define LOG_INFO(module, format, ...) \
if (log_level_check(LOG_INFO)) \
{ \
log_print(LOG_INFO, module, format, ##__VA_ARGS__); \
}
#define LOG_WARN(module, format, ...) \
if (log_level_check(LOG_WARN)) \
{ \
log_print(LOG_WARN, module, format, ##__VA_ARGS__); \
}
#define LOG_ERROR(module, format, ...) \
if (log_level_check(LOG_ERROR)) \
{ \
log_print(LOG_ERROR, module, format, ##__VA_ARGS__); \
}
#define LOG_FATAL(module, format, ...) \
if (log_level_check(LOG_FATAL)) \
{ \
log_print(LOG_FATAL, module, format, ##__VA_ARGS__); \
}
// return 0: success
// return -1: failed
int log_init(const char *config_file);
void log_free();
int log_level_check(enum log_level level);
void log_level_reload(const char *config_file);
void log_print(enum log_level level, const char *module, const char *fmt, ...);
#ifdef __cplusplus
}
#endif

18
src/log/log_private.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/log.h"
extern thread_local struct logger *__thread_local_logger;
struct logger *log_new(const char *config_file);
void log_free(struct logger *logger);
void log_level_reload(struct logger *logger);
#ifdef __cplusplus
}
#endif

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "log.h"
#include "log_private.h"
#if 1
TEST(LOG, STDERR)
@@ -8,26 +8,27 @@ TEST(LOG, STDERR)
char buffer[1024] = {0};
const char *config = "./conf/log_stderr.toml";
snprintf(buffer, sizeof(buffer), "sed -i 's/DEBUG/ERROR/g' %s", config);
EXPECT_TRUE(log_init(config) == 0);
struct logger *logger = log_new(config);
EXPECT_TRUE(logger);
LOG_TRACE("test", "test log 1");
LOG_DEBUG("test", "test log 1");
LOG_INFO("test", "test log 1");
LOG_WARN("test", "test log 1");
LOG_ERROR("test", "test log 1");
LOG_FATAL("test", "test log 1");
STELLAR_LOG_TRACE(logger, "test", "test log 1");
STELLAR_LOG_DEBUG(logger, "test", "test log 1");
STELLAR_LOG_INFO(logger, "test", "test log 1");
STELLAR_LOG_WARN(logger, "test", "test log 1");
STELLAR_LOG_ERROR(logger, "test", "test log 1");
STELLAR_LOG_FATAL(logger, "test", "test log 1");
system(buffer);
log_level_reload(config);
log_level_reload(logger);
LOG_TRACE("test", "test log 2");
LOG_DEBUG("test", "test log 2");
LOG_INFO("test", "test log 2");
LOG_WARN("test", "test log 2");
LOG_ERROR("test", "test log 2");
LOG_FATAL("test", "test log 2");
STELLAR_LOG_TRACE(logger, "test", "test log 2");
STELLAR_LOG_DEBUG(logger, "test", "test log 2");
STELLAR_LOG_INFO(logger, "test", "test log 2");
STELLAR_LOG_WARN(logger, "test", "test log 2");
STELLAR_LOG_ERROR(logger, "test", "test log 2");
STELLAR_LOG_FATAL(logger, "test", "test log 2");
log_free();
log_free(logger);
}
#endif
@@ -37,26 +38,27 @@ TEST(LOG, FILE)
char buffer[1024] = {0};
const char *config = "./conf/log_file.toml";
snprintf(buffer, sizeof(buffer), "sed -i 's/DEBUG/ERROR/g' %s", config);
EXPECT_TRUE(log_init(config) == 0);
struct logger *logger = log_new(config);
EXPECT_TRUE(logger);
LOG_TRACE("test", "test log 1");
LOG_DEBUG("test", "test log 1");
LOG_INFO("test", "test log 1");
LOG_WARN("test", "test log 1");
LOG_ERROR("test", "test log 1");
LOG_FATAL("test", "test log 1");
STELLAR_LOG_TRACE(logger, "test", "test log 1");
STELLAR_LOG_DEBUG(logger, "test", "test log 1");
STELLAR_LOG_INFO(logger, "test", "test log 1");
STELLAR_LOG_WARN(logger, "test", "test log 1");
STELLAR_LOG_ERROR(logger, "test", "test log 1");
STELLAR_LOG_FATAL(logger, "test", "test log 1");
system(buffer);
log_level_reload(config);
log_level_reload(logger);
LOG_TRACE("test", "test log 2");
LOG_DEBUG("test", "test log 2");
LOG_INFO("test", "test log 2");
LOG_WARN("test", "test log 2");
LOG_ERROR("test", "test log 2");
LOG_FATAL("test", "test log 2");
STELLAR_LOG_TRACE(logger, "test", "test log 2");
STELLAR_LOG_DEBUG(logger, "test", "test log 2");
STELLAR_LOG_INFO(logger, "test", "test log 2");
STELLAR_LOG_WARN(logger, "test", "test log 2");
STELLAR_LOG_ERROR(logger, "test", "test log 2");
STELLAR_LOG_FATAL(logger, "test", "test log 2");
log_free();
log_free(logger);
}
#endif
@@ -75,12 +77,12 @@ TEST(LOG, REOPEN)
for (int i = 0; i < 3; i++)
{
LOG_TRACE("test", "test log %d", i);
LOG_DEBUG("test", "test log %d", i);
LOG_INFO("test", "test log %d", i);
LOG_WARN("test", "test log %d", i);
LOG_ERROR("test", "test log %d", i);
LOG_FATAL("test", "test log %d", i);
STELLAR_LOG_TRACE(logger, "test", "test log %d", i);
STELLAR_LOG_DEBUG(logger, "test", "test log %d", i);
STELLAR_LOG_INFO(logger, "test", "test log %d", i);
STELLAR_LOG_WARN(logger, "test", "test log %d", i);
STELLAR_LOG_ERROR(logger, "test", "test log %d", i);
STELLAR_LOG_FATAL(logger, "test", "test log %d", i);
if (i == 1)
{
system(buffer2); // set date to 2099/01/01
@@ -91,7 +93,7 @@ TEST(LOG, REOPEN)
}
}
log_free();
log_free(logger);
}
#endif

View File

@@ -1,13 +1,13 @@
#include <time.h>
#include "log.h"
#include "checksum.h"
#include "packet_private.h"
#include "log_private.h"
#include "packet_helper.h"
#include "packet_parser.h"
#include "packet_private.h"
#define PACKET_CRAFT_LOG_DEBUG(format, ...) LOG_DEBUG("packet craft", format, ##__VA_ARGS__)
#define PACKET_CRAFT_LOG_ERROR(format, ...) LOG_ERROR("packet craft", format, ##__VA_ARGS__)
#define PACKET_CRAFT_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "packet craft", format, ##__VA_ARGS__)
#define PACKET_CRAFT_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet craft", format, ##__VA_ARGS__)
struct fingerprint
{

View File

@@ -4,14 +4,14 @@
#include <string.h>
#include <sys/time.h>
#include "log.h"
#include "utils.h"
#include "packet_helper.h"
#include "packet_private.h"
#include "log_private.h"
#include "packet_dump.h"
#include "packet_parser.h"
#include "packet_helper.h"
#include "packet_private.h"
#define PACKET_DUMP_LOG_ERROR(format, ...) LOG_ERROR("packet dump", format, ##__VA_ARGS__)
#define PACKET_DUMP_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet dump", format, ##__VA_ARGS__)
struct pcap_pkt_hdr
{

View File

@@ -4,14 +4,14 @@
#include <netinet/ip_icmp.h>
#include <linux/ppp_defs.h>
#include "log.h"
#include "log_private.h"
#include "packet_helper.h"
#include "packet_private.h"
#include "packet_parser.h"
#define PACKET_PARSE_LOG_DEBUG(format, ...) void(0) // LOG_DEBUG("packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_WARN(format, ...) LOG_WARN("packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_ERROR(format, ...) LOG_ERROR("packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_DEBUG(format, ...) void(0) // STELLAR_LOG_DEBUG(__thread_local_logger, "packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet parse", format, ##__VA_ARGS__)
#define PACKET_PARSE_LOG_WARN(format, ...) STELLAR_LOG_WARN(__thread_local_logger, "packet parse", format, ##__VA_ARGS__)
#define PACKET_LOG_DATA_INSUFFICIENCY(pkt, layer) \
{ \

View File

@@ -5,6 +5,8 @@ extern "C"
{
#endif
#include "stellar/packet.h"
const char *packet_parse(struct packet *pkt, const char *data, uint16_t len);
const char *layer_proto_to_str(enum layer_proto proto);

View File

@@ -1,10 +1,10 @@
#include "log.h"
#include "tuple.h"
#include "uthash.h"
#include "log_private.h"
#include "packet_helper.h"
#include "packet_private.h"
#define PACKET_LOG_ERROR(format, ...) LOG_ERROR("packet", format, ##__VA_ARGS__)
#define PACKET_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "packet", format, ##__VA_ARGS__)
/******************************************************************************
* metadata utils

View File

@@ -9,16 +9,16 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "log.h"
#include "tuple.h"
#include "utils.h"
#include "log_private.h"
#include "dumpfile_io.h"
#include "packet_private.h"
#include "packet_parser.h"
#include "packet_dump.h"
#include "packet_parser.h"
#include "packet_private.h"
#define PACKET_IO_LOG_FATAL(format, ...) LOG_FATAL("dumpfile", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("dumpfile", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_FATAL(format, ...) STELLAR_LOG_FATAL(__thread_local_logger, "dumpfile", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "dumpfile", format, ##__VA_ARGS__)
#define MAX_PACKET_QUEUE_SIZE (4096 * 1000)

View File

@@ -4,13 +4,13 @@
#include <string.h>
#include <netinet/ether.h>
#include "log.h"
#include "marsio.h"
#include "marsio_io.h"
#include "packet_private.h"
#include "log_private.h"
#include "packet_parser.h"
#include "packet_private.h"
#define PACKET_IO_LOG_ERROR(format, ...) LOG_ERROR("marsio", format, ##__VA_ARGS__)
#define PACKET_IO_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "marsio", format, ##__VA_ARGS__)
struct marsio_io
{

View File

@@ -2,8 +2,8 @@
#include <stdlib.h>
#include <assert.h>
#include "log.h"
#include "utils.h"
#include "log_private.h"
#include "packet_helper.h"
#include "packet_filter.h"
#include "session_private.h"
@@ -14,8 +14,8 @@
#include "session_manager.h"
#include "session_transition.h"
#define SESSION_LOG_ERROR(format, ...) LOG_ERROR("session", format, ##__VA_ARGS__)
#define SESSION_LOG_DEBUG(format, ...) LOG_DEBUG("session", format, ##__VA_ARGS__)
#define SESSION_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "session", format, ##__VA_ARGS__)
#define SESSION_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "session", format, ##__VA_ARGS__)
struct session_manager
{

View File

@@ -1,11 +1,11 @@
#include <stdio.h>
#include <assert.h>
#include "log.h"
#include "log_private.h"
#include "session_private.h"
#include "session_transition.h"
#define SESSION_TRANSITION_LOG_INFO(format, ...) LOG_INFO("session transition", format, ##__VA_ARGS__)
#define SESSION_TRANSITION_LOG_INFO(format, ...) STELLAR_LOG_INFO(__thread_local_logger, "session transition", format, ##__VA_ARGS__)
#define MAX_TRANSITION_PER_STATE 8

View File

@@ -1,10 +1,10 @@
#include <stdint.h>
#include <stdlib.h>
#include "log.h"
#include "snowflake.h"
#include "log_private.h"
#define SNOWFLAKE_LOG_ERROR(format, ...) LOG_ERROR("snowflake", format, ##__VA_ARGS__)
#define SNOWFLAKE_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "snowflake", format, ##__VA_ARGS__)
struct snowflake
{

View File

@@ -1,13 +1,13 @@
#include <string.h>
#include "log.h"
#include "list.h"
#include "log_private.h"
#include "interval_tree.h"
#include "tcp_reassembly.h"
#include "stellar/stellar.h"
#define TCP_REASSEMBLY_LOG_DEBUG(format, ...) LOG_DEBUG("tcp_reassembly", format, ##__VA_ARGS__)
#define TCP_REASSEMBLY_LOG_ERROR(format, ...) LOG_ERROR("tcp_reassembly", format, ##__VA_ARGS__)
#define TCP_REASSEMBLY_LOG_DEBUG(format, ...) STELLAR_LOG_DEBUG(__thread_local_logger, "tcp_reassembly", format, ##__VA_ARGS__)
#define TCP_REASSEMBLY_LOG_ERROR(format, ...) STELLAR_LOG_ERROR(__thread_local_logger, "tcp_reassembly", format, ##__VA_ARGS__)
struct tcp_segment_private
{

View File

@@ -7,6 +7,7 @@
#include "utils.h"
#include "packet_dump.h"
#include "session_private.h"
#include "stellar/log.h"
#include "stellar/stellar_mq.h"
#include "stellar/stellar_exdata.h"
@@ -51,6 +52,7 @@ static void log_print(int fd, const char *module, const char *fmt, ...)
struct plugin_ctx
{
struct stellar *st;
struct logger *logger;
int sess_exdata_idx;
int sess_plug_id;
int tcp_topic_id;
@@ -271,6 +273,7 @@ extern "C"
pthread_spin_init(&ctx->lock, PTHREAD_PROCESS_PRIVATE);
ctx->st = st;
ctx->logger = stellar_get_logger(st);
ctx->sess_exdata_idx = stellar_exdata_new_index(st, "DEBUG_PLUGIN_SESS_EXDATA", stellar_exdata_free_default, NULL);
ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx);
ctx->udp_topic_id = stellar_mq_get_topic_id(st, TOPIC_UDP);
@@ -281,6 +284,7 @@ extern "C"
stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_tcp_msg, ctx->sess_plug_id);
stellar_session_mq_subscribe(st, ctx->tcp_stream_topic_id, on_sess_tcp_stream_msg, ctx->sess_plug_id);
STELLAR_LOG_FATAL(ctx->logger, "debug plugin", "init");
log_print(ctx->fd, "debug plugin", "init");
return ctx;
@@ -291,6 +295,7 @@ extern "C"
struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx;
if (ctx)
{
STELLAR_LOG_FATAL(ctx->logger, "debug plugin", "exit");
log_print(ctx->fd, "debug plugin", "exit");
if (ctx->fd > 0)
{