log.c 681 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "log.h"
  5. static int loglevel = 0;
  6. void
  7. init_log(int level)
  8. {
  9. loglevel = level;
  10. char *debuglevel = getenv("LIBMEGAPIXELS_DEBUG");
  11. if (debuglevel != NULL) {
  12. char *end;
  13. long env_level = strtol(debuglevel, &end, 10);
  14. loglevel = (int) env_level;
  15. }
  16. }
  17. void
  18. log_error(const char *fmt, ...)
  19. {
  20. va_list args;
  21. va_start(args, fmt);
  22. fprintf(stderr, "[libmegapixels] ");
  23. vfprintf(stderr, fmt, args);
  24. va_end(args);
  25. }
  26. void
  27. log_debug(const char *fmt, ...)
  28. {
  29. if (loglevel < 2) {
  30. return;
  31. }
  32. va_list args;
  33. va_start(args, fmt);
  34. fprintf(stderr, "[libmegapixels] ");
  35. vfprintf(stderr, fmt, args);
  36. va_end(args);
  37. }