ES-2024 reverter
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.

103 lines
2.8KB

  1. /***************************************************************************
  2. * Copyright (C) 06/2006 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; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <stdarg.h>
  24. #include <errno.h>
  25. #include <string.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. if (log_fd)
  69. fclose(log_fd);
  70. }
  71. int log_init(const char *logfile)
  72. {
  73. if (log_fd != NULL)
  74. log_close();
  75. log_fd = fopen(logfile, "a");
  76. if (log_fd == NULL) {
  77. fprintf(stderr, "log_init(): can not open logfile");
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. void log_setprio(int prio)
  83. {
  84. log_prio = prio;
  85. }