Pārlūkot izejas kodu

refactor & feat: aFunInit移动到interface

SongZihuan 3 gadi atpakaļ
vecāks
revīzija
97712af856

+ 1 - 1
include/code/code-logger.h

@@ -5,7 +5,7 @@
 
 namespace aFuncode {
     AFUN_CODE_EXPORT extern aFuntool::Logger *aFunCodeLogger;
-    AFUN_INLINE void setCodeLogger(aFuntool::Logger *log);
+    AFUN_STATIC void setCodeLogger(aFuntool::Logger *log);
 }
 
 #include "code-logger.inline.h"

+ 0 - 1
include/core/aFuncore.h

@@ -1,7 +1,6 @@
 #ifndef AFUN_AFUNCORE_H
 #define AFUN_AFUNCORE_H
 
-#include "core-init.h"
 #include "core-logger.h"
 #include "msg.h"
 #include "env-var.h"

+ 0 - 26
include/core/core-init.h

@@ -1,26 +0,0 @@
-#ifndef AFUN_CORE_INIT_H
-#define AFUN_CORE_INIT_H
-#include "aFuntool.h"
-#include "aFunCoreExport.h"
-
-namespace aFuncore {
-    struct InitInfo {
-        aFuntool::LogFactory &factor;
-        aFuntool::Logger &core_logger;
-        aFuntool::Logger &sys_logger;
-
-        AFUN_INLINE InitInfo(aFuntool::LogFactory &factor_,
-                             aFuntool::Logger &core_logger_,
-                             aFuntool::Logger &sys_logger_);
-
-    };
-
-    AFUN_CORE_EXPORT extern aFuntool::Logger *aFunCoreLogger;
-
-    AFUN_CORE_EXPORT bool aFunCoreInit(InitInfo *info);
-    AFUN_STATIC void setCoreLogger(aFuntool::Logger *log);
-}
-
-#include "core-init.inline.h"
-
-#endif //AFUN_CORE_INIT_H

+ 0 - 19
include/core/core-init.inline.h

@@ -1,19 +0,0 @@
-#ifndef AFUN_CORE_INIT_INLINE_H
-#define AFUN_CORE_INIT_INLINE_H
-
-#include "core-init.h"
-
-namespace aFuncore {
-    InitInfo::InitInfo(aFuntool::LogFactory &factor_,
-                       aFuntool::Logger &core_logger_,
-                       aFuntool::Logger &sys_logger_) :
-        factor{factor_}, core_logger{core_logger_}, sys_logger{sys_logger_} {
-
-    }
-
-    void setCoreLogger(aFuntool::Logger *log) {
-        aFunCoreLogger = log;
-    }
-}
-
-#endif //AFUN_CORE_INIT_INLINE_H

+ 4 - 1
include/core/core-logger.h

@@ -1,10 +1,13 @@
-#ifndef AFUN_CORE_LOGGER_H
+#ifndef AFUN_CORE_LOGGER_H
 #define AFUN_CORE_LOGGER_H
 #include "aFuntool.h"
 #include "aFunCoreExport.h"
 
 namespace aFuncore {
     AFUN_CORE_EXPORT extern aFuntool::Logger *aFunCoreLogger;
+    AFUN_STATIC void setCoreLogger(aFuntool::Logger *log);
 }
 
+#include "core-logger.inline.h"
+
 #endif //AFUN_CORE_LOGGER_H

+ 12 - 0
include/core/core-logger.inline.h

@@ -0,0 +1,12 @@
+#ifndef AFUN_CORE_LOGGER_INLINE_H
+#define AFUN_CORE_LOGGER_INLINE_H
+
+#include "core-logger.h"
+
+namespace aFuncore {
+    void setCoreLogger(aFuntool::Logger *log) {
+        aFunCoreLogger = log;
+    }
+}
+
+#endif //AFUN_CORE_LOGGER_INLINE_H

+ 1 - 1
include/interface/aFunit.h

@@ -1,7 +1,7 @@
 #ifndef AFUN_AFUNIT_H
 #define AFUN_AFUNIT_H
 
-#include "it-init.h"
+#include "init.h"
 #include "it-reader.h"
 #include "it-exception.h"
 

+ 30 - 0
include/interface/init.h

@@ -0,0 +1,30 @@
+#ifndef AFUN_INIT_H
+#define AFUN_INIT_H
+#include "aFunlangExport.h"
+#include "aFuncore.h"
+#include "aFuncode.h"
+#include "aFuntool.h"
+
+namespace aFunit {
+    struct InitInfo {
+        aFuntool::LogFactory &factor;
+        aFuntool::Logger &core_logger;
+        aFuntool::Logger &code_logger;
+        aFuntool::Logger &sys_logger;
+        aFuntool::Logger &afun_logger;
+
+        AFUN_INLINE InitInfo(aFuntool::LogFactory &factor_,
+                             aFuntool::Logger &afun_logger_,
+                             aFuntool::Logger &core_logger_,
+                             aFuntool::Logger &code_logger_,
+                             aFuntool::Logger &sys_logger_);
+    };
+
+    AFUN_LANG_EXPORT extern aFuntool::Logger *aFunLogger;
+    AFUN_LANG_EXPORT bool aFunInit(InitInfo *info);
+    AFUN_STATIC void setAFunLogger(aFuntool::Logger *log);
+}
+
+#include "init.inline.h"
+
+#endif //AFUN_INIT_H

