smarty template cache syncing daemon (PoC)
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.

104 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 char *buffer = NULL;
  30. void log_print(int prio, const char *fmt, ...)
  31. {
  32. va_list az;
  33. int len;
  34. if (buffer == NULL) {
  35. buffer = malloc(BUFSIZE);
  36. if (buffer == NULL) {
  37. fprintf(stderr, "log_print: out of memory\nBailing out!\n");
  38. exit(-1);
  39. }
  40. }
  41. va_start(az, fmt);
  42. len = vsnprintf(buffer, BUFSIZE, fmt, az);
  43. va_end(az);
  44. if (len < 0 || len >= BUFSIZE) {
  45. log_print(LOG_ERROR, "log_print: arguments too long");
  46. errno = 0;
  47. return;
  48. }
  49. if (errno) {
  50. strncpy(buffer + len, ": ", BUFSIZE - len);
  51. len += 2;
  52. strncpy(buffer + len, strerror(errno), BUFSIZE - len);
  53. }
  54. if (log_fd) {
  55. char tbuf[64];
  56. time_t tzgr;
  57. time(&tzgr);
  58. strftime(tbuf, sizeof(tbuf), "%b %d %H:%M:%S :", localtime(&tzgr));
  59. fprintf(log_fd, "%s %s\n", tbuf, buffer);
  60. fflush(log_fd);
  61. } else {
  62. fprintf(stderr, "%s\n", buffer);
  63. }
  64. errno = 0;
  65. }
  66. static void log_close(void)
  67. {
  68. if (buffer)
  69. free(buffer);
  70. fclose(log_fd);
  71. }
  72. int log_init(char *logfile)
  73. {
  74. log_fd = fopen(logfile, "a");
  75. if (log_fd == NULL) {
  76. log_print(LOG_ERROR, "log_open('%s'): %s", logfile);
  77. return 0;
  78. }
  79. if (atexit(log_close) != 0) {
  80. log_print(LOG_ERROR, "log_open(): atexit()");
  81. return 0;
  82. }
  83. log_print(LOG_EVERYTIME, "==========================");
  84. return 1;
  85. }