1
0

syslog.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-09-07 armink the first version
  9. */
  10. #include <stdarg.h>
  11. #include <ulog.h>
  12. #include <rthw.h>
  13. #include "syslog.h"
  14. #ifdef ULOG_OUTPUT_FLOAT
  15. #include <stdio.h>
  16. #endif
  17. /*
  18. * reference:
  19. * http://pubs.opengroup.org/onlinepubs/7908799/xsh/syslog.h.html
  20. * https://www.gnu.org/software/libc/manual/html_node/Submitting-Syslog-Messages.html
  21. * http://man7.org/linux/man-pages/man3/syslog.3.html
  22. */
  23. #ifdef ULOG_USING_SYSLOG
  24. #ifndef ULOG_SYSLOG_IDENT_MAX_LEN
  25. #define ULOG_SYSLOG_IDENT_MAX_LEN ULOG_FILTER_TAG_MAX_LEN
  26. #endif
  27. static char local_ident[ULOG_SYSLOG_IDENT_MAX_LEN + 1];
  28. static int local_facility = LOG_USER;
  29. static int local_option = LOG_USER;
  30. static rt_bool_t is_open = RT_FALSE;
  31. /**
  32. * open connection to syslog
  33. *
  34. * @param ident is an arbitrary identification string which future syslog invocations will prefix to each message.
  35. * @param option is not using on ulog.
  36. * @param facility is the default facility code for this connection.
  37. */
  38. void openlog(const char *ident, int option, int facility)
  39. {
  40. rt_base_t level;
  41. ulog_init();
  42. level = rt_hw_interrupt_disable();
  43. rt_memset(local_ident, 0, sizeof(local_ident));
  44. if (ident)
  45. {
  46. rt_strncpy(local_ident, ident, ULOG_SYSLOG_IDENT_MAX_LEN);
  47. }
  48. else
  49. {
  50. rt_strncpy(local_ident, "rtt", ULOG_SYSLOG_IDENT_MAX_LEN);
  51. }
  52. local_option = option;
  53. if (facility)
  54. {
  55. local_facility = facility;
  56. }
  57. else
  58. {
  59. /* default facility is LOG_USER */
  60. local_facility = LOG_USER;
  61. }
  62. /* output all level log */
  63. setlogmask(LOG_UPTO(LOG_DEBUG));
  64. is_open = RT_TRUE;
  65. rt_hw_interrupt_enable(level);
  66. }
  67. /**
  68. * This is functionally identical to syslog.
  69. *
  70. * @param priority log priority, can be generated by the macro LOG_MAKEPRI
  71. * @param format log format
  72. * @param args log arguments
  73. */
  74. void vsyslog(int priority, const char *format, va_list args)
  75. {
  76. if (LOG_FAC(priority) == 0)
  77. {
  78. /* using local facility */
  79. priority |= local_facility;
  80. }
  81. ulog_voutput(priority, local_ident, format, args);
  82. }
  83. /**
  84. * generates a log message
  85. *
  86. * @param priority log priority, can be generated by the macro LOG_MAKEPRI
  87. * @param format log format, like printf()
  88. */
  89. void syslog(int priority, const char *format, ...)
  90. {
  91. va_list args;
  92. if (!is_open)
  93. {
  94. openlog(0, 0, 0);
  95. }
  96. /* args point to the first variable parameter */
  97. va_start(args, format);
  98. vsyslog(priority, format, args);
  99. va_end(args);
  100. }
  101. /**
  102. * close the syslog
  103. */
  104. void closelog(void)
  105. {
  106. ulog_deinit();
  107. is_open = RT_FALSE;
  108. }
  109. /**
  110. * set log priority mask
  111. *
  112. * @param mask The log priority mask which generate by macro LOG_MASK and LOG_UPTO.
  113. *
  114. * @return This function returns the previous log priority mask.
  115. */
  116. int setlogmask(int mask)
  117. {
  118. static int old_mask = 0;
  119. int return_mask = old_mask;
  120. ulog_tag_lvl_filter_set(local_ident, mask);
  121. old_mask = mask;
  122. return return_mask;
  123. }
  124. static const char *get_month_str(uint8_t month)
  125. {
  126. switch(month)
  127. {
  128. case 1: return "Jan";
  129. case 2: return "Feb";
  130. case 3: return "Mar";
  131. case 4: return "Apr";
  132. case 5: return "May";
  133. case 6: return "June";
  134. case 7: return "July";
  135. case 8: return "Aug";
  136. case 9: return "Sept";
  137. case 10: return "Oct";
  138. case 11: return "Nov";
  139. case 12: return "Dec";
  140. default: return "Unknown";
  141. }
  142. }
  143. RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, const char *format, va_list args)
  144. {
  145. extern size_t ulog_strcpy(size_t cur_len, char *dst, const char *src);
  146. rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
  147. int fmt_result;
  148. RT_ASSERT(log_buf);
  149. RT_ASSERT(LOG_PRI(level) <= LOG_DEBUG);
  150. RT_ASSERT(tag);
  151. RT_ASSERT(format);
  152. /* add time and priority (level) info */
  153. {
  154. time_t now = time(RT_NULL);
  155. struct tm *tm, tm_tmp;
  156. tm = gmtime_r(&now, &tm_tmp);
  157. #ifdef ULOG_OUTPUT_LEVEL
  158. rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "<%d>%s%3d %02d:%02d:%02d", level,
  159. get_month_str(tm->tm_mon + 1), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, rt_tick_get() % 1000);
  160. #else
  161. rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%s%3d %02d:%02d:%02d",
  162. get_month_str(tm->tm_mon + 1), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, rt_tick_get() % 1000);
  163. #endif /* ULOG_OUTPUT_LEVEL */
  164. log_len += rt_strlen(log_buf + log_len);
  165. }
  166. #ifdef ULOG_OUTPUT_TAG
  167. /* add identification (tag) info */
  168. {
  169. log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
  170. log_len += ulog_strcpy(log_len, log_buf + log_len, tag);
  171. }
  172. #endif /* ULOG_OUTPUT_TAG */
  173. #ifdef ULOG_OUTPUT_THREAD_NAME
  174. /* add thread info */
  175. {
  176. log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
  177. /* is not in interrupt context */
  178. if (rt_interrupt_get_nest() == 0)
  179. {
  180. log_len += ulog_strcpy(log_len, log_buf + log_len, rt_thread_self()->name);
  181. }
  182. else
  183. {
  184. log_len += ulog_strcpy(log_len, log_buf + log_len, "ISR");
  185. }
  186. }
  187. #endif /* ULOG_OUTPUT_THREAD_NAME */
  188. log_len += ulog_strcpy(log_len, log_buf + log_len, ": ");
  189. #ifdef ULOG_OUTPUT_FLOAT
  190. fmt_result = vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
  191. #else
  192. fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
  193. #endif /* ULOG_OUTPUT_FLOAT */
  194. /* calculate log length */
  195. if ((log_len + fmt_result <= ULOG_LINE_BUF_SIZE) && (fmt_result > -1))
  196. {
  197. log_len += fmt_result;
  198. }
  199. else
  200. {
  201. /* using max length */
  202. log_len = ULOG_LINE_BUF_SIZE;
  203. }
  204. /* overflow check and reserve some space for newline sign */
  205. if (log_len + newline_len > ULOG_LINE_BUF_SIZE)
  206. {
  207. /* using max length */
  208. log_len = ULOG_LINE_BUF_SIZE;
  209. /* reserve some space for newline sign */
  210. log_len -= newline_len;
  211. }
  212. /* package newline sign */
  213. log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
  214. return log_len;
  215. }
  216. #endif /* ULOG_USING_SYSLOG */