Quellcode durchsuchen

refactor & feat: 调整Logger初始化策略

SongZihuan vor 3 Jahren
Ursprung
Commit
f3b9ee934f

+ 5 - 7
include/core/core-init.h

@@ -7,22 +7,20 @@ namespace aFuncore {
     struct InitInfo {
         const std::string &base_dir;
         aFuntool::LogFactory &factor;
-        bool log_asyn;
-        aFuntool::LogLevel level;
+        aFuntool::Logger &core_logger;
+        aFuntool::Logger &sys_logger;
 
         inline InitInfo(const std::string &base_dir_,
                         aFuntool::LogFactory &factor_,
-                        bool log_asyn_,
-                        aFuntool::LogLevel level_);
+                        aFuntool::Logger &core_logger_,
+                        aFuntool::Logger &sys_logger_);
 
     };
 
-    AFUN_CORE_EXPORT extern std::string log_path;
-    AFUN_CORE_EXPORT extern std::string lang_path;
-    AFUN_CORE_EXPORT extern std::string varlib_path;
     AFUN_CORE_EXPORT extern aFuntool::Logger *aFunCoreLogger;
 
     AFUN_CORE_EXPORT bool aFunCoreInit(InitInfo *info);
+    static void setCoreLogger(aFuntool::Logger *log);
 }
 
 #include "core-init.inline.h"

+ 7 - 3
include/core/core-init.inline.h

@@ -6,11 +6,15 @@
 namespace aFuncore {
     inline InitInfo::InitInfo(const std::string &base_dir_,
                               aFuntool::LogFactory &factor_,
-                              bool log_asyn_,
-                              aFuntool::LogLevel level_) :
-        base_dir{base_dir_}, factor{factor_}, log_asyn{log_asyn_}, level{level_} {
+                              aFuntool::Logger &core_logger_,
+                              aFuntool::Logger &sys_logger_) :
+        base_dir{base_dir_}, factor{factor_}, core_logger{core_logger_}, sys_logger{sys_logger_} {
 
     }
+
+    static void setCoreLogger(aFuntool::Logger *log) {
+        aFunCoreLogger = log;
+    }
 }
 
 #endif //AFUN_CORE_INIT_INLINE_H

+ 6 - 5
include/interface/it-init.h

@@ -5,17 +5,18 @@
 
 namespace aFunit {
     struct aFunInitInfo : public aFuncore::InitInfo {
-        aFuntool::LogLevel lang_level;
+        aFuntool::Logger &afun_logger;
 
         inline aFunInitInfo(const std::string &base_dir_,
-                           aFuntool::LogFactory &factor_,
-                           bool log_asyn_,
-                           aFuntool::LogLevel core_level_,
-                           aFuntool::LogLevel lang_level_);
+                            aFuntool::LogFactory &factor_,
+                            aFuntool::Logger &afun_logger_,
+                            aFuntool::Logger &core_logger_,
+                            aFuntool::Logger &sys_logger_);
     };
 
     AFUN_LANG_EXPORT extern aFuntool::Logger *aFunLogger;
     AFUN_LANG_EXPORT bool aFunInit(aFunInitInfo *info);
+    static void setAFunLogger(aFuntool::Logger *log);
 }
 
 #include "it-init.inline.h"

+ 8 - 4
include/interface/it-init.inline.h

@@ -6,12 +6,16 @@
 namespace aFunit {
     inline aFunInitInfo::aFunInitInfo(const std::string &base_dir_,
                                       aFuntool::LogFactory &factor_,
-                                      bool log_asyn_,
-                                      aFuntool::LogLevel core_level_,
-                                      aFuntool::LogLevel lang_level_) :
-        InitInfo(base_dir_, factor_, log_asyn_, core_level_), lang_level{lang_level_} {
+                                      aFuntool::Logger &afun_logger_,
+                                      aFuntool::Logger &core_logger_,
+                                      aFuntool::Logger &sys_logger_) :
+        InitInfo(base_dir_, factor_, sys_logger_, sys_logger_), afun_logger{afun_logger_} {
 
     }
+
+    static void setAFunLogger(aFuntool::Logger *log) {
+        aFunLogger = log;
+    }
 }
 
 

