elog_flash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2015-2017, 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. * Function: Save log to flash. Must use EasyFlash(https://github.com/armink/EasyFlash) library.
  26. * Created on: 2015-06-05
  27. */
  28. #define LOG_TAG "elog.flash"
  29. #include "elog_flash.h"
  30. #include <easyflash.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef ELOG_FLASH_USING_BUF_MODE
  35. /* flash log buffer */
  36. static char log_buf[ELOG_FLASH_BUF_SIZE] = { 0 };
  37. /* current flash log buffer write position */
  38. static size_t cur_buf_size = 0;
  39. #endif
  40. /* initialize OK flag */
  41. static bool init_ok = false;
  42. /* the flash log buffer lock enable or disable. default is enable */
  43. static bool log_buf_lock_enabled = true;
  44. /* the flash log buffer is locked before enable. */
  45. static bool log_buf_is_locked_before_enable = false;
  46. /* the flash log buffer is locked before disable. */
  47. static bool log_buf_is_locked_before_disable = false;
  48. static void log_buf_lock(void);
  49. static void log_buf_unlock(void);
  50. /**
  51. * EasyLogger flash log plugin initialize.
  52. *
  53. * @return result
  54. */
  55. ElogErrCode elog_flash_init(void) {
  56. ElogErrCode result = ELOG_NO_ERR;
  57. /* buffer size must be word alignment */
  58. ELOG_ASSERT(ELOG_FLASH_BUF_SIZE % 4 == 0);
  59. #ifdef ELOG_FLASH_USING_BUF_MODE
  60. /* initialize current flash log buffer write position */
  61. cur_buf_size = 0;
  62. #endif
  63. /* port initialize */
  64. elog_flash_port_init();
  65. /* initialize OK */
  66. init_ok = true;
  67. return result;
  68. }
  69. /**
  70. * Read and output log which saved in flash.
  71. *
  72. * @param index index for saved log. @note It will auto word alignment.
  73. * Minimum index is 0.
  74. * Maximum index is log used flash total size - 1.
  75. * @param size
  76. */
  77. void elog_flash_output(size_t index, size_t size) {
  78. /* 128 bytes buffer */
  79. uint32_t buf[32] = { 0 };
  80. size_t log_total_size = ef_log_get_used_size();
  81. size_t buf_size = sizeof(buf);
  82. size_t read_size = 0, read_overage_size = 0;
  83. /* word alignment for index */
  84. index = index / 4 * 4;
  85. if (index + size > log_total_size) {
  86. log_i("The output position and size is out of bound. The max size is %d.", log_total_size);
  87. return;
  88. }
  89. /* must be call this function after initialize OK */
  90. ELOG_ASSERT(init_ok);
  91. /* lock flash log buffer */
  92. log_buf_lock();
  93. /* output all flash saved log. It will use filter */
  94. while (true) {
  95. if (read_size + buf_size < size) {
  96. ef_log_read(index + read_size, buf, buf_size);
  97. elog_flash_port_output((const char*)buf, buf_size);
  98. read_size += buf_size;
  99. } else {
  100. /* flash read is word alignment */
  101. if ((size - read_size) % 4 == 0) {
  102. read_overage_size = 0;
  103. } else {
  104. read_overage_size = 4 - ((size - read_size) % 4);
  105. }
  106. ef_log_read(index + read_size, buf, size - read_size + read_overage_size);
  107. elog_flash_port_output((const char*) buf + read_overage_size, size - read_size);
  108. /* output newline sign */
  109. elog_flash_port_output(ELOG_NEWLINE_SIGN, strlen(ELOG_NEWLINE_SIGN));
  110. break;
  111. }
  112. }
  113. /* unlock flash log buffer */
  114. log_buf_unlock();
  115. }
  116. /**
  117. * Read and output all log which saved in flash.
  118. */
  119. void elog_flash_output_all(void) {
  120. elog_flash_output(0, ef_log_get_used_size());
  121. }
  122. /**
  123. * Read and output recent log which saved in flash.
  124. *
  125. * @param size recent log size
  126. */
  127. void elog_flash_output_recent(size_t size) {
  128. size_t max_size = ef_log_get_used_size();
  129. if (size == 0) {
  130. return;
  131. }
  132. if (size > max_size) {
  133. log_i("The output size is out of bound. The max size is %d.", max_size);
  134. } else {
  135. elog_flash_output(max_size - size, size);
  136. }
  137. }
  138. /**
  139. * Write log to flash. The flash write use buffer mode.
  140. *
  141. * @param log log
  142. * @param size log size
  143. */
  144. void elog_flash_write(const char *log, size_t size) {
  145. #ifdef ELOG_FLASH_USING_BUF_MODE
  146. size_t write_size = 0, write_index = 0;
  147. #else
  148. size_t write_size_temp = 0;
  149. EfErrCode result = EF_NO_ERR;
  150. /* write some '\r' for word alignment */
  151. char write_overage_c[4] = { '\r', '\r', '\r', '\r' };
  152. #endif
  153. /* must be call this function after initialize OK */
  154. ELOG_ASSERT(init_ok);
  155. /* lock flash log buffer */
  156. log_buf_lock();
  157. #ifdef ELOG_FLASH_USING_BUF_MODE
  158. while (true) {
  159. if (cur_buf_size + size > ELOG_FLASH_BUF_SIZE) {
  160. write_size = ELOG_FLASH_BUF_SIZE - cur_buf_size;
  161. elog_memcpy(log_buf + cur_buf_size, log + write_index, write_size);
  162. write_index += write_size;
  163. size -= write_size;
  164. cur_buf_size += write_size;
  165. /* unlock flash log buffer */
  166. log_buf_unlock();
  167. /* write all buffered log to flash, cur_buf_size will reset */
  168. elog_flash_flush();
  169. /* lock flash log buffer */
  170. log_buf_lock();
  171. } else {
  172. elog_memcpy(log_buf + cur_buf_size, log + write_index, size);
  173. cur_buf_size += size;
  174. break;
  175. }
  176. }
  177. #else
  178. /* calculate the word alignment write size */
  179. write_size_temp = size / 4 * 4;
  180. /* write log to flash */
  181. result = ef_log_write((uint32_t *) log, write_size_temp);
  182. /* write last word alignment data */
  183. if ((result == EF_NO_ERR) && (write_size_temp != size)) {
  184. elog_memcpy(write_overage_c, log + write_size_temp, size - write_size_temp);
  185. ef_log_write((uint32_t *) write_overage_c, 4);
  186. }
  187. #endif
  188. /* unlock flash log buffer */
  189. log_buf_unlock();
  190. }
  191. #ifdef ELOG_FLASH_USING_BUF_MODE
  192. /**
  193. * write all buffered log to flash
  194. */
  195. void elog_flash_flush(void) {
  196. size_t write_overage_size = 0;
  197. /* must be call this function after initialize OK */
  198. ELOG_ASSERT(init_ok);
  199. /* lock flash log buffer */
  200. log_buf_lock();
  201. /* flash write is word alignment */
  202. if (cur_buf_size % 4 != 0) {
  203. write_overage_size = 4 - (cur_buf_size % 4);
  204. }
  205. /* fill '\r' for word alignment */
  206. memset(log_buf + cur_buf_size, '\r', write_overage_size);
  207. /* write all buffered log to flash */
  208. ef_log_write((uint32_t *) log_buf, cur_buf_size + write_overage_size);
  209. /* reset position */
  210. cur_buf_size = 0;
  211. /* unlock flash log buffer */
  212. log_buf_unlock();
  213. }
  214. #endif
  215. /**
  216. * clean all log which in flash and ram buffer
  217. */
  218. void elog_flash_clean(void) {
  219. EfErrCode clean_result = EF_NO_ERR;
  220. /* must be call this function after initialize OK */
  221. ELOG_ASSERT(init_ok);
  222. /* lock flash log buffer */
  223. log_buf_lock();
  224. /* clean all log which in flash */
  225. clean_result = ef_log_clean();
  226. #ifdef ELOG_FLASH_USING_BUF_MODE
  227. /* reset position */
  228. cur_buf_size = 0;
  229. #endif
  230. /* unlock flash log buffer */
  231. log_buf_unlock();
  232. if(clean_result == EF_NO_ERR) {
  233. log_i("All logs which in flash is clean OK.");
  234. } else {
  235. log_e("Clean logs which in flash has an error!");
  236. }
  237. }
  238. /**
  239. * enable or disable flash plugin lock
  240. * @note disable this lock is not recommended except you want output system exception log
  241. *
  242. * @param enabled true: enable false: disable
  243. */
  244. void elog_flash_lock_enabled(bool enabled) {
  245. log_buf_lock_enabled = enabled;
  246. /* it will re-lock or re-unlock before log buffer lock enable */
  247. if (log_buf_lock_enabled) {
  248. if (!log_buf_is_locked_before_disable && log_buf_is_locked_before_enable) {
  249. /* the log buffer lock is unlocked before disable, and the lock will unlocking after enable */
  250. elog_flash_port_lock();
  251. } else if (log_buf_is_locked_before_disable && !log_buf_is_locked_before_enable) {
  252. /* the log buffer lock is locked before disable, and the lock will locking after enable */
  253. elog_flash_port_unlock();
  254. }
  255. }
  256. }
  257. /**
  258. * lock flash log buffer
  259. */
  260. static void log_buf_lock(void) {
  261. if (log_buf_lock_enabled) {
  262. elog_flash_port_lock();
  263. log_buf_is_locked_before_disable = true;
  264. } else {
  265. log_buf_is_locked_before_enable = true;
  266. }
  267. }
  268. /**
  269. * unlock flash log buffer
  270. */
  271. static void log_buf_unlock(void) {
  272. if (log_buf_lock_enabled) {
  273. elog_flash_port_unlock();
  274. log_buf_is_locked_before_disable = false;
  275. } else {
  276. log_buf_is_locked_before_enable = false;
  277. }
  278. }