elog_async.c 10 KB

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