+ 2 - 3
include/tool/log.h

@@ -49,12 +49,11 @@ namespace aFuntool {
     class AFUN_TOOL_EXPORT LogFactory {
     public:
         Logger sys_log = Logger(*this, "SYSTEM");
-        LogFactory();
+        LogFactory(const aFuntool::FilePath &path, bool is_async) noexcept(false);
         ~LogFactory();
         LogFactory(const LogFactory &)=delete;
         LogFactory &operator=(const LogFactory &)=delete;
 
-        int initLogSystem(const aFuntool::FilePath &path, bool is_asyn = true);
         void writeLog(LogLevel level,
                       const char *id, pid_t tid,
                       const char *ti, time_t t,
@@ -85,7 +84,7 @@ namespace aFuntool {
         FILE *log_;  // 记录文件输出的位置
         FILE *csv_;
 
-        bool asyn_;  // 异步
+        bool async_;  // 异步
         std::thread thread_;
         std::condition_variable cond_;  // 有日志
         std::mutex mutex_;

+ 1 - 1
include/tool/mem.h

@@ -6,6 +6,6 @@
 #ifndef AFUN_MEM_H
 #define AFUN_MEM_H
 
-#include "mem.inline.h"
+#include "mem.template.h"
 
 #endif  // AFUN_MEM_H

+ 7 - 7
include/tool/mem.inline.h → include/tool/mem.template.h

@@ -1,5 +1,5 @@
-#ifndef AFUN_MEM_INLINE_H
-#define AFUN_MEM_INLINE_H
+#ifndef AFUN_MEM_TEMPLATE_H
+#define AFUN_MEM_TEMPLATE_H
 
 #include <cstdlib>
 #include "log.h"
@@ -15,8 +15,8 @@ namespace aFuntool {
     static T *safeCalloc(size_t n, size_t size){
         T *re = (T *)calloc(n, size);
         if (re == nullptr) {
-            if (SysLogger)
-                fatalErrorLog(SysLogger, EXIT_FAILURE, "The memory error");
+            if (aFunSysLogger)
+                fatalErrorLog(aFunSysLogger, EXIT_FAILURE, "The memory error");
             else
                 aFunExit(EXIT_FAILURE);
         }
@@ -27,8 +27,8 @@ namespace aFuntool {
     static T *safeCalloc(size_t n = 1){
         T *re = (T *)calloc(n, sizeof(T));  // 自动推断类型
         if (re == nullptr) {
-            if (SysLogger)
-                fatalErrorLog(SysLogger, EXIT_FAILURE, "The memory error");
+            if (aFunSysLogger)
+                fatalErrorLog(aFunSysLogger, EXIT_FAILURE, "The memory error");
             else
                 aFunExit(EXIT_FAILURE);
         }
@@ -36,4 +36,4 @@ namespace aFuntool {
     }
 }
 
-#endif //AFUN_MEM_INLINE_H
+#endif //AFUN_MEM_TEMPLATE_H

+ 4 - 4
include/tool/tool-logger.h

@@ -4,11 +4,11 @@
 
 namespace aFuntool {
     class Logger;
-    extern AFUN_TOOL_EXPORT class Logger *SysLogger;
+    extern AFUN_TOOL_EXPORT class Logger *aFunSysLogger;
 
-    static void setSysLogger(aFuntool::Logger *log) {
-        SysLogger = log;
-    }
+    static void setSysLogger(aFuntool::Logger *log);
 }
 
+#include "tool-logger.inline.h"
+
 #endif //AFUN_TOOL_LOGGER_H

+ 11 - 0
include/tool/tool-logger.inline.h

@@ -0,0 +1,11 @@
+#ifndef AFUN_TOOL_LOGGER_INLINE_H
+#define AFUN_TOOL_LOGGER_INLINE_H
+#include "tool-logger.h"
+
+namespace aFuntool {
+    static void setSysLogger(aFuntool::Logger *log) {
+        aFunSysLogger = log;
+    }
+}
+
+#endif //AFUN_TOOL_LOGGER_INLINE_H

+ 2 - 17
src/core/core-init.cpp

@@ -2,8 +2,6 @@
 #include "core-init.h"
 
 namespace aFuncore {
-    std::string log_path;
-    std::string varlib_path;
     aFuntool::Logger *aFunCoreLogger = nullptr;
 
     /**
@@ -21,21 +19,8 @@ namespace aFuncore {
         if (info->base_dir.empty())
             return false;
 
-        log_path = info->base_dir + aFuntool::SEP + aFunLogDir + aFuntool::SEP;
-        varlib_path = info->base_dir + aFuntool::SEP + aFunVarLibDir + aFuntool::SEP;
-
-        std::string log = log_path + "aFunlang";
-        bool re = info->factor.initLogSystem(log, info->log_asyn);
-        if (re == 0)
-            return false;
-
-        static aFuntool::Logger core_logger{info->factor, "aFunlang-core", info->level};
-        static aFuntool::Logger sys_logger{info->factor, "aFunlang-sys", info->level};
-        aFunCoreLogger = &core_logger;
-        aFuntool::setSysLogger(&sys_logger);
-
-        debugLog(aFunCoreLogger, "aFunCore log path: %s", log_path.c_str());
-        debugLog(aFunCoreLogger, "aFunCore var.lib path: %s", varlib_path.c_str());
+        setCoreLogger(&info->core_logger);
+        aFuntool::setSysLogger(&info->sys_logger);
 
         debugLog(aFunCoreLogger, "aFunCore init success");
         return true;

+ 1 - 2
src/interface/it-init.cpp

@@ -6,8 +6,7 @@ namespace aFunit {
     bool aFunInit(aFunInitInfo *info) {
         if (!aFuncore::aFunCoreInit(info))
             return false;
-        static aFuntool::Logger logger{info->factor, "aFunlang", info->lang_level};
-        aFunLogger = &logger;
+        setAFunLogger(&info->afun_logger);
         return true;
     }
 

+ 9 - 4
src/main.cpp

@@ -45,15 +45,20 @@ int main(int argc, char **argv_ansi) {
     }
 
     try {
-        aFun::LogFactory factor{};
-        factor.initLogSystem(home_path + aFun::SEP + "aFunlog");
+        auto factor = aFun::LogFactory(home_path + aFun::SEP + "aFunlog", true);
 
 #ifdef aFunDEBUG
-        aFun::aFunInitInfo info{home_path, factor, true, aFun::log_debug, aFun::log_debug};
+        auto core_logger = aFuntool::Logger(factor, "aFun-core", aFun::log_track);
+        auto sys_logger = aFuntool::Logger(factor, "aFun-sys", aFun::log_track);
+        auto aFun_logger = aFuntool::Logger(factor, "aFun", aFun::log_track);
 #else
-        aFun::aFunInitInfo info{base_path, factor, true, aFun::log_info, aFun::log_info};
+        auto core_logger = aFuntool::Logger(factor, "aFun-core", aFun::log_info);
+        auto sys_logger = aFuntool::Logger(factor, "aFun-sys", aFun::log_info);
+        auto aFun_logger = aFuntool::Logger(factor, "aFun", aFun::log_info);
 #endif
 
+        aFunit::aFunInitInfo info {home_path, factor, core_logger, sys_logger, aFun_logger};
+
         ff_FFlags *ff = ff_initFFlags(argc, argv, true, false, stderr, aFunlang_exe);
         if (ff == nullptr)
             aFun::aFunExit(EXIT_FAILURE);

+ 1 - 2
src/tool/file.cpp

@@ -241,8 +241,7 @@ namespace aFuntool {
             unsigned char c = *ch;
             unsigned char c_ = ~c;
 
-            if (SysLogger)
-                assertFatalErrorLog(code >= 0 && code <= 5, SysLogger, 2, "str = %s", str);
+            assertFatalErrorLog(code >= 0 && code <= 5, aFunSysLogger, 2, "str = %s", str);
             if (code == 0) {
                 if ((c_ & 0xFC) == 0 && (c & 0x02) == 0)  // 检查是否为1111110x, 先对其取反, 使用0xFC掩码检查前6位是否为0, 然后单独检查倒数第二位是否为0
                     code = 5;  // 剩余 5 个字节

+ 11 - 42
src/tool/log.cpp

@@ -27,7 +27,7 @@
 
 #ifdef aFunWIN32
 
-#include <windows.h>
+#include <Windows.h>
 
 #define getpid() static_cast<long>(GetCurrentProcessId())
 #define gettid() static_cast<long>(GetCurrentThreadId())
@@ -57,38 +57,10 @@ namespace aFuntool {
 
     void staticAnsyWritrLog(LogFactory::ansyData *data);
 
-    LogFactory::LogFactory() : sys_log{*this, "SYSTEM", log_info}{
-        init_ = false;
-        pid_ = 0;
-        log_ = nullptr;
-        csv_ = nullptr;
-
-        asyn_ = false;
-        log_buf_ = nullptr;
-        plog_buf_ = nullptr;
-    }
-
-/**
- * 函数名: initLogSystem
- * 目标: 初始化日志系统
- * 返回值:
- * 1 表示初始化成功
- * 2 表示已经初始化
- * 0 表示初始化失败
- *
- * 该程序线程不安全
- * @param path 日志保存的地址
- * @param is_asyn 是否启用异步
- * @return
- */
-    int LogFactory::initLogSystem(const aFuntool::FilePath &path, bool is_asyn) {
-        if (path.size() >= 218)  // 路径过长
-            return 0;
-
+    LogFactory::LogFactory(const aFuntool::FilePath &path, bool is_async) noexcept(false)
+        : sys_log{*this, "SYSTEM", log_info}{
         int re = 1;
         std::unique_lock<std::mutex> ul{mutex_};
-        if (init_)
-            return 2;
 
         char log_path[218] = {0};
         char csv_path[218] = {0};
@@ -104,14 +76,12 @@ namespace aFuntool {
         bool csv_head_write = (checkFile(csv_path) == 0);  // 文件不存在时才写入头部
 
         log_ = fileOpen(log_path, "a");
-        if (log_ == nullptr) {
-            perror("ERROR: ");
-            return 0;
-        }
+        if (log_ == nullptr)
+            throw FileOpenException(log_path);
 
         csv_ = fileOpen(csv_path, (char *) "a");
         if (csv_ == nullptr)
-            return 0;
+            throw FileOpenException(csv_path);
 
 #define CSV_FORMAT "%s,%s,%d,%d,%s,%lld,%s,%d,%s,%s\n"
 #define CSV_TITLE  "Level,Logger,PID,TID,Data,Timestamp,File,Line,Function,Log\n"
@@ -122,8 +92,8 @@ namespace aFuntool {
 #undef CSV_TITLE
 
         init_ = true;
-        asyn_ = is_asyn;
-        if (is_asyn) {
+        async_ = is_async;
+        if (is_async) {
             plog_buf_ = &log_buf_;
             auto *data = new ansyData{*this, mutex_};
             thread_ = std::thread(staticAnsyWritrLog, data);
@@ -133,7 +103,6 @@ namespace aFuntool {
         infoLog(&this->sys_log, "Log system init success");
         infoLog(&this->sys_log, "Log .log size %lld", log_size);
         infoLog(&this->sys_log, "Log .csv size %lld", csv_size);
-        return re;
     }
 
     LogFactory::~LogFactory(){
@@ -144,7 +113,7 @@ namespace aFuntool {
 
         ul.lock();
         init_ = false;
-        if (asyn_) {
+        if (async_) {
             ul.unlock();
             cond_.notify_all();
             thread_.join();
@@ -348,7 +317,7 @@ namespace aFuntool {
         vsnprintf(tmp, 1024, format, ap);  // ap只使用一次
         va_end(ap);
 
-        if (asyn_)
+        if (async_)
             writeLogAsyn(level, logger->id_.c_str(), tid, ti, t, file, line, func, tmp);
         else
             writeLog(level, logger->id_.c_str(), tid, ti, t, file, line, func, tmp);
@@ -357,7 +326,7 @@ namespace aFuntool {
             writeConsole(level, logger->id_.c_str(), tid, ti, t, file, line, func, tmp);
 
         ul.unlock();
-        if (asyn_)
+        if (async_)
             cond_.notify_all();
         safeFree(ti);
         return 0;

+ 1 - 1
src/tool/tool-logger.cpp

@@ -1,5 +1,5 @@
 #include "tool-logger.h"
 
 namespace aFuntool {
-    Logger *SysLogger = nullptr;
+    Logger *aFunSysLogger = nullptr;
 }

+ 1 - 0
test/src/CMakeLists.txt

@@ -35,5 +35,6 @@ add_new_test(core-reader COMMAND "$<TARGET_FILE:core-reader>")
 add_new_test(core-lexical COMMAND "$<TARGET_FILE:core-lexical>")
 
 add_new_test(it-syntactic COMMAND "$<TARGET_FILE:it-syntactic>")
+add_new_test(it-init COMMAND "$<TARGET_FILE:it-init>")
 
 add_new_test(run-code COMMAND "$<TARGET_FILE:run-code>")

+ 4 - 2
test/src/core-init.cpp

@@ -9,8 +9,10 @@ int main() {
         aFunExit(EXIT_FAILURE);
     }
 
-    aFuntool::LogFactory factor {};
-    aFuncore::InitInfo info {base_path, factor, true, log_debug};
+    auto factor = aFuntool::LogFactory(base_path + aFuntool::SEP + "aFunlog", true);
+    auto core_logger = aFuntool::Logger(factor, "aFun-core");
+    auto sys_logger = aFuntool::Logger(factor, "aFun-sys");
+    aFuncore::InitInfo info {base_path, factor, core_logger, sys_logger};
 
     if (!aFunCoreInit(&info)) {
         printf_stderr(0, "aFunlang init error.");

+ 23 - 0
test/src/it-init.cpp

@@ -0,0 +1,23 @@
+#include "it-init.h"
+using namespace aFuncore;
+using namespace aFuntool;
+
+int main() {
+    std::string base_path = getExedir(1);
+    if (base_path.empty()) {
+        printf_stderr(0, "aFunlang init error.");
+        aFunExit(EXIT_FAILURE);
+    }
+
+    auto factor = aFuntool::LogFactory(base_path + aFuntool::SEP + "aFunlog", true);
+    auto core_logger = aFuntool::Logger(factor, "aFun-core");
+    auto sys_logger = aFuntool::Logger(factor, "aFun-sys");
+    auto aFun_logger = aFuntool::Logger(factor, "aFun");
+    aFunit::aFunInitInfo info {base_path, factor, core_logger, sys_logger, aFun_logger};
+
+    if (!aFunInit(&info)) {
+        printf_stderr(0, "aFunlang init error.");
+        aFunExit(EXIT_FAILURE);
+    }
+    return 0;
+}

+ 5 - 3
test/src/run-code.cpp

@@ -305,12 +305,14 @@ int main() {
             aFunExitReal(EXIT_FAILURE);
         }
 
-        aFuntool::LogFactory factor{};
-        aFuncore::InitInfo info{base_path, factor, true, log_debug};
+        auto factor = aFuntool::LogFactory(base_path + aFuntool::SEP + "aFunlog", true);
+        auto core_logger = aFuntool::Logger(factor, "aFun-core");
+        auto sys_logger = aFuntool::Logger(factor, "aFun-sys");
+        aFuncore::InitInfo info {base_path, factor, core_logger, sys_logger};
 
         if (!aFunCoreInit(&info)) {
             printf_stderr(0, "aFunlang init error.");
-            aFunExitReal(EXIT_FAILURE);
+            aFunExit(EXIT_FAILURE);
         }
 
         exit_code = Main();

+ 2 - 4
test/src/tool-logger.cpp

@@ -12,10 +12,8 @@ int main(int argc, char **argv){
 
         setlocale(LC_ALL, "");
 
-        static LogFactory factor{};
-        factor.initLogSystem(base_path + SEP + "aFunlog");
-
-        static auto logger = Logger(factor, "Test", aFuntool::log_info);
+        auto factor = LogFactory(base_path + SEP + "aFunlog", true);
+        auto logger = Logger(factor, "Test", aFuntool::log_info);
         infoLog(&logger, "Test logger");
         aFunExit(0);
     } catch (aFuntool::Exit &e) {