syslog.c 6.3 KB

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