ulog_easyflash_cfg.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * This file is part of the EasyFlash Library.
  3. *
  4. * Copyright (c) 2014-2018, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * The ulog filter configuration store implement by EasyFlash.
  26. * Created on: 2018-11-08
  27. */
  28. #include <rtthread.h>
  29. #ifdef ULOG_EASYFLASH_CFG_SAVE_ENABLE
  30. #include <easyflash.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #define LOG_TAG "easyflash"
  34. #include <ulog.h>
  35. #define ENV_FILTER_GLOBAL_LVL_NAME "ulog.lvl"
  36. #define ENV_FILTER_GLOBAL_TAG_NAME "ulog.tag"
  37. #define ENV_FILTER_GLOBAL_KW_NAME "ulog.kw"
  38. #define ENV_FILTER_TAG_LVL_NAME "ulog.tag_lvl"
  39. extern size_t ulog_ultoa(char *s, unsigned long int n);
  40. /**
  41. * load the ulog configuration on flash
  42. *
  43. * @return result, 0 : success, else error
  44. *
  45. * @note don't using `&` and `##` on log tag definition when using this function.
  46. */
  47. int ulog_ef_filter_cfg_load(void)
  48. {
  49. char *value;
  50. /* restore the saving global level */
  51. if ((value = ef_get_env(ENV_FILTER_GLOBAL_LVL_NAME)) != NULL)
  52. {
  53. ulog_global_filter_lvl_set(atoi(value));
  54. }
  55. /* restore the saving global tag */
  56. if ((value = ef_get_env(ENV_FILTER_GLOBAL_TAG_NAME)) != NULL)
  57. {
  58. ulog_global_filter_tag_set(value);
  59. }
  60. /* restore the saving global kw */
  61. if ((value = ef_get_env(ENV_FILTER_GLOBAL_KW_NAME)) != NULL)
  62. {
  63. ulog_global_filter_kw_set(value);
  64. }
  65. /* restore the saving tag level list */
  66. if ((value = ef_get_env(ENV_FILTER_TAG_LVL_NAME)) != NULL)
  67. {
  68. char lvl_num[11], tag[ULOG_FILTER_TAG_MAX_LEN + 1], *lvl_pos, *next_node;
  69. rt_size_t node_len;
  70. /* decode every tag's level */
  71. while (1)
  72. {
  73. /* find every node */
  74. if ((next_node = strstr(value, "##")) != NULL)
  75. {
  76. node_len = next_node - value;
  77. }
  78. else
  79. {
  80. node_len = rt_strlen(value);
  81. }
  82. /* find level pos */
  83. lvl_pos = strstr(value, "&");
  84. if (lvl_pos != NULL && lvl_pos < value + node_len)
  85. {
  86. rt_strncpy(tag, value, lvl_pos - value);
  87. rt_strncpy(lvl_num, lvl_pos + 1, value + node_len - lvl_pos - 1);
  88. tag[lvl_pos - value] = '\0';
  89. lvl_num[value + node_len - lvl_pos - 1] = '\0';
  90. /* add a tag's level filter */
  91. ulog_tag_lvl_filter_set(tag, atoi(lvl_num));
  92. }
  93. else
  94. {
  95. LOG_W("Warning: tag's level decode failed!");
  96. break;
  97. }
  98. if (next_node)
  99. {
  100. value = next_node + 2;
  101. }
  102. else
  103. {
  104. break;
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. INIT_APP_EXPORT(ulog_ef_filter_cfg_load);
  111. /**
  112. * save the ulog filter configuration to flash
  113. *
  114. * @note don't using `&` and `##` on log tag definition when using this function.
  115. */
  116. void ulog_ef_filter_cfg_save(void)
  117. {
  118. unsigned char *cfgs = NULL;
  119. char lvl_num[11];
  120. /* set the global level env */
  121. {
  122. ulog_ultoa(lvl_num, ulog_global_filter_lvl_get());
  123. ef_set_env(ENV_FILTER_GLOBAL_LVL_NAME, lvl_num);
  124. }
  125. /* set the global tag env */
  126. if (rt_strlen(ulog_global_filter_tag_get()))
  127. {
  128. ef_set_env(ENV_FILTER_GLOBAL_TAG_NAME, ulog_global_filter_tag_get());
  129. }
  130. else if(ef_get_env(ENV_FILTER_GLOBAL_TAG_NAME))
  131. {
  132. ef_del_env(ENV_FILTER_GLOBAL_TAG_NAME);
  133. }
  134. /* set the global kw env */
  135. if (rt_strlen(ulog_global_filter_kw_get()))
  136. {
  137. ef_set_env(ENV_FILTER_GLOBAL_KW_NAME, ulog_global_filter_kw_get());
  138. }
  139. else if(ef_get_env(ENV_FILTER_GLOBAL_KW_NAME))
  140. {
  141. ef_del_env(ENV_FILTER_GLOBAL_KW_NAME);
  142. }
  143. /* set the tag's level env */
  144. {
  145. rt_slist_t *node;
  146. ulog_tag_lvl_filter_t tag_lvl = NULL;
  147. rt_size_t node_size, tag_len, lvl_len;
  148. int cfgs_size = 0;
  149. for (node = rt_slist_first(ulog_tag_lvl_list_get()); node; node = rt_slist_next(node))
  150. {
  151. tag_lvl = rt_slist_entry(node, struct ulog_tag_lvl_filter, list);
  152. ulog_ultoa(lvl_num, tag_lvl->level);
  153. tag_len = rt_strlen(tag_lvl->tag);
  154. lvl_len = rt_strlen(lvl_num);
  155. /* env string format: tag_name1&tag_lvl1##tag_name2&tag_lvl2## */
  156. node_size = tag_len + 1 + lvl_len + 2;
  157. cfgs_size += node_size;
  158. cfgs = (unsigned char *) rt_realloc(cfgs, cfgs_size);
  159. if (cfgs == NULL)
  160. {
  161. LOG_W("Warning: no memory for save cfgs");
  162. goto __exit;
  163. }
  164. rt_memcpy(cfgs + cfgs_size - node_size , tag_lvl->tag, tag_len);
  165. rt_memcpy(cfgs + cfgs_size - node_size + tag_len , "&", 1);
  166. rt_memcpy(cfgs + cfgs_size - node_size + tag_len + 1 , lvl_num, lvl_len);
  167. rt_memcpy(cfgs + cfgs_size - node_size + tag_len + 1 + lvl_len, "##", 2);
  168. }
  169. if((cfgs)&&(cfgs_size>2))
  170. {
  171. cfgs[cfgs_size - 2] = '\0';
  172. ef_set_env(ENV_FILTER_TAG_LVL_NAME, (char *)cfgs);
  173. }
  174. else if(ef_get_env(ENV_FILTER_TAG_LVL_NAME))
  175. {
  176. ef_del_env(ENV_FILTER_TAG_LVL_NAME);
  177. }
  178. }
  179. __exit:
  180. /* save the ulog filter env */
  181. ef_save_env();
  182. if (cfgs)
  183. {
  184. rt_free(cfgs);
  185. }
  186. }
  187. #endif /* ULOG_EASYFLASH_CFG_SAVE_ENABLE */