A small USV for ALIX boards with i2c support
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.

logging.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /***************************************************************************
  2. * Copyright (C) 07/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <stdarg.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include "logging.h"
  27. #define BUFSIZE 8192
  28. static FILE *log_fd = NULL;
  29. static int log_prio = LOG_EVERYTIME;
  30. static char *buffer = NULL;
  31. int log_print(int prio, const char *fmt, ...)
  32. {
  33. va_list az;
  34. int len = 0, retval;
  35. if (prio < log_prio)
  36. return 0;
  37. if (buffer == NULL) {
  38. buffer = malloc(BUFSIZE);
  39. if (buffer == NULL) {
  40. fprintf(stderr, "log_print(): out of memory\n");
  41. return -1;
  42. }
  43. }
  44. if (log_fd != NULL) {
  45. time_t tzgr;
  46. time(&tzgr);
  47. len += strftime(buffer, BUFSIZE, "%b %d %H:%M:%S :", localtime(&tzgr));
  48. }
  49. va_start(az, fmt);
  50. len += vsnprintf(buffer + len, BUFSIZE - len, fmt, az);
  51. va_end(az);
  52. if (len < 0 || len >= BUFSIZE) {
  53. errno = 0;
  54. return log_print(LOG_ERROR, "log_print: arguments too long");
  55. }
  56. if (errno) {
  57. len += snprintf(buffer + len, BUFSIZE - len, ": %s", strerror(errno));
  58. errno = 0;
  59. }
  60. retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer);
  61. fflush(log_fd);
  62. return retval;
  63. }
  64. void log_close(void)
  65. {
  66. if (buffer) {
  67. free(buffer);
  68. buffer = NULL;
  69. }
  70. if (log_fd) {
  71. fclose(log_fd);
  72. log_fd = NULL;
  73. }
  74. }
  75. int log_init(const char *logfile)
  76. {
  77. if (log_fd != NULL)
  78. log_close();
  79. log_fd = fopen(logfile, "a");
  80. if (log_fd == NULL) {
  81. fprintf(stderr, "log_init(): can not open logfile");
  82. return -1;
  83. }
  84. if (fcntl(fileno(log_fd), F_SETFD, FD_CLOEXEC) < 0) {
  85. fprintf(stderr, "log_init(): fcntl(FD_CLOEXEC)");
  86. return -1;
  87. }
  88. log_prio = LOG_EVERYTIME;
  89. return 0;
  90. }
  91. void log_setprio(int prio)
  92. {
  93. log_prio = prio;
  94. }