ebt_ulog based arpwatch
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.

102 lines
2.7KB

  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 <time.h>
  22. #include <stdarg.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include "logging.h"
  26. #define BUFSIZE 8192
  27. static FILE *log_fd = NULL;
  28. static int log_prio = LOG_EVERYTIME;
  29. static char *buffer = NULL;
  30. int log_print(int prio, const char *fmt, ...)
  31. {
  32. va_list az;
  33. int len = 0, retval;
  34. if (prio < log_prio)
  35. return 0;
  36. if (buffer == NULL) {
  37. buffer = malloc(BUFSIZE);
  38. if (buffer == NULL) {
  39. fprintf(stderr, "log_print(): out of memory\n");
  40. return -1;
  41. }
  42. }
  43. if (log_fd != NULL) {
  44. time_t tzgr;
  45. time(&tzgr);
  46. len += strftime(buffer, BUFSIZE, "%b %d %H:%M:%S :", localtime(&tzgr));
  47. }
  48. va_start(az, fmt);
  49. len += vsnprintf(buffer + len, BUFSIZE - len, fmt, az);
  50. va_end(az);
  51. if (len < 0 || len >= BUFSIZE) {
  52. errno = 0;
  53. return log_print(LOG_ERROR, "log_print: arguments too long");
  54. }
  55. if (errno) {
  56. len += snprintf(buffer + len, BUFSIZE - len, ": %s", strerror(errno));
  57. errno = 0;
  58. }
  59. retval = fprintf((log_fd ? log_fd : stderr), "%s\n", buffer);
  60. fflush(log_fd);
  61. return retval;
  62. }
  63. void log_close(void)
  64. {
  65. if (buffer)
  66. free(buffer);
  67. if (log_fd)
  68. fclose(log_fd);
  69. }
  70. int log_init(const char *logfile)
  71. {
  72. if (log_fd != NULL)
  73. log_close();
  74. log_fd = fopen(logfile, "a");
  75. if (log_fd == NULL) {
  76. fprintf(stderr, "log_init(): can not open logfile");
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. void log_setprio(int prio)
  82. {
  83. log_prio = prio;
  84. }