elog_async.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2016-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: Logs asynchronous output.
  26. * Created on: 2016-11-06
  27. */
  28. #include <elog.h>
  29. #include <string.h>
  30. #ifdef ELOG_ASYNC_OUTPUT_ENABLE
  31. #ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
  32. #include <pthread.h>
  33. #include <sched.h>
  34. #include <semaphore.h>
  35. /* thread default stack size */
  36. #ifndef ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE
  37. #if PTHREAD_STACK_MIN > 4*1024
  38. #define ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE PTHREAD_STACK_MIN
  39. #else
  40. #define ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE (1*1024)
  41. #endif
  42. /* thread default priority */
  43. #ifndef ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY
  44. #define ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY (sched_get_priority_max(SCHED_RR) - 1)
  45. #endif
  46. /* output thread poll get log buffer size */
  47. #ifndef ELOG_ASYNC_LINE_OUTPUT
  48. #ifndef ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE
  49. #define ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE (ELOG_ASYNC_OUTPUT_BUF_SIZE - 4)
  50. #endif
  51. #else
  52. #ifndef ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE
  53. #define ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE (ELOG_LINE_BUF_SIZE - 4)
  54. #endif
  55. #endif
  56. #endif /* ELOG_ASYNC_OUTPUT_USING_PTHREAD */
  57. /* asynchronous output log notice */
  58. static sem_t output_notice;
  59. /* asynchronous output pthread thread */
  60. static pthread_t async_output_thread;
  61. #endif /* ELOG_ASYNC_OUTPUT_ENABLE */
  62. /* the highest output level for async mode, other level will sync output */
  63. #ifdef ELOG_ASYNC_OUTPUT_LVL
  64. #define OUTPUT_LVL ELOG_ASYNC_OUTPUT_LVL
  65. #else
  66. #define OUTPUT_LVL ELOG_LVL_ASSERT
  67. #endif /* ELOG_ASYNC_OUTPUT_LVL */
  68. /* buffer size for asynchronous output mode */
  69. #ifdef ELOG_ASYNC_OUTPUT_BUF_SIZE
  70. #define OUTPUT_BUF_SIZE ELOG_ASYNC_OUTPUT_BUF_SIZE
  71. #else
  72. #define OUTPUT_BUF_SIZE (ELOG_LINE_BUF_SIZE * 10)
  73. #endif /* ELOG_ASYNC_OUTPUT_BUF_SIZE */
  74. /* Initialize OK flag */
  75. static bool init_ok = false;
  76. /* thread running flag */
  77. static bool thread_running = false;
  78. /* asynchronous output mode enabled flag */
  79. static bool is_enabled = false;
  80. /* asynchronous output mode's ring buffer */
  81. static char log_buf[OUTPUT_BUF_SIZE] = { 0 };
  82. /* log ring buffer write index */
  83. static size_t write_index = 0;
  84. /* log ring buffer read index */
  85. static size_t read_index = 0;
  86. /* log ring buffer full flag */
  87. static bool buf_is_full = false;
  88. /* log ring buffer empty flag */
  89. static bool buf_is_empty = true;
  90. extern void elog_port_output(const char *log, size_t size);
  91. extern void elog_output_lock(void);
  92. extern void elog_output_unlock(void);
  93. /**
  94. * asynchronous output ring buffer used size
  95. *
  96. * @return used size
  97. */
  98. static size_t elog_async_get_buf_used(void) {
  99. if (write_index > read_index) {
  100. return write_index - read_index;
  101. } else {
  102. if (!buf_is_full && !buf_is_empty) {
  103. return OUTPUT_BUF_SIZE - (read_index - write_index);
  104. } else if (buf_is_full) {
  105. return OUTPUT_BUF_SIZE;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. }
  111. /**
  112. * asynchronous output ring buffer remain space
  113. *
  114. * @return remain space
  115. */
  116. static size_t async_get_buf_space(void) {
  117. return OUTPUT_BUF_SIZE - elog_async_get_buf_used();
  118. }
  119. /**
  120. * put log to asynchronous output ring buffer
  121. *
  122. * @param log put log buffer
  123. * @param size log size
  124. *
  125. * @return put log size, the log which beyond ring buffer space will be dropped
  126. */
  127. static size_t async_put_log(const char *log, size_t size) {
  128. size_t space = 0;
  129. space = async_get_buf_space();
  130. /* no space */
  131. if (!space) {
  132. size = 0;
  133. goto __exit;
  134. }
  135. /* drop some log */
  136. if (space <= size) {
  137. size = space;
  138. buf_is_full = true;
  139. }
  140. if (write_index + size < OUTPUT_BUF_SIZE) {
  141. memcpy(log_buf + write_index, log, size);
  142. write_index += size;
  143. } else {
  144. memcpy(log_buf + write_index, log, OUTPUT_BUF_SIZE - write_index);
  145. memcpy(log_buf, log + OUTPUT_BUF_SIZE - write_index,
  146. size - (OUTPUT_BUF_SIZE - write_index));
  147. write_index += size - OUTPUT_BUF_SIZE;
  148. }
  149. buf_is_empty = false;
  150. __exit:
  151. return size;
  152. }
  153. #ifdef ELOG_ASYNC_LINE_OUTPUT
  154. /**
  155. * Get line log from asynchronous output ring buffer.
  156. * It will copy all log when the newline sign isn't find.
  157. *
  158. * @param log get line log buffer
  159. * @param size line log size
  160. *
  161. * @return get line log size, the log size is less than ring buffer used size
  162. */
  163. size_t elog_async_get_line_log(char *log, size_t size) {
  164. size_t used = 0, cpy_log_size = 0;
  165. /* lock output */
  166. elog_output_lock();
  167. used = elog_async_get_buf_used();
  168. /* no log */
  169. if (!used || !size) {
  170. goto __exit;
  171. }
  172. /* less log */
  173. if (used <= size) {
  174. size = used;
  175. }
  176. if (read_index + size < OUTPUT_BUF_SIZE) {
  177. cpy_log_size = elog_cpyln(log, log_buf + read_index, size);
  178. read_index += cpy_log_size;
  179. } else {
  180. cpy_log_size = elog_cpyln(log, log_buf + read_index, OUTPUT_BUF_SIZE - read_index);
  181. if (cpy_log_size == OUTPUT_BUF_SIZE - read_index) {
  182. cpy_log_size += elog_cpyln(log + cpy_log_size, log_buf, size - cpy_log_size);
  183. read_index += cpy_log_size - OUTPUT_BUF_SIZE;
  184. } else {
  185. read_index += cpy_log_size;
  186. }
  187. }
  188. if (used == cpy_log_size) {
  189. buf_is_empty = true;
  190. }
  191. if (cpy_log_size) {
  192. buf_is_full = false;
  193. }
  194. __exit:
  195. /* lock output */
  196. elog_output_unlock();
  197. return cpy_log_size;
  198. }
  199. #else
  200. /**
  201. * get log from asynchronous output ring buffer
  202. *
  203. * @param log get log buffer
  204. * @param size log size
  205. *
  206. * @return get log size, the log size is less than ring buffer used size
  207. */
  208. size_t elog_async_get_log(char *log, size_t size) {
  209. size_t used = 0;
  210. /* lock output */
  211. elog_output_lock();
  212. used = elog_async_get_buf_used();
  213. /* no log */
  214. if (!used || !size) {
  215. size = 0;
  216. goto __exit;
  217. }
  218. /* less log */
  219. if (used <= size) {
  220. size = used;
  221. buf_is_empty = true;
  222. }
  223. if (read_index + size < OUTPUT_BUF_SIZE) {
  224. memcpy(log, log_buf + read_index, size);
  225. read_index += size;
  226. } else {
  227. memcpy(log, log_buf + read_index, OUTPUT_BUF_SIZE - read_index);
  228. memcpy(log + OUTPUT_BUF_SIZE - read_index, log_buf,
  229. size - (OUTPUT_BUF_SIZE - read_index));
  230. read_index += size - OUTPUT_BUF_SIZE;
  231. }
  232. buf_is_full = false;
  233. __exit:
  234. /* lock output */
  235. elog_output_unlock();
  236. return size;
  237. }
  238. #endif /* ELOG_ASYNC_LINE_OUTPUT */
  239. void elog_async_output(uint8_t level, const char *log, size_t size) {
  240. /* this function must be implement by user when ELOG_ASYNC_OUTPUT_USING_PTHREAD is not defined */
  241. extern void elog_async_output_notice(void);
  242. size_t put_size;
  243. if (is_enabled) {
  244. if (level >= OUTPUT_LVL) {
  245. put_size = async_put_log(log, size);
  246. /* notify output log thread */
  247. if (put_size > 0) {
  248. elog_async_output_notice();
  249. }
  250. } else {
  251. elog_port_output(log, size);
  252. }
  253. } else {
  254. elog_port_output(log, size);
  255. }
  256. }
  257. #ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
  258. void elog_async_output_notice(void) {
  259. sem_post(&output_notice);
  260. }
  261. static void *async_output(void *arg) {
  262. size_t get_log_size = 0;
  263. static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE];
  264. while(thread_running) {
  265. /* waiting log */
  266. sem_wait(&output_notice);
  267. /* polling gets and outputs the log */
  268. while(true) {
  269. #ifdef ELOG_ASYNC_LINE_OUTPUT
  270. get_log_size = elog_async_get_line_log(poll_get_buf, ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE);
  271. #else
  272. get_log_size = elog_async_get_log(poll_get_buf, ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE);
  273. #endif
  274. if (get_log_size) {
  275. elog_port_output(poll_get_buf, get_log_size);
  276. } else {
  277. break;
  278. }
  279. }
  280. }
  281. return NULL;
  282. }
  283. #endif
  284. /**
  285. * enable or disable asynchronous output mode
  286. * the log will be output directly when mode is disabled
  287. *
  288. * @param enabled true: enabled, false: disabled
  289. */
  290. void elog_async_enabled(bool enabled) {
  291. is_enabled = enabled;
  292. }
  293. /**
  294. * asynchronous output mode initialize
  295. *
  296. * @return result
  297. */
  298. ElogErrCode elog_async_init(void) {
  299. ElogErrCode result = ELOG_NO_ERR;
  300. if (init_ok) {
  301. return result;
  302. }
  303. #ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
  304. pthread_attr_t thread_attr;
  305. struct sched_param thread_sched_param;
  306. sem_init(&output_notice, 0, 0);
  307. thread_running = true;
  308. pthread_attr_init(&thread_attr);
  309. //pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
  310. pthread_attr_setstacksize(&thread_attr, ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE);
  311. pthread_attr_setschedpolicy(&thread_attr, SCHED_RR);
  312. thread_sched_param.sched_priority = ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY;
  313. pthread_attr_setschedparam(&thread_attr, &thread_sched_param);
  314. pthread_create(&async_output_thread, &thread_attr, async_output, NULL);
  315. pthread_attr_destroy(&thread_attr);
  316. #endif
  317. init_ok = true;
  318. return result;
  319. }
  320. /**
  321. * asynchronous output mode deinitialize
  322. *
  323. */
  324. void elog_async_deinit(void) {
  325. if (!init_ok) {
  326. return ;
  327. }
  328. #ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
  329. thread_running = false;
  330. elog_async_output_notice();
  331. pthread_join(async_output_thread, NULL);
  332. sem_destroy(&output_notice);
  333. #endif
  334. init_ok = false;
  335. }
  336. #endif /* ELOG_ASYNC_OUTPUT_ENABLE */