Skip to content

Commit

Permalink
Added possibility for a custom internal log handler injection
Browse files Browse the repository at this point in the history
  • Loading branch information
imorda committed Aug 29, 2024
1 parent 69bf966 commit 25a38b2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions include/fluent-bit/flb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct flb_config {

/* Logging */
char *log_file;
void (*lib_consumer) (struct log_message*); /* custom consumer configured in the lib mode */
struct flb_log *log;

/* Parser Conf */
Expand Down
24 changes: 16 additions & 8 deletions include/fluent-bit/flb_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,30 @@ extern FLB_TLS_DEFINE(struct flb_log, flb_log_ctx)
#define FLB_LOG_STDERR 0 /* send logs to STDERR */
#define FLB_LOG_FILE 1 /* write logs to a file */
#define FLB_LOG_SOCKET 2 /* write logs to a unix socket */
#define FLB_LOG_LIB 3 /* write logs to a custom consumer */

#define FLB_LOG_EVENT MK_EVENT_NOTIFICATION
#define FLB_LOG_MNG 1024

/* Simple structure to dispatch messages to the log collector */
struct log_message {
size_t size;
char msg[4096 - sizeof(size_t)];
};

#define FLB_LOG_CACHE_ENTRIES 10
#define FLB_LOG_CACHE_TEXT_BUF_SIZE 1024

/* Logging main context */
struct flb_log {
struct mk_event event; /* worker event for manager */
flb_pipefd_t ch_mng[2]; /* worker channel manager */
uint16_t type; /* log type */
uint16_t level; /* level */
char *out; /* FLB_LOG_FILE or FLB_LOG_SOCKET */
pthread_t tid; /* thread ID */
struct flb_worker *worker; /* non-real worker reference */
struct mk_event event; /* worker event for manager */
flb_pipefd_t ch_mng[2]; /* worker channel manager */
uint16_t type; /* log type */
uint16_t level; /* level */
char *out; /* FLB_LOG_FILE, FLB_LOG_SOCKET or FLB_LOG_LIB */
void (*custom_consumer) (struct log_message*); /* redefined custom log output handler */
pthread_t tid; /* thread ID */
struct flb_worker *worker; /* non-real worker reference */
struct mk_event_loop *evl;

/* Initialization variables */
Expand Down Expand Up @@ -114,7 +122,7 @@ static inline int flb_log_check(int l) {
}

struct flb_log *flb_log_create(struct flb_config *config, int type,
int level, char *out);
int level, char *out, void (*custom_consumer) (struct log_message*));
int flb_log_set_level(struct flb_config *config, int level);
int flb_log_get_level_str(char *str);

Expand Down
5 changes: 4 additions & 1 deletion src/flb_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,14 @@ static int flb_engine_log_start(struct flb_config *config)
if (config->log_file) {
type = FLB_LOG_FILE;
}
else if(config->lib_consumer){
type = FLB_LOG_LIB;
}
else {
type = FLB_LOG_STDERR;
}

if (flb_log_create(config, type, level, config->log_file) == NULL) {
if (flb_log_create(config, type, level, config->log_file, config->lib_consumer) == NULL) {
return -1;
}

Expand Down
11 changes: 4 additions & 7 deletions src/flb_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ extern struct flb_aws_error_reporter *error_reporter;

FLB_TLS_DEFINE(struct flb_log, flb_log_ctx)

/* Simple structure to dispatch messages to the log collector */
struct log_message {
size_t size;
char msg[4096 - sizeof(size_t)];
};

static inline int consume_byte(flb_pipefd_t fd)
{
int ret;
Expand Down Expand Up @@ -79,6 +73,8 @@ static inline int log_push(struct log_message *msg, struct flb_log *log)
}
ret = write(fd, msg->msg, msg->size);
close(fd);
} else if (log->type == FLB_LOG_LIB) {
log->custom_consumer(msg);
}

return ret;
Expand Down Expand Up @@ -388,7 +384,7 @@ int flb_log_set_file(struct flb_config *config, char *out)
}

struct flb_log *flb_log_create(struct flb_config *config, int type,
int level, char *out)
int level, char *out, void (*custom_consumer) (struct log_message*))
{
int ret;
struct flb_log *log;
Expand All @@ -415,6 +411,7 @@ struct flb_log *flb_log_create(struct flb_config *config, int type,
log->type = type;
log->level = level;
log->out = out;
log->custom_consumer = custom_consumer;
log->evl = evl;
log->tid = 0;

Expand Down

0 comments on commit 25a38b2

Please sign in to comment.