syslog.c 6.3 KB

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