1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <sys/stat.h>
  5. #include <stdio.h>
  6. /* Clamps x(value) between y(min) and z(max) in a nested ternary operation */
  7. #define LIGHT_CLAMP(val, min, max) \
  8. (val < min \
  9. ? light_log_clamp_min(min) \
  10. : (val > max \
  11. ? light_log_clamp_max(max) \
  12. : val ))
  13. /* Verbosity levels:
  14. * 0 - No output
  15. * 1 - Errors
  16. * 2 - Errors, warnings
  17. * 3 - Errors, warnings, notices
  18. */
  19. typedef enum {
  20. LIGHT_ERROR_LEVEL = 1,
  21. LIGHT_WARN_LEVEL,
  22. LIGHT_NOTE_LEVEL
  23. } light_loglevel_t;
  24. light_loglevel_t light_loglevel;
  25. #define LIGHT_LOG(lvl, fp, fmt, args...) \
  26. if (light_loglevel >= lvl) \
  27. fprintf(fp, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##args)
  28. #define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL, stdout, " Notice: " fmt, ##args)
  29. #define LIGHT_WARN(fmt, args...) LIGHT_LOG(LIGHT_WARN_LEVEL, stderr, " Warning: " fmt, ##args)
  30. #define LIGHT_ERR(fmt, args...) LIGHT_LOG(LIGHT_ERROR_LEVEL, stderr, " Error: " fmt, ##args)
  31. #define LIGHT_MEMERR() LIGHT_ERR("memory error");
  32. #define LIGHT_PERMLOG(act, log) \
  33. do { \
  34. log("could not open '%s' for " act, filename); \
  35. log("Verify it exists with the right permissions"); \
  36. } while (0)
  37. #define LIGHT_PERMERR(x) LIGHT_PERMLOG(x, LIGHT_ERR)
  38. #define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x, LIGHT_WARN)
  39. bool light_file_write_uint64 (char const *filename, uint64_t val);
  40. bool light_file_read_uint64 (char const *filename, uint64_t *val);
  41. bool light_file_is_writable (char const *filename);
  42. bool light_file_is_readable (char const *filename);
  43. uint64_t light_log_clamp_min(uint64_t min);
  44. uint64_t light_log_clamp_max(uint64_t max);
  45. double light_percent_clamp(double percent);
  46. int light_mkpath(char *dir, mode_t mode);