log.c 987 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "log.h"
  2. #include <stdarg.h>
  3. #include <time.h>
  4. FILE *log_file;
  5. int log_verbose = 0;
  6. static void log_err_args(char *tag, char *fmt, va_list args) {
  7. FILE *err_file = stderr;
  8. if (log_file != stdout)
  9. err_file = log_file;
  10. fprintf(err_file, "(%lu) %s: ", (unsigned long)time(NULL), tag);
  11. vfprintf(err_file, fmt, args);
  12. }
  13. void log_err(char *tag, char *fmt, ...) {
  14. va_list args;
  15. va_start (args, fmt);
  16. log_err_args(tag, fmt, args);
  17. va_end (args);
  18. }
  19. static void log_out_args(char *tag, char *fmt, va_list args) {
  20. fprintf(log_file, "(%lu) %s: ", (unsigned long)time(NULL), tag);
  21. vfprintf(log_file, fmt, args);
  22. }
  23. void log_out(char *tag, char *fmt, ...) {
  24. va_list args;
  25. va_start (args, fmt);
  26. log_out_args(tag, fmt, args);
  27. va_end (args);
  28. }
  29. void log_out_v(char *tag, char *fmt, ...) {
  30. if (!log_verbose)
  31. return;
  32. va_list args;
  33. va_start (args, fmt);
  34. log_out_args(tag, fmt, args);
  35. va_end (args);
  36. }