Browse Source

refactor & feat: 整理输出IO函数

SongZihuan 3 năm trước cách đây
mục cha
commit
9cf951c35d

+ 24 - 0
include/tool/tool-stdio.h

@@ -1,6 +1,7 @@
 #ifndef AFUN_STDIO_H
 #define AFUN_STDIO_H
 #include <cstdio>
+#include <cinttypes>
 #include "tool.h"
 #include "aFunToolExport.h"
 
@@ -65,6 +66,29 @@ namespace aFuntool {
 
 #endif
 
+namespace aFuntool {
+    class OutStream {
+        typedef size_t PrintFunction(size_t, const char *, ...);
+        PrintFunction *func;
+    public:
+        inline explicit OutStream(PrintFunction *func_);
+        inline OutStream &operator<<(int8_t a);
+        inline OutStream &operator<<(int16_t a);
+        inline OutStream &operator<<(int32_t a);
+        inline OutStream &operator<<(int64_t a);
+        inline OutStream &operator<<(uint8_t a);
+        inline OutStream &operator<<(uint16_t a);
+        inline OutStream &operator<<(uint32_t a);
+        inline OutStream &operator<<(uint64_t a);
+        inline OutStream &operator<<(const char *a);
+        inline OutStream &operator<<(const std::string &a);
+        inline OutStream &operator<<(const void *a);
+    };
+
+    AFUN_TOOL_EXPORT extern OutStream cout;
+    AFUN_TOOL_EXPORT extern OutStream cerr;
+}
+
 #include "tool-stdio.inline.h"
 
 #endif //AFUN_STDIO_H

+ 62 - 0
include/tool/tool-stdio.inline.h

@@ -95,4 +95,66 @@ namespace aFuntool {
 }
 
 #endif
+
+namespace aFuntool {
+    inline OutStream::OutStream(PrintFunction *func_) : func {func_} {
+
+    }
+
+    inline OutStream &OutStream::operator<<(int8_t a) {
+        func(0, "%c", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(int16_t a) {
+        func(0, "%d", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(int32_t a) {
+        func(0, "%d", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(int64_t a) {
+        func(0, "%ld", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(uint8_t a) {
+        func(0, "%c", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(uint16_t a) {
+        func(0, "%u", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(uint32_t a) {
+        func(0, "%u", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(uint64_t a) {
+        func(0, "%lu", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(const char *a){
+        func(0, "%s", a);
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(const std::string &a) {
+        func(0, "%s", a.c_str());
+        return *this;
+    }
+
+    inline OutStream &OutStream::operator<<(const void *a) {
+        func(0, "%p", a);
+        return *this;
+    }
+}
+
 #endif //AFUN_STDIO_INLINE_H

+ 4 - 4
src/core/code.cpp

@@ -368,13 +368,13 @@ RETURN_FALSE:
      * 显式代码块内容
      */
     void Code::ByteCode::display() const{
-        aFuntool::printf_stdout(0, "%c[father: %p] type=%d %p", prefix == aFuntool::NUL ? '-' : prefix, father, type, this);
+        aFuntool::cout << (prefix == aFuntool::NUL ? '-' : prefix) << "[father: " << father << "] type=" << type << " " << this;
         if (type == code_element)
-            aFuntool::printf_stdout(0, " element: %s\n", data.element);
+            aFuntool::cout << " element: " << data.element << "\n";
         else if (type == code_block)
-            aFuntool::printf_stdout(0, " block: '%c' son: %p\n", data.block_type, data.son);
+            aFuntool::cout << " block: '" << data.block_type << "' son: " << data.son << "\n";
         else
-            aFuntool::printf_stdout(0, "\n");
+            aFuntool::cout << "\n";
     }
 
 #define Done(write) do{if(!(write)){return false;}}while(0)

+ 2 - 2
src/core/msg.cpp

@@ -9,10 +9,10 @@ namespace aFuncore {
     }
 
     void NormalMessage::topProgress(){
-        aFuntool::printf_stdout(0, "NORMAL: %p\n", obj);
+        aFuntool::printf_stdout(0, "NORMAL: %p\n", obj);  // TODO-szh 使用 Event
     }
 
-    ErrorMessage::ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation)
+    ErrorMessage::ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation)  // TODO-szh 使用Event
             : TopMessage("ERROR"), error_type{error_type_}, error_info{error_info_}, inter{activation->inter}{
         for (NULL; activation != nullptr; activation = activation->toPrev()) {
             if (activation->getFileLine() != 0)

+ 9 - 11
src/tool/log.cpp

@@ -81,7 +81,7 @@ namespace aFuntool {
  * @param is_asyn 是否启用异步
  * @return
  */
-    int LogFactory::initLogSystem(const aFuntool::FilePath &path, bool is_asyn){
+    int LogFactory::initLogSystem(const aFuntool::FilePath &path, bool is_asyn) {
         if (path.size() >= 218)  // 路径过长
             return 0;
 
@@ -113,7 +113,7 @@ namespace aFuntool {
         if (csv_ == nullptr)
             return 0;
 
-#define CSV_FORMAT "%s,%s,%d,%d,%s,%ld,%s,%d,%s,%s\n"
+#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"
         if (csv_head_write) {
             fprintf(csv_, CSV_TITLE);  // 设置 cvs 标题
@@ -199,14 +199,14 @@ namespace aFuntool {
                               const char *ti, time_t t,
                               const char *file, int line, const char *func,
                               const char *info){
-#define FORMAT "%s/[%s] %d %d {%s %ld} (%s:%d at %s) : '%s' \n"
+#define FORMAT "%s/[%s] %d %d {%s %lld} (%s:%d at %s) : '%s' \n"
         /* 写入文件日志 */
         if (log_ != nullptr) {
-            fprintf(log_, FORMAT, LogLevelName[level], id, pid_, tid, ti, t, file, line, func, info);
+            fprintf(log_, FORMAT, LogLevelName[level], id, pid_, tid, ti, static_cast<long long>(t), file, line, func, info);
             fflush(log_);
         }
         if (csv_ != nullptr) {
-            fprintf(csv_, CSV_FORMAT, LogLevelName[level], id, pid_, tid, ti, t, file, line, func, info);
+            fprintf(csv_, CSV_FORMAT, LogLevelName[level], id, pid_, tid, ti, static_cast<long long>(t), file, line, func, info);
             fflush(csv_);
         }
 
@@ -231,17 +231,15 @@ namespace aFuntool {
                                   const char *ti, time_t t,
                                   const char *file, int line, const char *func,
                                   const char *info){
-#define FORMAT_SHORT "\r* %s[%s] %d %s %ld (%s:%d) : %s \n"  // 显示到终端, 添加\r回车符确保顶行显示
-#define STD_BUF_SIZE (strlen(info) + 1024)
         if (level < log_warning) {
-            printf_stdout(STD_BUF_SIZE, FORMAT_SHORT, LogLevelNameLong[level], id, tid, ti, t, file, line, info);
+            cout << "\r* " << LogLevelName[level] << "/[" << id << "] " << tid;
+            cout << " " << t << " " << ti << " (" << file << ":" << line << ") : '" << info << "'\n";
             fflush(stdout);
         } else {
-            printf_stderr(STD_BUF_SIZE, FORMAT_SHORT, LogLevelNameLong[level], id, tid, ti, t, file, line, info);
+            cerr << "\r* " << LogLevelName[level] << "/[" << id << "] " << tid;
+            cerr << " " << t << " " << ti << " (" << file << ":" << line << ") : '" << info << "'\n";
             fflush(stderr);
         }
-#undef FORMAT_SHORT
-#undef STD_BUF_SIZE
     }
 
 /**

+ 5 - 0
src/tool/stdio.cpp

@@ -16,6 +16,11 @@
  * 实际上, khbit只能代表有内容输入而无法确定内容是否已经输入到缓冲区中
  */
 
+namespace aFuntool {
+    OutStream cout{printf_stdout};
+    OutStream cerr{printf_stderr};
+}
+
 #ifdef aFunWIN32_NO_CYGWIN
 #include <cstring>
 #include <cstdarg>

+ 6 - 7
test/src/run-code.cpp

@@ -59,7 +59,7 @@ public:
     ~Literaler1() override = default;
 
     void getObject(const std::string &literal, char prefix, Inter &inter) override {
-        printf_stdout(0, "Literaler1: %s %c\n", literal.c_str(), prefix == NUL ? '-' : prefix);
+        aFuntool::cout << "Literaler1: " << literal << (prefix == NUL ? '-' : prefix) << "\n";
         new ExeActivation(func_code, inter);
     }
 };
@@ -76,7 +76,7 @@ public:
     ~CBV1() override = default;
 
     void callBack(Inter &inter) override {
-        printf_stdout(0, "CallBackVar callback\n");
+        aFuntool::cout << "CallBackVar callback\n";
         new ExeActivation(func_code, inter);
     }
 };
@@ -87,20 +87,19 @@ int main() {
 
     auto obj = new Object("Object", inter);
     inter.getGlobalVarlist()->defineVar("test-var", obj);
-    printf_stdout(0, "obj: %p\n", obj);
+    aFuntool::cout << "obj: " << obj << "\n";
 
     auto func = new Func1(inter);
     inter.getGlobalVarlist()->defineVar("test-func", func);
-    printf_stdout(0, "func: %p\n", func);
+    aFuntool::cout << "func: " << func << "\n";
 
     auto literaler = new Literaler1(inter);
     inter.getGlobalVarlist()->defineVar("test-literaler", literaler);
-    printf_stdout(0, "literaler: %p\n", literaler);
+    aFuntool::cout << "literaler: " << literaler << "\n";
 
     auto cbv = new CBV1(inter);
     inter.getGlobalVarlist()->defineVar("test-cbv", cbv);
-    printf_stdout(0, "cbv: %p\n", cbv);
-    fputs_stdout("\n");
+    aFuntool::cout << "cbv: " << cbv << "\n\n";
     inter.getEnvVarSpace().setNumber("sys:error_std", 1);
 
     {