link_log.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. #ifndef LITEOS_LAB_IOT_LINK_LINK_LOG_LINK_LOG_H_
  35. #define LITEOS_LAB_IOT_LINK_LINK_LOG_LINK_LOG_H_
  36. #include <stdarg.h>
  37. #include <stdint.h>
  38. #include <stddef.h>
  39. #include <stdio.h>
  40. #include "cmsis_os2.h"
  41. // #include <iot_link_config.h>
  42. /**
  43. * @brief:this defines for the log module, and LINK_LOG_TRACE/LINK_LOG_DEBUG will not participate the compile in the release version
  44. *
  45. *
  46. * */
  47. typedef enum
  48. {
  49. EN_LINK_LOG_LEVEL_TRACE = 0, ///< this is used as the trace function,like the function enter and function out
  50. EN_LINK_LOG_LEVEL_DEBUG, ///< this is used as the debug, you could add any debug as you wish
  51. EN_LINK_LOG_LEVEL_INFO, ///< which means it is import message, and you should known
  52. EN_LINK_LOG_LEVEL_WARN, ///< this is used as the executed result,which means the status is not what we expected,but could accept
  53. EN_LINK_LOG_LEVEL_ERROR, ///< this is used as the executed result,which means the status is not what we expected,could not accepta
  54. EN_LINK_LOG_LEVEL_FATAL, ///< this is used as the parameters input for the api interface, which could not accepted
  55. EN_LINK_LOG_LEVEL_MAX,
  56. }en_link_log_level_t;
  57. /**
  58. * @brief:use this function to get the current output log
  59. *
  60. * @return: the current output mask log, defined by en_link_log_level_t
  61. * */
  62. en_link_log_level_t link_log_level_get(void);
  63. /**
  64. * @brief: use this function to get the debug level name
  65. *
  66. * @parameter[in]:level, the level to get
  67. *
  68. * @return: the mapped level name
  69. * */
  70. const char *link_log_level_name(en_link_log_level_t level);
  71. /**
  72. * @brief:use this function to set the current output log
  73. *
  74. *@parameter[in] level:defined by en_link_log_level_t
  75. *
  76. * @return: 0 success while -1 failed
  77. * */
  78. int link_log_level_set(en_link_log_level_t level);
  79. /*
  80. * @brief: this is a weak function ,and you could rewrite one
  81. *
  82. * @param fmt: same use as the fmt for printf
  83. *
  84. * @param unfixed: same use for printf
  85. *
  86. * @return: don't care about it
  87. *
  88. * @attention: and the components should not call this function directly, you'd better
  89. *
  90. * call LINK_LOG groups
  91. *
  92. * */
  93. //__attribute__((weak)) void link_printf(const char *format, ...);
  94. #ifndef link_printf
  95. #define link_printf(fmt, ...) \
  96. do \
  97. { \
  98. printf(fmt, ##__VA_ARGS__); \
  99. \
  100. }while(0)
  101. #endif
  102. // #ifdef CONFIG_LINKLOG_ENABLE
  103. #define osal_sys_time() (osKernelGetTickCount() * (1000 / 100))
  104. #define LINK_LOG(level,fmt, ...) \
  105. do \
  106. { \
  107. link_printf("[%s][%u][%s] " fmt "\r\n", \
  108. link_log_level_name((level)), (unsigned int)osal_sys_time(),__FUNCTION__, ##__VA_ARGS__); \
  109. } while (0)
  110. #define LINK_LOG_TRACE(fmt, ...) \
  111. do \
  112. { \
  113. if ((EN_LINK_LOG_LEVEL_TRACE) >= link_log_level_get()) \
  114. { \
  115. LINK_LOG(EN_LINK_LOG_LEVEL_TRACE,fmt,##__VA_ARGS__); \
  116. } \
  117. } while (0)
  118. #define LINK_LOG_DEBUG(fmt, ...) \
  119. do \
  120. { \
  121. if ((EN_LINK_LOG_LEVEL_DEBUG) >= link_log_level_get()) \
  122. { \
  123. LINK_LOG(EN_LINK_LOG_LEVEL_DEBUG,fmt,##__VA_ARGS__); \
  124. } \
  125. } while (0)
  126. // #else
  127. // #define LINK_LOG(level,fmt, ...)
  128. // #define LINK_LOG_TRACE(fmt, ...)
  129. // #define LINK_LOG_DEBUG(fmt, ...)
  130. // #endif
  131. #define LINK_LOG_INFO(fmt, ...) LINK_LOG(EN_LINK_LOG_LEVEL_INFO,fmt,##__VA_ARGS__)
  132. #define LINK_LOG_WARN(fmt, ...) LINK_LOG(EN_LINK_LOG_LEVEL_WARN,fmt,##__VA_ARGS__)
  133. #define LINK_LOG_ERROR(fmt, ...) LINK_LOG(EN_LINK_LOG_LEVEL_ERROR,fmt,##__VA_ARGS__)
  134. #define LINK_LOG_FATAL(fmt, ...) LINK_LOG(EN_LINK_LOG_LEVEL_FATAL,fmt,##__VA_ARGS__)
  135. #endif /* LITEOS_LAB_IOT_LINK_LINK_LOG_LINK_LOG_H_ */