elog_buf.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2016, 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 buffered output.
  26. * Created on: 2016-11-09
  27. */
  28. #include <elog.h>
  29. #include <string.h>
  30. #ifdef ELOG_BUF_OUTPUT_ENABLE
  31. #if !defined(ELOG_BUF_OUTPUT_BUF_SIZE)
  32. #error "Please configure buffer size for buffered output mode (in elog_cfg.h)"
  33. #endif
  34. /* buffered output mode's buffer */
  35. static char log_buf[ELOG_BUF_OUTPUT_BUF_SIZE] = { 0 };
  36. /* log buffer current write size */
  37. static size_t buf_write_size = 0;
  38. /* buffered output mode enabled flag */
  39. static bool is_enabled = false;
  40. extern void elog_port_output(const char *log, size_t size);
  41. extern void elog_output_lock(void);
  42. extern void elog_output_unlock(void);
  43. /**
  44. * output buffered logs when buffer is full
  45. *
  46. * @param log will be buffered line's log
  47. * @param size log size
  48. */
  49. void elog_buf_output(const char *log, size_t size) {
  50. size_t write_size = 0, write_index = 0;
  51. if (!is_enabled) {
  52. elog_port_output(log, size);
  53. return;
  54. }
  55. while (true) {
  56. if (buf_write_size + size > ELOG_BUF_OUTPUT_BUF_SIZE) {
  57. write_size = ELOG_BUF_OUTPUT_BUF_SIZE - buf_write_size;
  58. memcpy(log_buf + buf_write_size, log + write_index, write_size);
  59. write_index += write_size;
  60. size -= write_size;
  61. buf_write_size += write_size;
  62. /* output log */
  63. elog_port_output(log_buf, buf_write_size);
  64. /* reset write index */
  65. buf_write_size = 0;
  66. } else {
  67. memcpy(log_buf + buf_write_size, log + write_index, size);
  68. buf_write_size += size;
  69. break;
  70. }
  71. }
  72. }
  73. /**
  74. * flush all buffered logs to output device
  75. */
  76. void elog_flush(void) {
  77. if (buf_write_size == 0)
  78. return;
  79. /* lock output */
  80. elog_output_lock();
  81. /* output log */
  82. elog_port_output(log_buf, buf_write_size);
  83. /* reset write index */
  84. buf_write_size = 0;
  85. /* unlock output */
  86. elog_output_unlock();
  87. }
  88. /**
  89. * enable or disable buffered output mode
  90. * the log will be output directly when mode is disabled
  91. *
  92. * @param enabled true: enabled, false: disabled
  93. */
  94. void elog_buf_enabled(bool enabled) {
  95. is_enabled = enabled;
  96. }
  97. #endif /* ELOG_BUF_OUTPUT_ENABLE */