Jelajahi Sumber

ncd: Implement logging by function implementations.

Ambroz Bizjak 11 tahun lalu
induk
melakukan
9bdb2f3793

+ 1 - 0
blog_channels.txt

@@ -143,3 +143,4 @@ ncd_getenv 4
 BThreadSignal 4
 BThreadSignal 4
 BLockReactor 4
 BLockReactor 4
 ncd_load_module 4
 ncd_load_module 4
+ncd_basic_functions 4

+ 4 - 0
generated/blog_channel_ncd_basic_functions.h

@@ -0,0 +1,4 @@
+#ifdef BLOG_CURRENT_CHANNEL
+#undef BLOG_CURRENT_CHANNEL
+#endif
+#define BLOG_CURRENT_CHANNEL BLOG_CHANNEL_ncd_basic_functions

+ 2 - 1
generated/blog_channels_defines.h

@@ -143,4 +143,5 @@
 #define BLOG_CHANNEL_BThreadSignal 142
 #define BLOG_CHANNEL_BThreadSignal 142
 #define BLOG_CHANNEL_BLockReactor 143
 #define BLOG_CHANNEL_BLockReactor 143
 #define BLOG_CHANNEL_ncd_load_module 144
 #define BLOG_CHANNEL_ncd_load_module 144
-#define BLOG_NUM_CHANNELS 145
+#define BLOG_CHANNEL_ncd_basic_functions 145
+#define BLOG_NUM_CHANNELS 146

+ 1 - 0
generated/blog_channels_list.h

@@ -143,3 +143,4 @@
 {"BThreadSignal", 4},
 {"BThreadSignal", 4},
 {"BLockReactor", 4},
 {"BLockReactor", 4},
 {"ncd_load_module", 4},
 {"ncd_load_module", 4},
+{"ncd_basic_functions", 4},

+ 13 - 0
ncd/NCDInterpreter.c

@@ -121,6 +121,7 @@ static btime_t statement_instance_func_interp_getretrytime (void *vinterp);
 static int statement_instance_func_interp_loadgroup (void *vinterp, const struct NCDModuleGroup *group);
 static int statement_instance_func_interp_loadgroup (void *vinterp, const struct NCDModuleGroup *group);
 static void process_moduleprocess_func_event (struct process *p, int event);
 static void process_moduleprocess_func_event (struct process *p, int event);
 static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
 static int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
+static void function_logfunc (void *vo);
 
 
 #define STATEMENT_LOG(ps, channel, ...) if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, channel)) statement_log(ps, channel, __VA_ARGS__)
 #define STATEMENT_LOG(ps, channel, ...) if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, channel)) statement_log(ps, channel, __VA_ARGS__)
 
 
@@ -214,6 +215,7 @@ int NCDInterpreter_Init (NCDInterpreter *o, NCDProgram program, struct NCDInterp
     o->module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
     o->module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
     o->module_iparams.func_interp_getretrytime = statement_instance_func_interp_getretrytime;
     o->module_iparams.func_interp_getretrytime = statement_instance_func_interp_getretrytime;
     o->module_iparams.func_loadgroup = statement_instance_func_interp_loadgroup;
     o->module_iparams.func_loadgroup = statement_instance_func_interp_loadgroup;
+    o->module_func_params.logfunc = function_logfunc;
     
     
     // init processes list
     // init processes list
     LinkedList1_Init(&o->processes);
     LinkedList1_Init(&o->processes);
@@ -837,6 +839,8 @@ static int eval_func_eval_call (void *user, NCD_string_id_t func_name_id, NCDEva
     
     
     struct NCDModuleFunction_eval_params params;
     struct NCDModuleFunction_eval_params params;
     params.ifunc = ifunc;
     params.ifunc = ifunc;
+    params.params = &p->interp->module_func_params;
+    params.interp_user = ps;
     
     
     return ifunc->function.func_eval(args, mem, out, &params);
     return ifunc->function.func_eval(args, mem, out, &params);
 }
 }
@@ -1366,3 +1370,12 @@ int process_moduleprocess_func_getobj (struct process *p, NCD_string_id_t name,
     
     
     return process_find_object(p, p->num_statements, name, out_object);
     return process_find_object(p, p->num_statements, name, out_object);
 }
 }
+
+void function_logfunc (void *vo)
+{
+    struct statement *ps = vo;
+    ASSERT(ps->inst.istate == SSTATE_FORGOTTEN)
+    
+    statement_logfunc(ps);
+    BLog_Append("func_eval: ");
+}

