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.

log.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code 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.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <syslog.h>
  22. #include <errno.h>
  23. #ifdef DEBUG
  24. #include <sys/types.h>
  25. #include <signal.h>
  26. #include <unistd.h>
  27. #endif
  28. #include "log.h"
  29. char *__argv0;
  30. int dbg_lvl;
  31. int log_to_stderr;
  32. static int log_facility = LOG_DAEMON;
  33. int log_file_opened = 0;
  34. FILE *log_file, *log_fd;
  35. void
  36. log_init(char *prog, int dbg, int log_stderr)
  37. {
  38. __argv0 = prog;
  39. dbg_lvl = dbg;
  40. log_to_stderr = log_stderr;
  41. if (log_stderr)
  42. log_fd = stderr;
  43. if (!log_file_opened)
  44. log_file = 0;
  45. if (!log_to_stderr)
  46. openlog(__argv0, dbg ? LOG_PID : 0, log_facility);
  47. }
  48. /*
  49. * log_to_file
  50. *
  51. * If `filename' is not null, it is opened and set as the logfile.
  52. * When `filename' is null, it just updates the `log_fd' global variable.
  53. *
  54. * On errors it returns -1;
  55. */
  56. int
  57. log_to_file(char *filename)
  58. {
  59. if (!filename) {
  60. if (log_file)
  61. log_fd = log_file;
  62. else
  63. return -1;
  64. return 0;
  65. }
  66. if (!(log_file = fopen(filename, "w"))) {
  67. log_fd = stderr;
  68. error("Cannot open the \"%s\" logfile: %s",
  69. filename, strerror(errno));
  70. return -1;
  71. }
  72. log_fd = log_file;
  73. log_file_opened = 1;
  74. return 0;
  75. }
  76. void
  77. close_log_file(void)
  78. {
  79. if (log_file) {
  80. fflush(log_file);
  81. fclose(log_file);
  82. }
  83. }
  84. /* Life is fatal! */
  85. void
  86. fatal(const char *fmt, ...)
  87. {
  88. char str[strlen(fmt) + 3];
  89. va_list args;
  90. if (fmt) {
  91. str[0] = '!';
  92. str[1] = ' ';
  93. strncpy(str + 2, fmt, strlen(fmt));
  94. str[strlen(fmt) + 2] = 0;
  95. va_start(args, fmt);
  96. print_log(LOG_CRIT, str, args);
  97. va_end(args);
  98. }
  99. /** Flush the stream if we want to read something */
  100. if (log_to_stderr || log_file)
  101. fflush(log_fd);
  102. if (log_file)
  103. close_log_file();
  104. /**/
  105. #ifdef DEBUG
  106. /* Useful to catch the error in gdb */
  107. kill(getpid(), SIGSEGV);
  108. #endif
  109. exit(1);
  110. }
  111. /* Misc errors */
  112. void
  113. error(const char *fmt, ...)
  114. {
  115. char str[strlen(fmt) + 3];
  116. va_list args;
  117. str[0] = '*';
  118. str[1] = ' ';
  119. strncpy(str + 2, fmt, strlen(fmt));
  120. str[strlen(fmt) + 2] = 0;
  121. va_start(args, fmt);
  122. print_log(LOG_ERR, str, args);
  123. va_end(args);
  124. }
  125. /* Let's give some news */
  126. void
  127. loginfo(const char *fmt, ...)
  128. {
  129. char str[strlen(fmt) + 3];
  130. va_list args;
  131. str[0] = '+';
  132. str[1] = ' ';
  133. strncpy(str + 2, fmt, strlen(fmt));
  134. str[strlen(fmt) + 2] = 0;
  135. va_start(args, fmt);
  136. print_log(LOG_INFO, str, args);
  137. va_end(args);
  138. }
  139. /* "Debugging is twice as hard as writing the code in the first place.
  140. * Therefore, if you write the code as cleverly as possible, you are,
  141. * by definition, not smart enough to debug it." - Brian W. Kernighan
  142. * Damn!
  143. */
  144. void
  145. debug(int lvl, const char *fmt, ...)
  146. {
  147. char str[strlen(fmt) + 3];
  148. va_list args;
  149. if (lvl <= dbg_lvl) {
  150. str[0] = '#';
  151. str[1] = ' ';
  152. strncpy(str + 2, fmt, strlen(fmt));
  153. str[strlen(fmt) + 2] = 0;
  154. va_start(args, fmt);
  155. print_log(LOG_DEBUG, str, args);
  156. va_end(args);
  157. }
  158. }
  159. void
  160. print_log(int level, const char *fmt, va_list args)
  161. {
  162. if (log_to_stderr || log_file) {
  163. vfprintf(log_fd, fmt, args);
  164. fprintf(log_fd, "\n");
  165. } else
  166. vsyslog(level | log_facility, fmt, args);
  167. }