elog_port.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This file is part of the EasyLogger Library.
  3. *
  4. * Copyright (c) 2015, 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: Portable interface for linux.
  26. * Created on: 2015-04-28
  27. */
  28. #include <elog.h>
  29. #include <stdio.h>
  30. #include <pthread.h>
  31. #include <unistd.h>
  32. #include <time.h>
  33. #include <sys/syscall.h>
  34. #ifdef ELOG_FILE_ENABLE
  35. #include <elog_file.h>
  36. #endif
  37. static pthread_mutex_t output_lock;
  38. /**
  39. * EasyLogger port initialize
  40. *
  41. * @return result
  42. */
  43. int elog_port_init(void) {
  44. int result = 0;
  45. pthread_mutex_init(&output_lock, NULL);
  46. #ifdef ELOG_FILE_ENABLE
  47. elog_file_init();
  48. #endif
  49. return result;
  50. }
  51. /**
  52. * EasyLogger port deinitialize
  53. *
  54. */
  55. void elog_port_deinit(void) {
  56. #ifdef ELOG_FILE_ENABLE
  57. elog_file_deinit();
  58. #endif
  59. pthread_mutex_destroy(&output_lock);
  60. }
  61. /**
  62. * output log port interface
  63. *
  64. * @param log output of log
  65. * @param size log size
  66. */
  67. void elog_port_output(const char *log, size_t size) {
  68. /* output to terminal */
  69. #ifdef ELOG_TERMINAL_ENABLE
  70. printf("%.*s", (int)size, log);
  71. #endif
  72. #ifdef ELOG_FILE_ENABLE
  73. /* write the file */
  74. elog_file_write(log, size);
  75. #endif
  76. }
  77. /**
  78. * output lock
  79. */
  80. void elog_port_output_lock(void) {
  81. pthread_mutex_lock(&output_lock);
  82. }
  83. /**
  84. * output unlock
  85. */
  86. void elog_port_output_unlock(void) {
  87. pthread_mutex_unlock(&output_lock);
  88. }
  89. /**
  90. * get current time interface
  91. *
  92. * @return current time
  93. */
  94. const char *elog_port_get_time(void) {
  95. static char cur_system_time[24] = { 0 };
  96. time_t cur_t;
  97. struct tm cur_tm;
  98. time(&cur_t);
  99. localtime_r(&cur_t, &cur_tm);
  100. strftime(cur_system_time, sizeof(cur_system_time), "%Y-%m-%d %T", &cur_tm);
  101. return cur_system_time;
  102. }
  103. /**
  104. * get current process name interface
  105. *
  106. * @return current process name
  107. */
  108. const char *elog_port_get_p_info(void) {
  109. static char cur_process_info[10] = { 0 };
  110. snprintf(cur_process_info, 10, "pid:%04d", getpid());
  111. return cur_process_info;
  112. }
  113. /**
  114. * get current thread name interface
  115. *
  116. * @return current thread name
  117. */
  118. const char *elog_port_get_t_info(void) {
  119. static char cur_thread_info[16] = { 0 };
  120. snprintf(cur_thread_info, 16, "tid:%04ld", syscall(SYS_gettid));
  121. return cur_thread_info;
  122. }