合并日志相关pr: #1077

This commit is contained in:
ziyue 2021-08-30 20:43:03 +08:00
parent 7d456a0513
commit 57e91054af
11 changed files with 43 additions and 73 deletions

@ -1 +1 @@
Subproject commit 6214f5028763c5245d79d4c6c9d50bc780c8d6b7 Subproject commit 36d1122f42ab6b92d0ca8f57df0462acc632efc7

View File

@ -38,18 +38,25 @@
extern "C" { extern "C" {
#endif #endif
//输出日志到shell
#define LOG_CONSOLE (1 << 0)
//输出日志到文件
#define LOG_FILE (1 << 1)
//输出日志到回调函数(mk_events::on_mk_log)
#define LOG_CALLBACK (1 << 2)
typedef struct { typedef struct {
// 线程数 // 线程数
int thread_num; int thread_num;
// 日志级别,支持0~4 // 日志级别,支持0~4
int log_level; int log_level;
//控制日志输出的掩模请查看LOG_CONSOLE、LOG_FILE、LOG_CALLBACK等宏
int log_mask;
//文件日志保存路径,路径可以不存在(内部可以创建文件夹)设置为NULL关闭日志输出至文件 //文件日志保存路径,路径可以不存在(内部可以创建文件夹)设置为NULL关闭日志输出至文件
const char *log_file_path; const char *log_file_path;
//文件日志保存天数,设置为0关闭日志文件 //文件日志保存天数,设置为0关闭日志文件
int log_file_days; int log_file_days;
// 是否关闭 控制台 日志
int disable_console_log;
// 配置文件是内容还是路径 // 配置文件是内容还是路径
int ini_is_path; int ini_is_path;
@ -79,6 +86,7 @@ API_EXPORT void API_CALL mk_stop_all_server();
* mk_env_init便 * mk_env_init便
* @param thread_num 线 * @param thread_num 线
* @param log_level ,0~4 * @param log_level ,0~4
* @param log_mask LOG_CONSOLELOG_FILELOG_CALLBACK等宏
* @param log_file_path ,()NULL关闭日志输出至文件 * @param log_file_path ,()NULL关闭日志输出至文件
* @param log_file_days ,0 * @param log_file_days ,0
* @param ini_is_path * @param ini_is_path
@ -89,6 +97,7 @@ API_EXPORT void API_CALL mk_stop_all_server();
*/ */
API_EXPORT void API_CALL mk_env_init1(int thread_num, API_EXPORT void API_CALL mk_env_init1(int thread_num,
int log_level, int log_level,
int log_mask,
const char *log_file_path, const char *log_file_path,
int log_file_days, int log_file_days,
int ini_is_path, int ini_is_path,
@ -96,29 +105,6 @@ API_EXPORT void API_CALL mk_env_init1(int thread_num,
int ssl_is_path, int ssl_is_path,
const char *ssl, const char *ssl,
const char *ssl_pwd); const char *ssl_pwd);
/**
* mk_env_init便
* @param thread_num 线
* @param log_level ,0~4
* @param log_file_path ,()NULL关闭日志输出至文件
* @param log_file_days ,0
* @param ini_is_path
* @param ini NULL,
* @param ssl_is_path ssl证书是内容还是路径
* @param ssl ssl证书内容或路径NULL
* @param ssl_pwd NULL
* @param disable_console_log
*/
API_EXPORT void API_CALL mk_env_init2(int thread_num,
int log_level,
const char *log_file_path,
int log_file_days,
int ini_is_path,
const char *ini,
int ssl_is_path,
const char *ssl,
const char *ssl_pwd,
int disable_console_log);
/** /**
* *

View File

@ -159,10 +159,10 @@ typedef struct {
* @param level * @param level
* @param file * @param file
* @param line * @param line
* @param function * @param function
* @param message * @param message
*/ */
void (API_CALL *on_mk_log)(int level, const char* file, int line, const char *function, const char* message); void (API_CALL *on_mk_log)(int level, const char *file, int line, const char *function, const char *message);
} mk_events; } mk_events;

View File

@ -35,18 +35,19 @@ static std::shared_ptr<RtpServer> rtpServer;
#endif #endif
//////////////////////////environment init/////////////////////////// //////////////////////////environment init///////////////////////////
API_EXPORT void API_CALL mk_env_init(const mk_config *cfg) { API_EXPORT void API_CALL mk_env_init(const mk_config *cfg) {
assert(cfg); assert(cfg);
mk_env_init2(cfg->thread_num, mk_env_init1(cfg->thread_num,
cfg->log_level, cfg->log_level,
cfg->log_mask,
cfg->log_file_path, cfg->log_file_path,
cfg->log_file_days, cfg->log_file_days,
cfg->ini_is_path, cfg->ini_is_path,
cfg->ini, cfg->ini,
cfg->ssl_is_path, cfg->ssl_is_path,
cfg->ssl, cfg->ssl,
cfg->ssl_pwd, cfg->ssl_pwd);
cfg->disable_console_log);
} }
extern void stopAllTcpServer(); extern void stopAllTcpServer();
@ -63,6 +64,7 @@ API_EXPORT void API_CALL mk_stop_all_server(){
API_EXPORT void API_CALL mk_env_init1(int thread_num, API_EXPORT void API_CALL mk_env_init1(int thread_num,
int log_level, int log_level,
int log_mask,
const char *log_file_path, const char *log_file_path,
int log_file_days, int log_file_days,
int ini_is_path, int ini_is_path,
@ -70,42 +72,24 @@ API_EXPORT void API_CALL mk_env_init1(int thread_num,
int ssl_is_path, int ssl_is_path,
const char *ssl, const char *ssl,
const char *ssl_pwd) { const char *ssl_pwd) {
mk_env_init2(
thread_num,
log_level,
log_file_path,
log_file_days,
ini_is_path,
ini,
ssl_is_path,
ssl,
ssl_pwd,
0
);
}
API_EXPORT void API_CALL mk_env_init2(int thread_num,
int log_level,
const char *log_file_path,
int log_file_days,
int ini_is_path,
const char *ini,
int ssl_is_path,
const char *ssl,
const char *ssl_pwd,
int disable_console_log) {
//确保只初始化一次 //确保只初始化一次
static onceToken token([&]() { static onceToken token([&]() {
if(disable_console_log) { if (log_mask & LOG_CONSOLE) {
// 广播日志
Logger::Instance().add(std::make_shared<EventChannel>("EventChannel", (LogLevel) log_level));
} else {
//控制台日志 //控制台日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", (LogLevel) log_level)); Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel", (LogLevel) log_level));
} }
if(log_file_path && log_file_days){
if (log_mask & LOG_CALLBACK) {
//广播日志
Logger::Instance().add(std::make_shared<EventChannel>("EventChannel", (LogLevel) log_level));
}
if (log_mask & LOG_FILE) {
//日志文件 //日志文件
auto channel = std::make_shared<FileChannel>("FileChannel", File::absolutePath(log_file_path, ""), (LogLevel) log_level); auto channel = std::make_shared<FileChannel>("FileChannel",
log_file_path ? File::absolutePath(log_file_path, "") :
exeDir() + "log/", (LogLevel) log_level);
channel->setMaxDay(log_file_days ? log_file_days : 1);
Logger::Instance().add(channel); Logger::Instance().add(channel);
} }

View File

@ -20,13 +20,13 @@ static void* s_tag;
static mk_events s_events = {0}; static mk_events s_events = {0};
API_EXPORT void API_CALL mk_events_listen(const mk_events *events){ API_EXPORT void API_CALL mk_events_listen(const mk_events *events){
if(events){ if (events) {
memcpy(&s_events,events, sizeof(s_events)); memcpy(&s_events, events, sizeof(s_events));
}else{ } else {
memset(&s_events,0,sizeof(s_events)); memset(&s_events, 0, sizeof(s_events));
} }
static onceToken tokne([]{ static onceToken token([]{
NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastMediaChanged,[](BroadcastMediaChangedArgs){ NoticeCenter::Instance().addListener(&s_tag,Broadcast::kBroadcastMediaChanged,[](BroadcastMediaChangedArgs){
if(s_events.on_mk_media_changed){ if(s_events.on_mk_media_changed){
s_events.on_mk_media_changed(bRegist, s_events.on_mk_media_changed(bRegist,
@ -152,9 +152,10 @@ API_EXPORT void API_CALL mk_events_listen(const mk_events *events){
} }
}); });
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastLog,[](BroadcastLogArgs){ NoticeCenter::Instance().addListener(&s_tag, EventChannel::kBroadcastLogEvent,[](BroadcastLogEventArgs){
if (s_events.on_mk_log) { if (s_events.on_mk_log) {
s_events.on_mk_log((int)level, file, line, function, message); auto log = ctx->str();
s_events.on_mk_log((int) ctx->_level, ctx->_file.data(), ctx->_line, ctx->_function.data(), log.data());
} }
}); });
}); });

View File

@ -37,6 +37,7 @@ int main(int argc, char *argv[]) {
.ini = NULL, .ini = NULL,
.ini_is_path = 1, .ini_is_path = 1,
.log_level = 0, .log_level = 0,
.log_mask = LOG_CONSOLE,
.log_file_path = NULL, .log_file_path = NULL,
.log_file_days = 0, .log_file_days = 0,
.ssl = NULL, .ssl = NULL,

View File

@ -164,6 +164,7 @@ int main(int argc, char *argv[]){
.ini = NULL, .ini = NULL,
.ini_is_path = 0, .ini_is_path = 0,
.log_level = 0, .log_level = 0,
.log_mask = LOG_CONSOLE,
.ssl = NULL, .ssl = NULL,
.ssl_is_path = 1, .ssl_is_path = 1,
.ssl_pwd = NULL, .ssl_pwd = NULL,

View File

@ -402,6 +402,7 @@ int main(int argc, char *argv[]) {
.ini = ini_path, .ini = ini_path,
.ini_is_path = 1, .ini_is_path = 1,
.log_level = 0, .log_level = 0,
.log_mask = LOG_CONSOLE,
.log_file_path = NULL, .log_file_path = NULL,
.log_file_days = 0, .log_file_days = 0,
.ssl = ssl_path, .ssl = ssl_path,

View File

@ -190,6 +190,7 @@ int main(int argc, char *argv[]) {
.ini = ini_path, .ini = ini_path,
.ini_is_path = 1, .ini_is_path = 1,
.log_level = 0, .log_level = 0,
.log_mask = LOG_CONSOLE,
.ssl = ssl_path, .ssl = ssl_path,
.ssl_is_path = 1, .ssl_is_path = 1,
.ssl_pwd = NULL, .ssl_pwd = NULL,

View File

@ -53,7 +53,6 @@ const string kBroadcastShellLogin = "kBroadcastShellLogin";
const string kBroadcastNotFoundStream = "kBroadcastNotFoundStream"; const string kBroadcastNotFoundStream = "kBroadcastNotFoundStream";
const string kBroadcastStreamNoneReader = "kBroadcastStreamNoneReader"; const string kBroadcastStreamNoneReader = "kBroadcastStreamNoneReader";
const string kBroadcastHttpBeforeAccess = "kBroadcastHttpBeforeAccess"; const string kBroadcastHttpBeforeAccess = "kBroadcastHttpBeforeAccess";
const string kBroadcastLog = "kBroadcastEventLog";
} //namespace Broadcast } //namespace Broadcast
//通用配置项目 //通用配置项目

View File

@ -105,10 +105,6 @@ extern const string kBroadcastStreamNoneReader;
extern const string kBroadcastReloadConfig; extern const string kBroadcastReloadConfig;
#define BroadcastReloadConfigArgs void #define BroadcastReloadConfigArgs void
//日志输出广播目的是为了通过C API运行时由上级程序打印日志
extern const string kBroadcastLog;
#define BroadcastLogArgs const LogLevel level, const char* file, int line, const char* function, const char* message
#define ReloadConfigTag ((void *)(0xFF)) #define ReloadConfigTag ((void *)(0xFF))
#define RELOAD_KEY(arg,key) \ #define RELOAD_KEY(arg,key) \
do { \ do { \