Bladeren bron

feat: 使用日志系统输出大部分信息

SongZihuan 3 jaren geleden
bovenliggende
commit
82ba262235
8 gewijzigde bestanden met toevoegingen van 49 en 47 verwijderingen
  1. 15 10
      src/core/env.c
  2. 17 21
      src/core/gc.c
  3. 3 3
      src/core/run.c
  4. 7 7
      src/main.c
  5. 4 3
      src/main_build.c
  6. 1 1
      src/main_run.c
  7. 1 1
      src/runtime/aFunlang.c
  8. 1 1
      src/runtime/base/base.c

+ 15 - 10
src/core/env.c

@@ -683,17 +683,17 @@ int32_t *findEnvVarNumber(char *name, af_Environment *env) {
 
 static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env) {
     if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
-        fprintf(stderr, "NORMAL msg: %p error\n", msg->msg);
+        writeErrorLog(aFunCoreLogger, "NORMAL msg: %p error", msg->msg);
         return;
     }
     gc_delReference(*(af_Object **)msg->msg);
     if (!is_gc)
-        printf("NORMAL Point: %p\n", *(af_Object **)msg->msg);
+        writeInfoLog(aFunCoreLogger, "NORMAL Point: %p", *(af_Object **)msg->msg);
 }
 
 static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
     if (msg->msg == NULL || *(af_ErrorInfo **)msg->msg == NULL) {
-        printf("ERROR msg: %p error\n", msg->msg);
+        writeErrorLog(aFunCoreLogger, "ERROR msg: %p error", msg->msg);
         return;
     }
     if (!is_gc)
@@ -703,20 +703,20 @@ static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
 
 static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env) {
     if (msg->msg == NULL || *(af_ImportInfo **)msg->msg == NULL) {
-        printf("IMPORT msg: %p error\n", msg->msg);
+        writeErrorLog(aFunCoreLogger, "IMPORT msg: %p error", msg->msg);
         return;
     }
     af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
     if (ii->obj == NULL) {
-        printf("IMPORT msg: %p do not get obj\n", msg->msg);
+        writeInfoLog(aFunCoreLogger, "IMPORT msg: %p do not get obj", msg->msg);
         return;
     }
 
     if (ii->mark != NULL) {
         makeVarToProtectVarSpace(ii->mark, 3, 3, 3, ii->obj, env);
-        printf("IMPORT point: [%s] %p \n", ii->mark, ii->obj);
+        writeInfoLog(aFunCoreLogger, "IMPORT point: [%s] %p", ii->mark, ii->obj);
     } else
-        printf("IMPORT point: <no-name> %p \n", ii->obj);
+        writeInfoLog(aFunCoreLogger, "IMPORT point: <no-name> %p", ii->obj);
     freeImportInfo(ii);
 }
 
@@ -762,7 +762,7 @@ void freeEnvironment(af_Environment *env) {
     freeAllTopMsgProcess(env->process);
 
     if (!res)
-        printf("iterDestruct Error\n");
+        writeErrorLog(aFunCoreLogger, "Run iterDestruct error.");
     free(env);
 }
 