+ 1 - 0
ncd/NCDInterpreter.h

@@ -112,6 +112,7 @@ typedef struct {
     // common module parameters
     // common module parameters
     struct NCDModuleInst_params module_params;
     struct NCDModuleInst_params module_params;
     struct NCDModuleInst_iparams module_iparams;
     struct NCDModuleInst_iparams module_iparams;
+    struct NCDModuleFunction_params module_func_params;
     
     
     // processes
     // processes
     LinkedList1 processes;
     LinkedList1 processes;

+ 5 - 0
ncd/NCDModule.c

@@ -588,6 +588,11 @@ int NCDModuleProcess_Interp_GetSpecialObj (NCDModuleProcess *o, NCD_string_id_t
     return res;
     return res;
 }
 }
 
 
+BLogContext NCDModuleFunction_LogContext (struct NCDModuleFunction_eval_params const *params)
+{
+    return BLog_MakeContext(params->params->logfunc, params->interp_user);
+}
+
 static int process_args_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
 static int process_args_object_func_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
 {
 {
     NCDModuleProcess *o = NCDObject_DataPtr(obj);
     NCDModuleProcess *o = NCDObject_DataPtr(obj);

+ 29 - 0
ncd/NCDModule.h

@@ -1017,6 +1017,18 @@ struct NCDInterpModuleGroup {
     void *group_state;
     void *group_state;
 };
 };
 
 
+/**
+ * Holds some things passed to function implementations which are presumed
+ * to stay the same within an interpreter (for optimization purposes).
+ */
+struct NCDModuleFunction_params {
+    /**
+     * Log function which appends a log prefix with {@link BLog_Append}.
+     * Its user argument will be the user member of {@link NCDModuleFunction_eval_params}.
+     */
+    BLog_logfunc logfunc;
+};
+
 /**
 /**
  * Contains parameters to {@link NCDModuleFunction_func_eval} that are passed indirectly.
  * Contains parameters to {@link NCDModuleFunction_func_eval} that are passed indirectly.
  */
  */
@@ -1025,6 +1037,17 @@ struct NCDModuleFunction_eval_params {
      * A pointer to the {@link NCDInterpFunction} structure for the function being called.
      * A pointer to the {@link NCDInterpFunction} structure for the function being called.
      */
      */
     struct NCDInterpFunction const *ifunc;
     struct NCDInterpFunction const *ifunc;
+    
+    /**
+     * Pointer to an additional structure needed for the call, containing callbacks to
+     * the interpreter.
+     */
+    struct NCDModuleFunction_params const *params;
+    
+    /**
+     * This is passed back to interpeter callbacks done as part of function evaluation.
+     */
+    void *interp_user;
 };
 };
 
 
 /**
 /**
@@ -1044,6 +1067,12 @@ struct NCDModuleFunction_eval_params {
  */
  */
 typedef int (*NCDModuleFunction_func_eval) (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params);
 typedef int (*NCDModuleFunction_func_eval) (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params);
 
 
+/**
+ * Returns a logging context for use by the function implementation during
+ * function evaluation.
+ */
+BLogContext NCDModuleFunction_LogContext (struct NCDModuleFunction_eval_params const *params);
+
 struct NCDModuleFunction {
 struct NCDModuleFunction {
     /**
     /**
      * The name of the function.
      * The name of the function.

+ 6 - 0
ncd/modules/basic_functions.c

@@ -31,14 +31,20 @@
 
 
 #include <ncd/NCDModule.h>
 #include <ncd/NCDModule.h>
 
 
+#include <generated/blog_channel_ncd_basic_functions.h>
+
+#define FunctionLog(params, ...) BContextLog(NCDModuleFunction_LogContext(params), __VA_ARGS__)
+
 static int error_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
 static int error_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
 {
 {
+    FunctionLog(params, BLOG_ERROR, "error: failing");
     return 0;
     return 0;
 }
 }
 
 
 static int identity_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
 static int identity_eval (NCDEvaluatorArgs args, NCDValMem *mem, NCDValRef *out, struct NCDModuleFunction_eval_params const *params)
 {
 {
     if (NCDEvaluatorArgs_Count(&args) != 1) {
     if (NCDEvaluatorArgs_Count(&args) != 1) {
+        FunctionLog(params, BLOG_ERROR, "identity: need one argument");
         return 0;
         return 0;
     }
     }
     return NCDEvaluatorArgs_EvalArg(&args, 0, mem, out);
     return NCDEvaluatorArgs_EvalArg(&args, 0, mem, out);