syslog.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2017-2024 Alibaba Group Holding Limited
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /******************************************************************************
  19. * @file syslog.h
  20. * @brief Defines syslog APIs and usage
  21. * @version V1.1
  22. * @date 14. February 2019
  23. * @usage Add 3 lines codes below at head of source code file
  24. * // 0: Err; 1: Err&Warn; 2: Err&Warn&Info; 3: Err&Warn&Info&Debug
  25. * #define LOG_LEVEL 3
  26. * #include <syslog.h>
  27. ******************************************************************************/
  28. #include <stdio.h>
  29. #ifndef _SYSLOG_H_
  30. #define _SYSLOG_H_
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #ifdef LOG_LEVEL
  35. #if (LOG_LEVEL >= 3) && \
  36. (defined CONFIG_SYSLOG_LEVEL_DEBUG)
  37. #define LOG_ENABLE_D
  38. #endif
  39. #if (LOG_LEVEL >= 2) && \
  40. (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
  41. defined CONFIG_SYSLOG_LEVEL_INFO)
  42. #define LOG_ENABLE_I
  43. #endif
  44. #if (LOG_LEVEL >= 1) && \
  45. (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
  46. defined CONFIG_SYSLOG_LEVEL_INFO || \
  47. defined CONFIG_SYSLOG_LEVEL_WARN)
  48. #define LOG_ENABLE_W
  49. #endif
  50. #if (LOG_LEVEL >= 0) && \
  51. (defined CONFIG_SYSLOG_LEVEL_DEBUG || \
  52. defined CONFIG_SYSLOG_LEVEL_INFO || \
  53. defined CONFIG_SYSLOG_LEVEL_WARN || \
  54. defined CONFIG_SYSLOG_LEVEL_ERROR)
  55. #define LOG_ENABLE_E
  56. #endif
  57. #endif /* #ifdef LOG_LEVEL */
  58. /* [LogLevel:FileName:Function:Line] */
  59. extern const char *PFORMAT_D;
  60. extern const char *PFORMAT_I;
  61. extern const char *PFORMAT_W;
  62. extern const char *PFORMAT_E;
  63. #define LOG_E_BASE_ARGS __FUNCTION__, __LINE__
  64. #define LOG_W_BASE_ARGS __FUNCTION__, __LINE__
  65. #define LOG_I_BASE_ARGS __FUNCTION__, __LINE__
  66. #define LOG_D_BASE_ARGS __FUNCTION__, __LINE__
  67. /* Log in freely format without prefix */
  68. #define LOG_F(fmt, args...) printf(fmt,##args)
  69. /* Log debug */
  70. #ifdef LOG_ENABLE_D
  71. #define LOG_D(fmt, args...) \
  72. do {printf(PFORMAT_D,LOG_D_BASE_ARGS); printf(fmt,##args);} while(0)
  73. #else
  74. #define LOG_D(fmt, args...)
  75. #endif
  76. /* Log information */
  77. #ifdef LOG_ENABLE_I
  78. #define LOG_I(fmt, args...) \
  79. do {printf(PFORMAT_I ,LOG_I_BASE_ARGS); printf(fmt,##args);} while(0)
  80. #else
  81. #define LOG_I(fmt, args...)
  82. #endif
  83. /* Log warning */
  84. #ifdef LOG_ENABLE_W
  85. #define LOG_W(fmt, args...) \
  86. do {printf(PFORMAT_W,LOG_W_BASE_ARGS); printf(fmt,##args);} while(0)
  87. #else
  88. #define LOG_W(fmt, args...)
  89. #endif
  90. /* Log error */
  91. #ifdef LOG_ENABLE_E
  92. #define LOG_E(fmt, args...) \
  93. do {printf(PFORMAT_E,LOG_E_BASE_ARGS); printf(fmt,##args);} while(0)
  94. #else
  95. #define LOG_E(fmt, args...)
  96. #endif
  97. #define ENTER() LOG_D("Enter\n")
  98. #define EXIT_VOID() do { LOG_D("Exit\n"); return;} while(0)
  99. #define EXIT_INT(val) do { LOG_D("Exit, return val=%d\n", (int)val); return val;} while(0)
  100. #define EXIT_PTR(ptr) do { LOG_D("Exit, return ptr=%p\n", (void*)ptr); return ptr;} while(0)
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* _SYSLOG_H_ */