@@ -809,7 +809,7 @@ bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
 
 static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env){
     if (next == NULL && env->activity->body_next == NULL && env->activity->type == act_func) {
-        printf("Tail call optimization\n");
+        writeInfoLog(aFunCoreLogger, "Tail call optimization");
         tailCallActivity(env->activity);
         setActivityBtTop(bt, env->activity);
         env->activity->optimization = true;
@@ -848,6 +848,8 @@ static bool isInfixFunc(af_Code *code, af_Environment *env) {
 bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
     af_Code *next;
     next = getCodeNext(bt);
+    writeInfoLog(aFunCoreLogger, "Run execution");
+
     if (bt->type != code_block || bt->block.is_empty) {
         pushMessageDown(makeERRORMessage(SYNTAX_ERROR, NOT_CODE_INFO, env), env);
         return false;
@@ -869,6 +871,7 @@ bool pushFuncActivity(af_Code *bt, af_Environment *env) {
     af_Object *parentheses_call = env->activity->parentheses_call;
     env->activity->parentheses_call = NULL;
 
+    writeInfoLog(aFunCoreLogger, "Run func");
     next = getCodeNext(bt);
     switch (bt->block.type) {
         case curly:
@@ -918,6 +921,7 @@ bool pushFuncActivity(af_Code *bt, af_Environment *env) {
 bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environment *env) {
     setActivityBtNext(bt->next, env->activity);
 
+    writeInfoLog(aFunCoreLogger, "Run literal");
     newActivity(bt, bt->next, false, env);
     env->activity->is_literal = true;
     pushLiteralData(strCopy(data), env);  // FuncBody的释放导致code和literal_data释放, 所以要复制
@@ -927,6 +931,7 @@ bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environmen
 bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env) {
     setActivityBtNext(bt->next, env->activity);
 
+    writeInfoLog(aFunCoreLogger, "Run variable");
     newActivity(bt, bt->next, false, env);
     env->activity->is_obj_func = true;
     return setFuncActivityToArg(func, env);
@@ -936,7 +941,7 @@ bool pushMacroFuncActivity(af_Object *func, af_Environment *env) {
     /* Macro是隐式调用, bt不移动 */
     /* 沿用activity */
 
-    printf("Run macro\n");
+    writeInfoLog(aFunCoreLogger, "Run macro");
     if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) { // 释放外部变量空间
         env->activity->new_vs_count = 0;
         pushMessageDown(makeERRORMessage(RUN_ERROR, FREE_VARSPACE_INFO, env), env);

+ 17 - 21
src/core/gc.c

@@ -315,7 +315,7 @@ static void freeValue(af_Environment *env) {
     for (af_ObjectData *od = env->core->gc_ObjectData, *next; od != NULL; od = next) {
         next = od->gc.next;
         if (!od->gc.info.reachable) {
-            printf("- gc free ObjectData: %p\n", od);
+            writeInfoLog(aFunCoreLogger, "- gc free ObjectData: %p", od);
             freeObjectData(od, env);
         }
     }
@@ -323,7 +323,7 @@ static void freeValue(af_Environment *env) {
     for (af_Object *obj = env->core->gc_Object, *next; obj != NULL; obj = next) {
         next = obj->gc.next;
         if (!obj->gc.info.reachable) {
-            printf("- gc free Object: %p\n", obj);
+            writeInfoLog(aFunCoreLogger, "- gc free Object: %p", obj);
             freeObjectByCore(obj, env->core);
         }
     }
@@ -331,7 +331,7 @@ static void freeValue(af_Environment *env) {
     for (af_VarSpace *vs = env->core->gc_VarSpace, *next; vs != NULL; vs = next) {
         next = vs->gc.next;
         if (!vs->gc.info.reachable) {
-            printf("- gc free VarSpace: %p\n", vs);
+            writeInfoLog(aFunCoreLogger, "- gc free VarSpace: %p", vs);
             freeVarSpaceByCore(vs, env->core);
         }
     }
@@ -339,7 +339,7 @@ static void freeValue(af_Environment *env) {
     for (af_Var *var = env->core->gc_Var, *next; var != NULL; var = next) {
         next = var->gc.next;
         if (!var->gc.info.reachable) {
-            printf("- gc free Var: %p\n", var);
+            writeInfoLog(aFunCoreLogger, "- gc free Var: %p", var);
             freeVarByCore(var, env->core);
         }
     }
@@ -415,42 +415,38 @@ void gc_freeAllValue(af_Environment *env) {
 
 void printGCByCode(af_Core *core) {
     bool success = true;
-    printf("GC ObjectData:\n");
     for (af_ObjectData *od = core->gc_ObjectData; od != NULL; od = od->gc.next) {
         if (od->gc.info.reference != 0) {
-            printf("########## ########## ");
+            writeWarningLog(aFunCoreLogger, "af_ObjectData(%p) Reference: %d", od, od->gc.info.reference);
             success = false;
-        }
-        printf("af_ObjectData(%p) Reference: %d\n", od, od->gc.info.reference);
+        } else
+            writeInfoLog(aFunCoreLogger, "af_ObjectData(%p) Reference: %d", od, od->gc.info.reference);
     }
 
-    printf("GC Object:\n");
     for (af_Object *obj = core->gc_Object; obj != NULL; obj = obj->gc.next) {
         if (obj->gc.info.reference != 0) {
-            printf("########## ########## ");
+            writeWarningLog(aFunCoreLogger, "af_Object(%p->%p) Reference: %d", obj, obj->data, obj->gc.info.reference);
             success = false;
-        }
-        printf("af_Object(%p->%p) Reference: %d\n", obj, obj->data, obj->gc.info.reference);
+        } else
+            writeInfoLog(aFunCoreLogger, "af_Object(%p->%p) Reference: %d", obj, obj->data, obj->gc.info.reference);
     }
 
-    printf("GC VarSpace:\n");
     for (af_VarSpace *vs = core->gc_VarSpace; vs != NULL; vs = vs->gc.next) {
         if (vs->gc.info.reference != 0) {
-            printf("########## ########## ");
+            writeWarningLog(aFunCoreLogger, "af_VarSpace(%p) Reference: %d", vs, vs->gc.info.reference);
             success = false;
-        }
-        printf("af_VarSpace(%p) Reference: %d\n", vs, vs->gc.info.reference);
+        } else
+            writeInfoLog(aFunCoreLogger, "af_VarSpace(%p) Reference: %d", vs, vs->gc.info.reference);
     }
 
-    printf("GC Var:\n");
     for (af_Var *var = core->gc_Var; var != NULL; var = var->gc.next) {
         if (var->gc.info.reference != 0) {
-            printf("########## ########## ");
+            writeWarningLog(aFunCoreLogger, "af_Var(%p) Reference: %d", var, var->gc.info.reference);
             success = false;
-        }
-        printf("af_Var(%p) Reference: %d\n", var, var->gc.info.reference);
+        } else
+            writeInfoLog(aFunCoreLogger, "af_Var(%p) Reference: %d", var, var->gc.info.reference);
     }
 
     if (!success)
-        printf("gc warning.\n");
+        writeWarningLog(aFunCoreLogger, "gc warning.");
 }

+ 3 - 3
src/core/run.c

@@ -62,7 +62,7 @@ static bool checkLiteral(af_Message **msg, af_Environment *env) {
     freeAllLiteralData(env->activity->ld);
     env->activity->ld = NULL;
     env->activity->is_literal = false;
-    printf("Literal %p\n", obj);
+    writeInfoLog(aFunCoreLogger, "Literal %p", obj);
     return true;
 }
 
@@ -133,7 +133,7 @@ static bool iterCodeInit(af_Code *code, int mode, af_Environment *env) {
 
             char *name = getFileName(code->path);
             pushImportActivity(code, NULL, name, env);
-            printf("name = %s\n", name);
+            writeInfoLog(aFunCoreLogger, "Top-Import name = %s", name);
             free(name);
             break;
         }
@@ -212,7 +212,7 @@ static bool codeElement(af_Code *code, af_Environment *env) {
 
     pushMessageDown(makeNORMALMessage(obj), env);
     setActivityBtNext(env->activity->bt_next->next, env->activity);
-    printf("Get Variable %s : %p\n", code->element.data, obj);
+    writeInfoLog(aFunCoreLogger, "Get Variable %s : %p", code->element.data, obj);
     return false;
 }
 

+ 7 - 7
src/main.c

@@ -118,7 +118,7 @@ static void printHelp(void) {
  * 目标: 打印参数错误信息
  */
 static void printError(ff_FFlags *ff) {
-    fprintf(stderr, "[aFunlang] Command line argument error (%s)\n", ff_getChild(ff));
+    writeErrorLog(aFunlangLogger, "Command line argument error (%s).", ff_getChild(ff));
     printHelp();
 }
 
@@ -160,7 +160,7 @@ static int mainRun(ff_FFlags *ff) {
     char **argv = NULL;
     int argc = ff_get_process_argv(&argv, ff);
     if (argv == 0) {
-        fprintf(stderr, "There are not file to run.\n");
+        writeErrorLog(aFunlangLogger, "There are not file to run.");
         return 1;
     }
 
@@ -230,7 +230,7 @@ static int mainCL(ff_FFlags *ff) {
     RunList *rl = getRunList(ff, &command_line, &save_aub);
 
     if (rl == NULL && !command_line) {
-        fprintf(stderr, "[aFunlang] There are not code to run.\n");
+        writeErrorLog(aFunlangLogger, "There are not code to run.");
         printError(ff);
         return EXIT_FAILURE;
     }
@@ -273,14 +273,14 @@ static int mainBuild(ff_FFlags *ff) {
         switch (mark) {
             case 'o':
                 if (path != NULL) {
-                    fprintf(stderr, "[aFunlang] Argument conflict (out, path).\n");
+                    writeErrorLog(aFunlangLogger, "Argument conflict (out, path).");
                     goto error;
                 }
                 out_put = text;
                 break;
             case 'p':
                 if (out_put != NULL) {
-                    fprintf(stderr, "[aFunlang] Argument conflict (out, path).\n");
+                    writeErrorLog(aFunlangLogger, "Argument conflict (out, path).");
                     goto error;
                 }
                 path = text;
@@ -301,14 +301,14 @@ out:
 
         /* 如果没有参数 */
         if (!ff_getopt_wild(&text, ff)) {
-            fprintf(stderr, "[aFunlang] There are not source file to build.\n");
+            writeErrorLog(aFunlangLogger, "There are not source file to build.");
             goto error;
         } else
             in = text;
 
         /* 如果还有第二个参数 */
         if (ff_getopt_wild(&text, ff)) {
-            fprintf(stderr, "[aFunlang] There are too many source file to build. (Do not use --out option)\n");
+            writeErrorLog(aFunlangLogger, "There are too many source file to build. (Do not use --out option).");
             goto error;
         }
 

+ 4 - 3
src/main_build.c

@@ -1,4 +1,5 @@
 #include "aFun.h"
+#include "main_build.h"
 
 int buildFileOutput(FilePath out, FilePath in, bool force) {
     if (!force) {
@@ -6,17 +7,17 @@ int buildFileOutput(FilePath out, FilePath in, bool force) {
         time_t time_2 = getFileMTime(out);
 
         if (time_1 == 0 && time_2 == 0) {
-            fprintf(stderr, "File not exists [%s].", in);
+            writeErrorLog(aFunlangLogger, "File not exists [%s].", in);
             return -1;
         }
 
         if (time_2 >= time_1) {
-            fprintf(stdout, "[aFunlang] Source already build (%s), use --force to build again.\n", in);
+            writeErrorLog(aFunlangLogger, "Source already build (%s), use --force to build again.", in);
             return 0;
         }
     }
 
-    fprintf(stdout, "[aFunlang] File (%s) will be build. (%s)\n", in, out);
+    writeErrorLog(aFunlangLogger, "File (%s) will be build. (%s)", in, out);
     return buildFile(out, in, stderr);
 }
 

+ 1 - 1
src/main_run.c

@@ -81,7 +81,7 @@ int runCodeFromRunList(RunList *run_list, RunList **bak, bool save_afb, af_Envir
         }
 
         if (isCoreExit(env) == 1) {
-            fprintf((exit_code == 0) ? stdout : stderr, "[aFunlang] aFun core exit, exit_code = %d\n", exit_code);
+            writeErrorLog(aFunlangLogger, "aFun core exit, exit_code = %d", exit_code);
             break;
         }
     }

+ 1 - 1
src/runtime/aFunlang.c

@@ -23,7 +23,7 @@ af_Environment *creatAFunEnviroment(int argc, char **argv){
     af_Code *code = NULL;
 
     for(int i = 0; i < argc; i++)
-        printf("[aFunlang] Env-arg %d. %s\n", i, argv[i]);
+        writeInfoLog(aFunCoreLogger, "[aFunlang] Env-arg %d. %s", i, argv[i]);
 
     env->core->argc->num = argc;
     for (int i = 0; i < argc; i++) {

+ 1 - 1
src/runtime/base/base.c

@@ -1,7 +1,7 @@
 #include "__base.h"
 
 int aFunTool_base(af_Code **code, af_Object *visitor, af_VarSpace *vs, af_Environment *env) {
-    printf("Run runtimeTool-Base %p\n", vs);
+    writeInfoLog(aFunCoreLogger, "Run runtimeTool-Base %p", vs);
     makeLiteralRegexFromList(literal, env);
     makeStrFunc(visitor, vs, env);
     makeQuitFunc(visitor, vs, env);