syslog.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #ifndef SYSLOG_H
  13. #define SYSLOG_H
  14. #define LOG_ENABLE
  15. #ifdef LOG_LEVEL
  16. #if (LOG_LEVEL >= 3)
  17. #define LOG_ENABLE_D
  18. #endif
  19. #if (LOG_LEVEL >= 2)
  20. #define LOG_ENABLE_I
  21. #endif
  22. #if (LOG_LEVEL >= 1)
  23. #define LOG_ENABLE_W
  24. #endif
  25. #if (LOG_LEVEL >= 0)
  26. #define LOG_ENABLE_E
  27. #endif
  28. #else /* #ifdef LOG_LEVEL */
  29. #define LOG_ENABLE_E /* Default level if LOG_LEVEL not defiend */
  30. #endif /* #ifdef LOG_LEVEL */
  31. /* [LogLevel:FileName:Function:Line] */
  32. extern const char *PFORMAT_D;
  33. extern const char *PFORMAT_I;
  34. extern const char *PFORMAT_W;
  35. extern const char *PFORMAT_E;
  36. extern const char *PFORMAT_O;
  37. #ifdef LOG_PREFIX
  38. #define LOG_PREF LOG_PREFIX"-"
  39. #else
  40. #define LOG_PREF ""
  41. #endif
  42. #define LOG_E_BASE_ARGS LOG_PREF, __FUNCTION__, __LINE__
  43. #define LOG_W_BASE_ARGS LOG_PREF, __FUNCTION__, __LINE__
  44. #define LOG_I_BASE_ARGS LOG_PREF, __FUNCTION__, __LINE__
  45. #define LOG_D_BASE_ARGS LOG_PREF, __FUNCTION__, __LINE__
  46. #define LOG_O_BASE_ARGS LOG_PREF, __FUNCTION__, __LINE__
  47. /* Log in freely format without prefix */
  48. #ifdef LOG_ENABLE
  49. #define LOG_F(fmt, args...) printf(fmt,##args)
  50. #else
  51. #define LOG_F(fmt, args...)
  52. #endif
  53. // OK, Green color
  54. #define LOG_O(fmt, args...) \
  55. do {printf(PFORMAT_O,LOG_O_BASE_ARGS); printf(fmt,##args);} while(0)
  56. #define LOGPRINT_E printf
  57. #define LOGPRINT_W printf
  58. #define LOGPRINT_I printf
  59. #define LOGPRINT_D printf
  60. /* Log debug */
  61. #if defined(LOG_ENABLE_D) && defined(LOG_ENABLE)
  62. #define LOG_D(fmt, args...) \
  63. do {LOGPRINT_D(PFORMAT_D,LOG_D_BASE_ARGS); LOGPRINT_D(fmt,##args);} while(0)
  64. #else
  65. #define LOG_D(fmt, args...)
  66. #endif
  67. /* Log information */
  68. #if defined(LOG_ENABLE_I) && defined(LOG_ENABLE)
  69. #define LOG_I(fmt, args...) \
  70. do {LOGPRINT_I(PFORMAT_I ,LOG_I_BASE_ARGS); LOGPRINT_I(fmt,##args);} while(0)
  71. #else
  72. #define LOG_I(fmt, args...)
  73. #endif
  74. /* Log warning */
  75. #if defined(LOG_ENABLE_W) && defined(LOG_ENABLE)
  76. #define LOG_W(fmt, args...) \
  77. do {LOGPRINT_W(PFORMAT_W,LOG_W_BASE_ARGS); LOGPRINT_W(fmt,##args);} while(0)
  78. #else
  79. #define LOG_W(fmt, args...)
  80. #endif
  81. /* Log error */
  82. #if defined(LOG_ENABLE_E) && defined(LOG_ENABLE)
  83. #define LOG_E(fmt, args...) \
  84. do{LOGPRINT_E(PFORMAT_E,LOG_E_BASE_ARGS); LOGPRINT_E(fmt,##args);} while(0)
  85. #else
  86. #define LOG_E(fmt, args...)
  87. #endif
  88. #define ENTER_VOID() LOG_D("Enter\n")
  89. #define EXIT_VOID() do { LOG_D("Exit\n"); return;} while(0)
  90. #define EXIT_INT(val) do { LOG_D("Exit, return val=%d\n", (int)val); return val;} while(0)
  91. #define EXIT_PTR(ptr) do { LOG_D("Exit, return ptr=%p\n", (void*)ptr); return ptr;} while(0)
  92. #endif // SYSLOG_H