feat(module manager API): add stellar_module_manager_get_logger

This commit is contained in:
yangwei
2024-09-29 14:23:26 +08:00
parent 2ea8d96c5c
commit d9d9b4728d
6 changed files with 19 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ extern "C"
#endif
#include "stellar/mq.h"
#include "stellar/log.h"
struct stellar_module;
struct stellar_module *stellar_module_new(const char *name, void *ctx);
@@ -22,7 +23,7 @@ struct stellar_module_manager;
typedef struct stellar_module *module_on_init_func(struct stellar_module_manager *mod_mgr);
typedef void module_on_exit_func(struct stellar_module_manager *mod_mgr, struct stellar_module *mod);
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema);
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema, struct logger *logger);
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr);
void stellar_module_manager_register_thread(struct stellar_module_manager* mod_mgr, int thread_id, struct mq_runtime *mq_rt);
@@ -36,6 +37,7 @@ struct stellar_module *stellar_module_manager_get_module(struct stellar_module_m
int stellar_module_manager_get_max_thread_num(struct stellar_module_manager* mod_mgr);
const char *stellar_module_manager_get_toml_path(struct stellar_module_manager *mod_mgr);
struct mq_schema *stellar_module_manager_get_mq_schema(struct stellar_module_manager *mod_mgr);
struct logger *stellar_module_manager_get_logger(struct stellar_module_manager *mod_mgr);

View File

@@ -13,16 +13,14 @@
*******************************************/
#include "toml/toml.h"
#include <fcntl.h>
#include <unistd.h>
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema)
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema, struct logger *logger)
{
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
mod_mgr->schema.max_thread_num=max_thread_num;
mod_mgr->schema.mq_schema=mq_schema;
mod_mgr->schema.logger=logger;
if(module_spec_toml_path==NULL)return mod_mgr;
FILE *fp = fopen(module_spec_toml_path, "r");
if (fp == NULL)return mod_mgr;
@@ -133,6 +131,12 @@ struct mq_schema *stellar_module_manager_get_mq_schema(struct stellar_module_man
return mod_mgr->schema.mq_schema;
}
struct logger *stellar_module_manager_get_logger(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return NULL;
return mod_mgr->schema.logger;
}
const char *stellar_module_manager_get_toml_path(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return NULL;

View File

@@ -12,7 +12,6 @@ extern "C"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
struct stellar_module
{
@@ -41,6 +40,7 @@ struct stellar_module_manager
{
int max_thread_num;
struct mq_schema *mq_schema;
struct logger *logger;
}schema;
}__attribute__((aligned(sizeof(void*))));

View File

@@ -31,7 +31,7 @@ TEST(module_manager_internal, stellar_module_manager_new_with_toml) {
write(fd, gtest_mock_spec_toml, strlen(gtest_mock_spec_toml));
close(fd);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema, NULL);
EXPECT_TRUE(mod_mgr!=NULL);
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==NULL);
@@ -72,7 +72,7 @@ TEST(stellar_module, basic_new_and_free) {
TEST(stellar_module_manager, new_with_null_toml) {
struct mq_schema *mq_schema=NULL;
struct stellar_module_manager *mod_mgr = stellar_module_manager_new(NULL, 10, mq_schema);
struct stellar_module_manager *mod_mgr = stellar_module_manager_new(NULL, 10, mq_schema, NULL);
EXPECT_TRUE(mod_mgr!=NULL);
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==NULL);
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
@@ -87,7 +87,7 @@ TEST(stellar_module_manager, new_with_null_toml) {
TEST(stellar_module_manager, new_with_empty_toml) {
struct mq_schema *mq_schema=NULL;
struct stellar_module_manager *mod_mgr = stellar_module_manager_new("/dev/null", 10, mq_schema);
struct stellar_module_manager *mod_mgr = stellar_module_manager_new("/dev/null", 10, mq_schema, NULL);
EXPECT_TRUE(mod_mgr!=NULL);
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==NULL);
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
@@ -102,7 +102,7 @@ TEST(stellar_module_manager, new_with_empty_toml) {
TEST(stellar_module_manager, register_thread) {
struct mq_schema *mq_schema=(struct mq_schema*)1;
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(NULL, 10, mq_schema);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(NULL, 10, mq_schema, NULL);
EXPECT_TRUE(mod_mgr!=NULL);
@@ -163,7 +163,7 @@ TEST(module_manager, basic_module) {
write(fd, gtest_module_spec_toml, strlen(gtest_module_spec_toml));
close(fd);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema, NULL);
EXPECT_TRUE(mod_mgr!=NULL);
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "gtest")!=NULL);

View File

@@ -44,7 +44,7 @@ TEST(polling_manager, basic_polling_module) {
write(fd, gtest_mock_spec_toml, strlen(gtest_mock_spec_toml));
close(fd);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema, NULL);
EXPECT_TRUE(mod_mgr!=NULL);
struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr);

View File

@@ -214,7 +214,7 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *module_cfg
goto error_out;
}
st->mod_mgr = stellar_module_manager_new(module_cfg_file, st->thread_num, st->mq_schema);
st->mod_mgr = stellar_module_manager_new(module_cfg_file, st->thread_num, st->mq_schema, st->logger);
if (st->mod_mgr == NULL)
{
CORE_LOG_ERROR("unable to create module manager");