(*exos_config_change_cb)(): React on configuration changes

<< 点击显示目录 >>

主页  exOS使用助手 > exOS Automation Help > Development > Programming > exOS Log API reference (exos_log.h) >

(*exos_config_change_cb)(): React on configuration changes

 typedef void (*exos_config_change_cb)(const exos_log_handle_t *log, const exos_log_config_t *new_config, void *user_context);

exos_config_change_cb 回调会在第一次接收到配置(指模块将其缓冲信息转发到日志服务器的时间点)时触发,也会在系统中的模块调用 exos_log_change_config() 时触发 。

参数

log:指向  触发事件的exos_log_handle_t的指针

new_config:指向  包含新日志配置的exos_log_config_t 结构的指针

user_context:用户定义的上下文指针,通过 exos_log_config_listener 传递 。可在回调中检索。

使用示例:

回调可以与 exos_log_change_config()结合使用,以方便 对日志进行相对修改,例如从系统日志配置开始。

重要提示:请确保 exos_log_change 不会在此回调中被自动调用,否则,您将创建一个无限循环,进行毫无意义的配置更改。虽然系统不会瘫痪,但每次更改配置都会让系统中的其他参与者感到厌烦。

 exos_log_handle_t logger;
 bool change_config = false;
 exos_log_config_t config = {};
 //make sure this module is NEVER EVER excluded from logging J
 void config_changed(exos_log_handle_t *log, const exos_log_config_t *new_config, void *user_context)
 {
     //takeover the system configuration, or the last change from the system into the local config structure
     memcpy(&config, new_config, sizeof(exos_log_config_t));
     if(log->excluded)
     {
         exos_log_stdout("I, %s, was excluded from logging :( - but not if I have a say!", log->name);
         //ENABLE me again!  =)
         for(int i=0; i<EXOS_LOG_EXCLUDE_LIST_LEN; i++)
         {
             if(0 == strcmp(log->name,config.excluded[i])) //remove me from the exclude list!
             {
                 config.excluded[i][0] = '\0';
                 //ha! cannot avoid me!
                 exos_log_change_config(log, &config);
             }
         }
     }
 }
 int main()
 {
     exos_log_init(&logger, "MyApplication");
     exos_log_config_listener(&logger, config_changed, NULL); //we dont have a specific user context here, variables are accesses globally..
     ...
     while( .. )
     {
         exos_log_process(&logger); //this is where the callback is triggered
     }

使用 log->excluded,你可以很容易地发现该模块是否被排除在日志记录之外 。该信息基本上是 配置 结构的一部分  ,以字符串的形式给出,但为了方便起见,也可以以布尔变量的形式访问。


 typedef void (*exos_config_change_cb)(const exos_log_handle_t *log, const exos_log_config_t *new_config, void *user_context);

The exos_config_change_cb callback is triggered the first time a configuration is received (meaning the point in time where the module will forward its buffered messages to the log server), and at any other given time when a module in the system calls exos_log_change_config().

Parameters:

log: pointer to the exos_log_handle_t that triggered the event

new_config: pointer to the exos_log_config_t structure that contains the new log configuration

user_context: user defined context pointer that is passed via the exos_log_config_listener. It can be retrieved in the callback.

Example usage:

The callback can conveniently be combinded with exos_log_change_config(), in case you want to make relative changes to the log, for example start out from the system log configuration.

Important: Make sure that exos_log_change is not called automatically within this callback, otherwise, you will create an infinite loop of meaningless configuration changes. Although the system will not break down, changing the configuration whenever it has been changed will probably just be annoying for all other participants in the system.

 exos_log_handle_t logger;
 bool change_config = false;
 exos_log_config_t config = {};
 //make sure this module is NEVER EVER excluded from logging J
 void config_changed(exos_log_handle_t *log, const exos_log_config_t *new_config, void *user_context)
 {
     //takeover the system configuration, or the last change from the system into the local config structure
     memcpy(&config, new_config, sizeof(exos_log_config_t));
     if(log->excluded)
     {
         exos_log_stdout("I, %s, was excluded from logging :( - but not if I have a say!", log->name);
         //ENABLE me again!  =)
         for(int i=0; i<EXOS_LOG_EXCLUDE_LIST_LEN; i++)
         {
             if(0 == strcmp(log->name,config.excluded[i])) //remove me from the exclude list!
             {
                 config.excluded[i][0] = '\0';
                 //ha! cannot avoid me!
                 exos_log_change_config(log, &config);
             }
         }
     }
 }
 int main()
 {
     exos_log_init(&logger, "MyApplication");
     exos_log_config_listener(&logger, config_changed, NULL); //we dont have a specific user context here, variables are accesses globally..
     ...
     while( .. )
     {
         exos_log_process(&logger); //this is where the callback is triggered
     }

You can easily find out whether or not this module has been excluded from logging using log->excluded. This information is basically part of the config structure given as a string, but for convenience reasons, you can also access it as a boolean variable.