log.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * C Fancy Logger
  3. * log.h:
  4. * Copyright (c) 2009-2021 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 TIME_STAMP_LOG
  24. //#define LOG_ALWAYS_FFLUSH
  25. #define DYNA_LOG_LEVEL
  26. //#define SET_DEBUG_LOG
  27. /* Set if DYNALOG is set the maxium compiled log level */
  28. #ifndef MAXIMUM_DEBUG_LEVEL
  29. #ifndef SET_DEBUG_LOG
  30. #define MAXIMUM_DEBUG_LEVEL TLOG_NORMAL
  31. #else
  32. #define MAXIMUM_DEBUG_LEVEL TLOG_VERBOSE
  33. #endif
  34. #endif /* MAXIMUM_DEBUG_LEVEL */
  35. /* Set the default log level */
  36. #ifndef SET_DEBUG_LOG
  37. #define DEFAULT_DEBUG_LEVEL TLOG_PANIC
  38. #else
  39. #define DEFAULT_DEBUG_LEVEL TLOG_NORMAL
  40. #endif
  41. /******************************************************************************/
  42. /* DO NOT MESS AFTER THIS LINE */
  43. /******************************************************************************/
  44. #ifdef DYNA_LOG_LEVEL
  45. # ifdef MAX_DEBUG_LEVEL
  46. # undef MAX_DEBUG_LEVEL
  47. # endif
  48. # ifdef __LOG_C_INTERNAL_
  49. int MAX_DEBUG_LEVEL = DEFAULT_DEBUG_LEVEL;
  50. #else
  51. extern int MAX_DEBUG_LEVEL;
  52. #endif
  53. #else
  54. # ifndef MAX_DEBUG_LEVEL
  55. # define MAX_DEBUG_LEVEL DEFAULT_DEBUG_LEVEL
  56. # endif
  57. #endif
  58. extern char use_syslog;
  59. #define log(_level, _user, _fmt, ...)\
  60. if (_level <= MAXIMUM_DEBUG_LEVEL)\
  61. if ((_level <= MAX_DEBUG_LEVEL) || (_level <= TLOG_PANIC))\
  62. do { log_real(_level, _user, _fmt, ##__VA_ARGS__); } while(0)
  63. void log_real(int level, const char *user, const char *fmt, ...);
  64. #define LOG(_level, _str, ...) if ((_level <= MAX_DEBUG_LEVEL) || (_level <= TLOG_PANIC)) do { fputs(_str, stderr); } while(0)
  65. #define LOGCODE(_level, _user, _code)\
  66. if (_level <= MAXIMUM_DEBUG_LEVEL) do{\
  67. log(_level, _user, "");\
  68. if ((_level <= MAX_DEBUG_LEVEL) || (_level <= TLOG_PANIC))\
  69. do { _code; fprintf(stderr, "\n"); } while(0); } while(0)
  70. #define INFOL(_level, _fmt) LOGCODE(_level, "INFOL", { printf _fmt; })
  71. #define FUNC_IN() log(TLOG_VERBOSE, NULL, ">>%s", __func__)
  72. #define FUNC_OUT() log(TLOG_VERBOSE, NULL, "<<%s", __func__)
  73. #define FUNC_OUTR(out) log(TLOG_VERBOSE, NULL, "<<%s (%d)", __func__, out)
  74. void log_displayPanic(int signal);
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif /* _LOG_H */