+ 22 - 0
include/interface/init.inline.h

@@ -0,0 +1,22 @@
+#ifndef AFUN_INIT_INLINE_H
+#define AFUN_INIT_INLINE_H
+
+#include "init.h"
+
+namespace aFunit {
+    InitInfo::InitInfo(aFuntool::LogFactory &factor_,
+                               aFuntool::Logger &afun_logger_,
+                               aFuntool::Logger &core_logger_,
+                               aFuntool::Logger &code_logger_,
+                               aFuntool::Logger &sys_logger_)
+        : factor{factor_}, core_logger{core_logger_}, code_logger{code_logger_}, sys_logger{sys_logger_}, afun_logger{afun_logger_} {
+
+    }
+
+    void setAFunLogger(aFuntool::Logger *log) {
+        aFunLogger = log;
+    }
+}
+
+
+#endif //AFUN_INIT_INLINE_H

+ 0 - 23
include/interface/it-init.h

@@ -1,23 +0,0 @@
-#ifndef AFUN_IT_INIT_H
-#define AFUN_IT_INIT_H
-#include "aFunlangExport.h"
-#include "aFuncore.h"
-
-namespace aFunit {
-    struct aFunInitInfo : public aFuncore::InitInfo {
-        aFuntool::Logger &afun_logger;
-
-        AFUN_INLINE aFunInitInfo(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);
-    AFUN_STATIC void setAFunLogger(aFuntool::Logger *log);
-}
-
-#include "it-init.inline.h"
-
-#endif //AFUN_IT_INIT_H

+ 0 - 21
include/interface/it-init.inline.h

@@ -1,21 +0,0 @@
-#ifndef AFUN_IT_INIT_INLINE_H
-#define AFUN_IT_INIT_INLINE_H
-
-#include "it-init.h"
-
-namespace aFunit {
-    aFunInitInfo::aFunInitInfo(aFuntool::LogFactory &factor_,
-                               aFuntool::Logger &afun_logger_,
-                               aFuntool::Logger &core_logger_,
-                               aFuntool::Logger &sys_logger_) :
-        InitInfo(factor_, core_logger_, sys_logger_), afun_logger{afun_logger_} {
-
-    }
-
-    void setAFunLogger(aFuntool::Logger *log) {
-        aFunLogger = log;
-    }
-}
-
-
-#endif //AFUN_IT_INIT_INLINE_H

+ 1 - 1
include/interface/it-reader.h

@@ -3,7 +3,7 @@
 #include <functional>
 #include "aFunlangExport.h"
 #include "aFuncore.h"
-#include "it-init.h"
+#include "init.h"
 
 namespace aFunit {
     class AFUN_LANG_EXPORT StringReader : public aFuncore::Reader {

+ 0 - 27
src/core/core-init.cpp

@@ -1,27 +0,0 @@
-#include <clocale>
-#include "core-init.h"
-#include "core-logger.h"
-
-namespace aFuncore {
-    aFuntool::Logger *aFunCoreLogger = nullptr;
-
-    /**
-     * 初始化程序
-     * @param info 初始化信息
-     * @return 是否初始化成功
-     */
-    bool aFunCoreInit(InitInfo *info){
-        if (info == nullptr)
-            return false;
-
-        aFuntool::getEndian();
-        if (setlocale(LC_ALL, "") == nullptr)
-            return false;
-
-        setCoreLogger(&info->core_logger);
-        aFuntool::setSysLogger(&info->sys_logger);
-
-        debugLog(aFunCoreLogger, "aFunCore init success");
-        return true;
-    }
-}

+ 6 - 0
src/core/core-logger.cpp

@@ -0,0 +1,6 @@
+#include <clocale>
+#include "core-logger.h"
+
+namespace aFuncore {
+    aFuntool::Logger *aFunCoreLogger = nullptr;
+}

+ 23 - 0
src/interface/init.cpp

@@ -0,0 +1,23 @@
+#include "init.h"
+
+namespace aFunit {
+    aFuntool::Logger *aFunLogger = nullptr;
+
+    bool aFunInit(InitInfo *info) {
+        if (info == nullptr)
+            return false;
+
+        aFuntool::getEndian();
+        if (setlocale(LC_ALL, "") == nullptr)
+            return false;
+
+        setAFunLogger(&info->afun_logger);
+        aFuncore::setCoreLogger(&info->core_logger);
+        aFuncode::setCodeLogger(&info->code_logger);
+        aFuntool::setSysLogger(&info->sys_logger);
+
+        infoLog(aFunLogger, "aFun init success.");
+        return true;
+    }
+
+}

+ 0 - 13
src/interface/it-init.cpp

@@ -1,13 +0,0 @@
-#include "it-init.h"
-
-namespace aFunit {
-    aFuntool::Logger *aFunLogger = nullptr;
-
-    bool aFunInit(aFunInitInfo *info) {
-        if (!aFuncore::aFunCoreInit(info))
-            return false;
-        setAFunLogger(&info->afun_logger);
-        return true;
-    }
-
-}

+ 1 - 1
src/interface/it-reader.cpp

@@ -1,5 +1,5 @@
 #include "it-reader.h"
-#include "it-init.h"
+#include "init.h"
 
 namespace aFunit {
     size_t StringReader::readText(char *dest, size_t read_len, ReadMode &mode) {

+ 4 - 3
test/src/CMakeLists.txt

@@ -45,14 +45,15 @@ add_new_test(tool-c-hash COMMAND "$<TARGET_FILE:tool-c-hash>")
 add_new_test(tool-c-stdio COMMAND "$<TARGET_FILE:tool-c-stdio>")
 set_test_label(tool-c tool-c-hash tool-c-stdio)
 
-add_new_test(core-init COMMAND "$<TARGET_FILE:core-init>")
-add_new_test(core-code COMMAND "$<TARGET_FILE:core-code>")
+add_new_test(code-create COMMAND "$<TARGET_FILE:code-create>")
+set_test_label(code code-create)
+
 add_new_test(core-env-var COMMAND "$<TARGET_FILE:core-env-var>")
 add_new_test(core-down-msg COMMAND "$<TARGET_FILE:core-down-msg>")
 add_new_test(core-up-msg COMMAND "$<TARGET_FILE:core-up-msg>")
 add_new_test(core-reader COMMAND "$<TARGET_FILE:core-reader>")
 add_new_test(core-lexical COMMAND "$<TARGET_FILE:core-lexical>")
-set_test_label(core core-init core-code core-env-var core-down-msg core-up-msg core-reader core-lexical)
+set_test_label(core core-env-var core-down-msg core-up-msg core-reader core-lexical)
 
 add_new_test(it-syntactic COMMAND "$<TARGET_FILE:it-syntactic>")
 add_new_test(it-init COMMAND "$<TARGET_FILE:it-init>")

+ 0 - 0
test/src/core-code.cpp → test/src/code-create.cpp


+ 0 - 16
test/src/core-init.cpp

@@ -1,16 +0,0 @@
-#include "core-init.h"
-using namespace aFuncore;
-using namespace aFuntool;
-
-int main() {
-    auto factor = aFuntool::LogFactory(std::string(".") + aFuntool::SEP + "aFunlog", true);
-    auto core_logger = aFuntool::Logger(factor, "aFun-core");
-    auto sys_logger = aFuntool::Logger(factor, "aFun-sys");
-    auto info = aFuncore::InitInfo(factor, core_logger, sys_logger);
-
-    if (!aFunCoreInit(&info)) {
-        printf_stderr(0, "aFunlang init error.");
-        aFunExitReal(EXIT_FAILURE);
-    }
-    return 0;
-}

+ 13 - 0
test/src/core-logger.cpp

@@ -0,0 +1,13 @@
+#include "core-logger.h"
+using namespace aFuncore;
+using namespace aFuntool;
+
+int main() {
+    auto factor = aFuntool::LogFactory(std::string(".") + aFuntool::SEP + "aFunlog", true);
+    auto logger = aFuntool::Logger(factor, "aFun");
+    debugLog(&logger, "I am debug\n");
+    infoLog(&logger, "I am info\n");
+    warningLog(&logger, "I am warning\n");
+    errorLog(&logger, "I am error\n");
+    aFunExitReal(EXIT_FAILURE);
+}

+ 2 - 2
test/src/it-init.cpp

@@ -1,4 +1,4 @@
-#include "it-init.h"
+#include "init.h"
 using namespace aFuncore;
 using namespace aFuntool;
 
@@ -7,7 +7,7 @@ int main() {
     auto core_logger = aFuntool::Logger(factor, "aFun-core");
     auto sys_logger = aFuntool::Logger(factor, "aFun-sys");
     auto aFun_logger = aFuntool::Logger(factor, "aFun");
-    auto info = aFunit::aFunInitInfo(factor, aFun_logger, core_logger, sys_logger);
+    auto info = aFunit::InitInfo(factor, aFun_logger, core_logger, core_logger, sys_logger);
 
     if (!aFunInit(&info)) {
         printf_stderr(0, "aFunlang init error.");

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

@@ -359,11 +359,12 @@ int main() {
     int exit_code = 0;
     try {
         auto factor = aFuntool::LogFactory(std::string(".") + aFuntool::SEP + "aFunlog", true);
+        auto it_logger = aFuntool::Logger(factor, "aFun");
         auto core_logger = aFuntool::Logger(factor, "aFun-core");
         auto sys_logger = aFuntool::Logger(factor, "aFun-sys");
-        auto info = aFuncore::InitInfo(factor, core_logger, sys_logger);
+        auto info = aFunit::InitInfo(factor, core_logger, core_logger, sys_logger, it_logger);
 
-        if (!aFunCoreInit(&info)) {
+        if (!aFunInit(&info)) {
             printf_stderr(0, "aFunlang init error.");
             aFunExit(EXIT_FAILURE);
         }