QNAP-TS419p system daemon
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.

115 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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_file = 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, "%s(): out of memory\n", __FUNCTION__);
  41. return -1;
  42. }
  43. }
  44. if (log_file != 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, "%s: arguments too long", __FUNCTION__);
  55. }
  56. if (errno) {
  57. len += snprintf(buffer + len, BUFSIZE - len, ": %s", strerror(errno));
  58. errno = 0;
  59. }
  60. retval = fprintf((log_file ? log_file : stderr), "%s\n", buffer);
  61. fflush(log_file);
  62. return retval;
  63. }
  64. void log_close(void)
  65. {
  66. if (buffer) {
  67. free(buffer);
  68. buffer = NULL;
  69. }
  70. if (log_file) {
  71. fclose(log_file);
  72. log_file = NULL;
  73. }
  74. }
  75. int log_init(const char *logfile)
  76. {
  77. if (log_file != NULL)
  78. log_close();
  79. log_file = fopen(logfile, "a");
  80. if (log_file == NULL) {
  81. fprintf(stderr, "%s(): can not open logfile", __FUNCTION__);
  82. return -1;
  83. }
  84. if (fcntl(fileno(log_file), F_SETFD, FD_CLOEXEC) < 0) {
  85. fprintf(stderr, "%s(): fcntl(FD_CLOEXEC)", __FUNCTION__);
  86. fclose(log_file);
  87. return -1;
  88. }
  89. log_prio = LOG_EVERYTIME;
  90. return 0;
  91. }
  92. void log_setprio(int prio)
  93. {
  94. log_prio = prio;
  95. }