syslog.c 6.3 KB

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