协能can协议
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

elog_port.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <pthread.h>
  30. #include <stddef.h>
  31. #include <stdio.h>
  32. #include <sys/time.h>
  33. #include <time.h>
  34. #include <unistd.h>
  35. #ifdef ELOG_FILE_ENABLE
  36. #include <elog_file.h>
  37. #endif
  38. static pthread_mutex_t output_lock;
  39. /**
  40. * EasyLogger port initialize
  41. *
  42. * @return result
  43. */
  44. ElogErrCode elog_port_init(void)
  45. {
  46. ElogErrCode result = ELOG_NO_ERR;
  47. pthread_mutex_init(&output_lock, NULL);
  48. #ifdef ELOG_FILE_ENABLE
  49. elog_file_init();
  50. #endif
  51. return result;
  52. }
  53. /**
  54. * output log port interface
  55. *
  56. * @param log output of log
  57. * @param size log size
  58. */
  59. void elog_port_output(const char *log, size_t size)
  60. {
  61. /* output to terminal */
  62. printf("%.*s", (int) size, log);
  63. #ifdef ELOG_FILE_ENABLE
  64. /* write the file */
  65. elog_file_write(log, size);
  66. #endif
  67. }
  68. /**
  69. * output lock
  70. */
  71. void elog_port_output_lock(void)
  72. {
  73. pthread_mutex_lock(&output_lock);
  74. }
  75. /**
  76. * output unlock
  77. */
  78. void elog_port_output_unlock(void)
  79. {
  80. pthread_mutex_unlock(&output_lock);
  81. }
  82. /**
  83. * get current time interface
  84. *
  85. * @return current time
  86. */
  87. const char *elog_port_get_time(void)
  88. {
  89. static char cur_system_time[30] =
  90. { 0 };
  91. struct timeval tv;
  92. struct timezone tz;
  93. struct tm *p;
  94. gettimeofday(&tv, &tz);
  95. p = localtime(&tv.tv_sec);
  96. if(p == NULL) {
  97. return "";
  98. }
  99. snprintf(cur_system_time, 26, "%04d-%02d-%02d %02d:%02d:%02d.%06ld", 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday,
  100. p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
  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. {
  110. static char cur_process_info[10] =
  111. { 0 };
  112. snprintf(cur_process_info, 10, "pid:%04d", getpid());
  113. return cur_process_info;
  114. }
  115. /**
  116. * get current thread name interface
  117. *
  118. * @return current thread name
  119. */
  120. const char *elog_port_get_t_info(void)
  121. {
  122. static char cur_thread_info[10] =
  123. { 0 };
  124. snprintf(cur_thread_info, 10, "tid:%04ld", pthread_self());
  125. return cur_thread_info;
  126. }