log.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * C Fancy Logger
  3. * log.h:
  4. * Copyright (c) 2009-2022 986-Studio. All rights reserved.
  5. *
  6. * Created by Manoël Trapier on 20/01/2009.
  7. */
  8. #ifndef _LOG_H
  9. #define _LOG_H
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. enum
  14. {
  15. TLOG_ALWAYS = -1,
  16. TLOG_PANIC = 0,
  17. TLOG_ERROR,
  18. TLOG_WARNING,
  19. TLOG_NORMAL,
  20. TLOG_VERBOSE,
  21. TLOG_DEBUG,
  22. };
  23. //#define LOG_ALWAYS_FFLUSH
  24. #define DYNA_LOG_LEVEL
  25. #define SET_DEBUG_LOG
  26. /* Set if DYNALOG is set the maximum compiled log level */
  27. #ifndef MAXIMUM_DEBUG_LEVEL
  28. #ifndef SET_DEBUG_LOG
  29. #define MAXIMUM_DEBUG_LEVEL TLOG_NORMAL
  30. #else
  31. #define MAXIMUM_DEBUG_LEVEL TLOG_DEBUG
  32. #endif
  33. #endif /* MAXIMUM_DEBUG_LEVEL */
  34. /* Set the default log level */
  35. #ifndef SET_DEBUG_LOG
  36. #define DEFAULT_DEBUG_LEVEL TLOG_PANIC
  37. #else
  38. #define DEFAULT_DEBUG_LEVEL TLOG_NORMAL
  39. #endif
  40. /******************************************************************************/
  41. /* DO NOT MESS AFTER THIS LINE */
  42. /******************************************************************************/
  43. #ifdef DYNA_LOG_LEVEL
  44. # ifdef MAX_DEBUG_LEVEL
  45. # undef MAX_DEBUG_LEVEL
  46. # endif
  47. # ifdef __LOG_C_INTERNAL_
  48. int MAX_DEBUG_LEVEL = DEFAULT_DEBUG_LEVEL;
  49. #else
  50. extern int MAX_DEBUG_LEVEL;
  51. #endif
  52. #else
  53. # ifndef MAX_DEBUG_LEVEL
  54. # define MAX_DEBUG_LEVEL DEFAULT_DEBUG_LEVEL
  55. # endif
  56. #endif
  57. #define Log(_level, _user, _fmt, ...)\
  58. if (_level <= MAXIMUM_DEBUG_LEVEL)\
  59. if ((_level <= MAX_DEBUG_LEVEL) || (_level <= TLOG_PANIC))\
  60. do { log_real(_level, _user, _fmt, ##__VA_ARGS__); } while(0)
  61. void log_real(int level, const char *user, const char *fmt, ...);
  62. #define LOG(_level, _str, ...) if ((_level <= MAX_DEBUG_LEVEL) || (_level <= TLOG_PANIC)) do { fputs(_str, stderr); } while(0)
  63. #define LOGCODE(_level, _user, _code)\
  64. if (_level <= MAXIMUM_DEBUG_LEVEL) do{\
  65. Log(_level, _user, "");\
  66. if ((_level <= MAX_DEBUG_LEVEL) || (_level <= TLOG_PANIC))\
  67. do { _code; fprintf(stderr, "\n"); } while(0); } while(0)
  68. #define INFOL(_level, _fmt) LOGCODE(_level, "INFOL", { printf _fmt; })
  69. #define FUNC_IN() Log(TLOG_VERBOSE, NULL, ">>%s", __func__)
  70. #define FUNC_OUT() Log(TLOG_VERBOSE, NULL, "<<%s", __func__)
  71. #define FUNC_OUTR(out) Log(TLOG_VERBOSE, NULL, "<<%s (%d)", __func__, out)
  72. void log_displayPanic(int signal);
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif /* _LOG_H */