فهرست منبع

refactor & feat: aFunExit抛出异常

SongZihuan 3 سال پیش
والد
کامیت
82a67b61d4
7فایلهای تغییر یافته به همراه50 افزوده شده و 18 حذف شده
  1. 3 1
      include/tool/tool-exception.h
  2. 5 1
      include/tool/tool-exception.inline.h
  3. 2 1
      include/tool/tool-exit.h
  4. 16 1
      src/tool/exit.cpp
  5. 2 0
      src/tool/log.cpp
  6. 7 3
      test/src/tool-exit.cpp
  7. 15 11
      test/src/tool-logger.cpp

+ 3 - 1
include/tool/tool-exception.h

@@ -31,8 +31,10 @@ namespace aFuntool {
     };
 
     class Exit : public aFuntoolException {
+        int exit_code;
     public:
-        inline explicit Exit();
+        inline explicit Exit(int exit_code_);
+        inline int getExitCode() const;
     };
 }
 

+ 5 - 1
include/tool/tool-exception.inline.h

@@ -28,9 +28,13 @@ namespace aFuntool {
 
     }
 
-    inline Exit::Exit() : aFuntoolException("Exit by user") {
+    inline Exit::Exit(int exit_code_) : aFuntoolException("Exit by user"), exit_code{exit_code_} {
 
     }
+
+    inline int Exit::getExitCode() const {
+        return exit_code;
+    }
 }
 
 #endif //AFUN_TOOL_EXCEPTION_INLINE_H

+ 2 - 1
include/tool/tool-exit.h

@@ -4,7 +4,8 @@
 namespace aFuntool {
     typedef void aFunExitFunc(void *);
 
-    [[noreturn]] AFUN_TOOL_EXPORT void aFunExit(int exit_code);
+    AFUN_TOOL_EXPORT void aFunExit(int exit_code) noexcept(false);
+    [[noreturn]] AFUN_TOOL_EXPORT void aFunExitReal(int exit_code);
     AFUN_TOOL_EXPORT int aFunTryExitPseudo();
     AFUN_TOOL_EXPORT int aFunExitPseudo();
     AFUN_TOOL_EXPORT int aFunAtExitTry(aFunExitFunc *func, void *data);

+ 16 - 1
src/tool/exit.cpp

@@ -1,5 +1,6 @@
 #include "tool.h"
 #include "tool-exit.h"
+#include "tool-exception.h"
 #include "mutex"
 
 namespace aFuntool {
@@ -14,7 +15,21 @@ namespace aFuntool {
      * 退出程序
      * @param exit_code 退出代码
      */
-    [[noreturn]] void aFunExit(int exit_code){
+    void aFunExit(int exit_code) noexcept(false) {
+        std::unique_lock<std::mutex> ul{exit_mutex};
+        for (int i = exit_func_size - 1; i >= 0; i--) {
+            if (exit_func[i].func != nullptr)
+                exit_func[i].func(exit_func[i].data);
+        }
+        ul.unlock();
+        throw Exit(exit_code);
+    }
+
+    /**
+     * 退出程序
+     * @param exit_code 退出代码
+     */
+    [[noreturn]] void aFunExitReal(int exit_code) {
         std::unique_lock<std::mutex> ul{exit_mutex};
         for (int i = exit_func_size - 1; i >= 0; i--) {
             if (exit_func[i].func != nullptr)

+ 2 - 0
src/tool/log.cpp

@@ -433,6 +433,7 @@ namespace aFuntool {
             throw LogFatalError("Log Fatal Error");
         aFunExit(EXIT_FAILURE);
 #endif
+        return 0;
     }
 
 #undef fatalErrorLog
@@ -450,5 +451,6 @@ namespace aFuntool {
         else
             aFunExit(exit_code);
 #endif
+        return 0;
     }
 }

+ 7 - 3
test/src/tool-exit.cpp

@@ -11,7 +11,11 @@ void exit_func_push2(void *) {
 }
 
 int main(int argc, char **argv) {
-    aFunAtExit(exit_func_push1, nullptr);
-    aFunAtExit(exit_func_push2, nullptr);
-    aFunExit(0);
+    try {
+        aFunAtExit(exit_func_push1, nullptr);
+        aFunAtExit(exit_func_push2, nullptr);
+        aFunExit(0);
+    } catch (aFuntool::Exit &e) {
+        aFunExitReal(e.getExitCode());
+    }
 }

+ 15 - 11
test/src/tool-logger.cpp

@@ -2,18 +2,22 @@
 using namespace aFuntool;
 
 int main(int argc, char **argv){
-    std::string base_path = getExedir(1);
-    if (base_path.empty()) {
-        printf("Not Exe Dir\n");
-        aFunExit(0);
-    }
+    try {
+        std::string base_path = getExedir(1);
+        if (base_path.empty()) {
+            printf("Not Exe Dir\n");
+            aFunExit(0);
+        }
 
-    setlocale(LC_ALL, "");
+        setlocale(LC_ALL, "");
 
-    static LogFactory factor {};
-    factor.initLogSystem(base_path + SEP + "aFunlog");
+        static LogFactory factor{};
+        factor.initLogSystem(base_path + SEP + "aFunlog");
 
-    static auto logger = Logger(factor, "Test", aFuntool::log_info);
-    infoLog(&logger, "Test logger");
-    aFunExit(0);
+        static auto logger = Logger(factor, "Test", aFuntool::log_info);
+        infoLog(&logger, "Test logger");
+        aFunExit(0);
+    } catch (aFuntool::Exit &e) {
+        aFunExitReal(e.getExitCode());
+    }
 }