logging.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* logging.c: Logging code for masterserver. */
  2. /* This file is part of masterserver.
  3. *
  4. * masterserver is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * masterserver 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 masterserver; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * vim:sw=4:ts=4
  20. */
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include "logging.h"
  31. #define DEFAULT_LOGFILE "/var/log/masterserver.log"
  32. #define DEFAULT_FILEMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  33. #define DEFAULT_FILEFLAGS (O_CREAT | O_RDWR)
  34. #define DEFAULT_PROGNAME "masterserver"
  35. char *default_logfile = NULL; // can be set from main(); fallback to DEFAULT_LOGFILE
  36. char *default_progname = NULL;
  37. int _lfd = 0; // global file descriptor where debug messages go
  38. FILE *_lfp = NULL; // global file pointer where debug messages go
  39. int _log_level = 0;
  40. int log_init(char *filename, char *progname)
  41. {
  42. default_progname = progname != NULL ? strdup(progname) : strdup(DEFAULT_PROGNAME);
  43. if (filename != NULL) {
  44. default_logfile = strdup(filename);
  45. _lfd = open(default_logfile, DEFAULT_FILEFLAGS, DEFAULT_FILEMODE);
  46. if (_lfd == -1) {
  47. fprintf(stderr, "%s, %d: %s %s", __FILE__, __LINE__, default_logfile, strerror(errno));
  48. return -1;
  49. }
  50. _lfp = fdopen(_lfd, "a");
  51. if (_lfp == NULL) {
  52. fprintf(stderr, "%s, %d: %s", __FILE__, __LINE__, strerror(errno)); // redundant, maybe stderr is closed
  53. return -1;
  54. }
  55. } else {
  56. _lfp = stdout;
  57. }
  58. return 0;
  59. }
  60. void log_write(int log_level, char *subname, char *fmt, ...) {
  61. time_t t;
  62. struct tm *tm_now;
  63. va_list tmp;
  64. t = time(NULL);
  65. tm_now = localtime(&t);
  66. fprintf(_lfp, "[%.2d.%.2d.%d %.2d:%.2d:%.2d] %s:%s ",
  67. tm_now->tm_mday, tm_now->tm_mon + 1, tm_now->tm_year + 1900,
  68. tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec,
  69. default_progname, subname);
  70. switch (log_level) {
  71. case LOG_LEVEL_INFO:
  72. fprintf(_lfp, "info: ");
  73. break;
  74. case LOG_LEVEL_WARNING:
  75. fprintf(_lfp, "warning: ");
  76. break;
  77. case LOG_LEVEL_ERROR:
  78. fprintf(_lfp, "Error: ");
  79. break;
  80. case LOG_LEVEL_DEBUG:
  81. fprintf(_lfp, "Debug: ");
  82. break;
  83. }
  84. va_start(tmp, fmt);
  85. vfprintf(_lfp, fmt, tmp);
  86. va_end(tmp);
  87. fflush(_lfp); // write it to disk
  88. }
  89. void log_close(void)
  90. {
  91. if (close(_lfd) == -1) {
  92. fprintf(stderr, "%s, %d: %s", __FILE__, __LINE__, strerror(errno)); // redundant, maybe stderr is closed
  93. return;
  94. }
  95. }