Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

log.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. */
  19. #include "log.h"
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <errno.h>
  29. static const char *_log_level_colors[] = {
  30. "\x1b[1;5;91m", "\x1b[1;91m", "\x1b[91m", "\x1b[31m", "\x1b[33m", "\x1b[34m", "\x1b[32m", "\x1b[36m"
  31. };
  32. static const char *_log_level_names[] = {
  33. "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"
  34. };
  35. static uint8_t _log_quiet = 0;
  36. static uint8_t _log_level = LOG_DEBUG;
  37. static uint8_t _log_syslog = 0;
  38. static uint8_t _log_pipe = 0;
  39. static char *_log_pipe_file = NULL;
  40. static int _log_pipe_fd = -1;
  41. static FILE *_log_file_fp = NULL;
  42. void logSetQuiet(uint8_t enable)
  43. {
  44. _log_quiet = enable ? 1 : 0;
  45. }
  46. void logSetLevel(int level)
  47. {
  48. if (level < LOG_EMERG || level > LOG_DEBUG) {
  49. return;
  50. }
  51. _log_level = level;
  52. }
  53. void logSetSyslog(int options, int facility)
  54. {
  55. openlog(NULL, options, facility);
  56. _log_syslog = 1;
  57. }
  58. int logSetPipe(char *pipe_file)
  59. {
  60. if (pipe_file == NULL) {
  61. return -1;
  62. }
  63. _log_pipe_file = strdup(pipe_file);
  64. if (_log_pipe_file == NULL) {
  65. return -1;
  66. }
  67. int ret = mkfifo(_log_pipe_file, 0666);
  68. if (ret == 0) {
  69. _log_pipe = 1;
  70. }
  71. return ret;
  72. }
  73. int logSetFile(char *file)
  74. {
  75. if (file == NULL) {
  76. return -1;
  77. }
  78. _log_file_fp = fopen(file, "a");
  79. if (_log_file_fp == NULL) {
  80. return errno;
  81. }
  82. return 0;
  83. }
  84. void logClose(void)
  85. {
  86. if (_log_syslog) {
  87. closelog();
  88. _log_syslog = 0;
  89. }
  90. if (_log_pipe) {
  91. if (_log_pipe_fd > 0) {
  92. close(_log_pipe_fd);
  93. }
  94. /* remove the FIFO */
  95. unlink(_log_pipe_file);
  96. _log_pipe = 0;
  97. }
  98. if (_log_pipe_file != NULL) {
  99. free(_log_pipe_file);
  100. _log_pipe_file = NULL;
  101. }
  102. if (_log_file_fp != NULL) {
  103. fclose(_log_file_fp);
  104. _log_file_fp = NULL;
  105. }
  106. }
  107. void vlog(int level, const char *fmt, va_list args)
  108. {
  109. if (_log_level < level) {
  110. return;
  111. }
  112. if (!_log_quiet || _log_file_fp != NULL) {
  113. /* Get current time */
  114. time_t t = time(NULL);
  115. struct tm *lt = localtime(&t);
  116. char date[16];
  117. date[strftime(date, sizeof(date), "%b %d %H:%M:%S", lt)] = '\0';
  118. if (_log_file_fp != NULL) {
  119. fprintf(_log_file_fp, "%s %-5s ", date, _log_level_names[level]);
  120. vfprintf(_log_file_fp, fmt, args);
  121. }
  122. if (!_log_quiet) {
  123. #ifdef LOG_DISABLE_COLOR
  124. (void)_log_level_colors;
  125. fprintf(stderr, "%s %-5s ", date, _log_level_names[level]);
  126. vfprintf(stderr, fmt, args);
  127. #else
  128. fprintf(stderr, "%s %s%-5s\x1b[0m ", date, _log_level_colors[level], _log_level_names[level]);
  129. vfprintf(stderr, fmt, args);
  130. #endif
  131. }
  132. }
  133. if (_log_syslog) {
  134. vsyslog(level, fmt, args);
  135. }
  136. if (_log_pipe) {
  137. if (_log_pipe_fd < 0) {
  138. _log_pipe_fd = open(_log_pipe_file, O_WRONLY | O_NONBLOCK);
  139. }
  140. if (_log_pipe_fd > 0) {
  141. if (vdprintf(_log_pipe_fd, fmt, args) < 0) {
  142. close(_log_pipe_fd);
  143. _log_pipe_fd = -1;
  144. }
  145. }
  146. }
  147. }
  148. void
  149. #ifdef __GNUC__
  150. __attribute__((format(printf, 1, 2)))
  151. #endif
  152. logEmergency(const char *fmt, ...)
  153. {
  154. va_list args;
  155. va_start(args, fmt);
  156. vlog(LOG_EMERG, fmt, args);
  157. va_end(args);
  158. }
  159. void
  160. #ifdef __GNUC__
  161. __attribute__((format(printf, 1, 2)))
  162. #endif
  163. logAlert(const char *fmt, ...)
  164. {
  165. va_list args;
  166. va_start(args, fmt);
  167. vlog(LOG_ALERT, fmt, args);
  168. va_end(args);
  169. }
  170. void
  171. #ifdef __GNUC__
  172. __attribute__((format(printf, 1, 2)))
  173. #endif
  174. logCritical(const char *fmt, ...)
  175. {
  176. va_list args;
  177. va_start(args, fmt);
  178. vlog(LOG_CRIT, fmt, args);
  179. va_end(args);
  180. }
  181. void
  182. #ifdef __GNUC__
  183. __attribute__((format(printf, 1, 2)))
  184. #endif
  185. logError(const char *fmt, ...)
  186. {
  187. va_list args;
  188. va_start(args, fmt);
  189. vlog(LOG_ERR, fmt, args);
  190. va_end(args);
  191. }
  192. void
  193. #ifdef __GNUC__
  194. __attribute__((format(printf, 1, 2)))
  195. #endif
  196. logWarning(const char *fmt, ...)
  197. {
  198. va_list args;
  199. va_start(args, fmt);
  200. vlog(LOG_WARNING, fmt, args);
  201. va_end(args);
  202. }
  203. void
  204. #ifdef __GNUC__
  205. __attribute__((format(printf, 1, 2)))
  206. #endif
  207. logNotice(const char *fmt, ...)
  208. {
  209. va_list args;
  210. va_start(args, fmt);
  211. vlog(LOG_NOTICE, fmt, args);
  212. va_end(args);
  213. }
  214. void
  215. #ifdef __GNUC__
  216. __attribute__((format(printf, 1, 2)))
  217. #endif
  218. logInfo(const char *fmt, ...)
  219. {
  220. va_list args;
  221. va_start(args, fmt);
  222. vlog(LOG_INFO, fmt, args);
  223. va_end(args);
  224. }
  225. void
  226. #ifdef __GNUC__
  227. __attribute__((format(printf, 1, 2)))
  228. #endif
  229. logDebug(const char *fmt, ...)
  230. {
  231. va_list args;
  232. va_start(args, fmt);
  233. vlog(LOG_DEBUG, fmt, args);
  234. va_end(args);
  235. }