You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mslog.h 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef PREDICT_COMMON_MSLOG_H_
  17. #define PREDICT_COMMON_MSLOG_H_
  18. #include <syslog.h>
  19. #include <unistd.h>
  20. #include <csignal>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <string>
  24. #if defined(__ANDROID__) || defined(ANDROID)
  25. #include <android/log.h>
  26. #endif
  27. namespace mindspore {
  28. namespace predict {
  29. constexpr const char *TAG = "MS_PREDICT";
  30. constexpr int DEBUG = 1;
  31. constexpr int INFO = 2;
  32. constexpr int WARN = 3;
  33. constexpr int ERROR = 4;
  34. #define MSPREDICT_API __attribute__((visibility("default")))
  35. bool MSPREDICT_API IsPrint(int level);
  36. #if !defined(__ANDROID__) && !defined(ANDROID)
  37. #if LOG_TO_FILE
  38. #define MS_LOGD(fmt, args...) \
  39. { \
  40. if (mindspore::predict::IsPrint(mindspore::predict::DEBUG)) { \
  41. syslog(LOG_DEBUG, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, \getpid(), __func__, __LINE__, ##args); \
  42. } \
  43. }
  44. #define MS_LOGI(fmt, args...) \
  45. { \
  46. if (mindspore::predict::IsPrint(mindspore::predict::INFO)) { \
  47. syslog(LOG_INFO, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, \getpid(), __func__, __LINE__, ##args); \
  48. } \
  49. }
  50. #define MS_LOGW(fmt, args...) \
  51. { \
  52. if (mindspore::predict::IsPrint(mindspore::predict::WARN)) { \
  53. syslog(LOG_WARNING, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, \getpid(), __func__, __LINE__, ##args); \
  54. } \
  55. }
  56. #define MS_LOGE(fmt, args...) \
  57. { \
  58. if (mindspore::predict::IsPrint(mindspore::predict::ERROR)) { \
  59. syslog(LOG_ERR, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, getpid(), __func__, __LINE__, ##args); \
  60. } \
  61. }
  62. #else
  63. #define MS_LOGD(fmt, args...) \
  64. { \
  65. if (mindspore::predict::IsPrint(mindspore::predict::DEBUG)) { \
  66. printf("[DEBUG] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \
  67. __LINE__, ##args); \
  68. } \
  69. }
  70. #define MS_LOGI(fmt, args...) \
  71. { \
  72. if (mindspore::predict::IsPrint(mindspore::predict::INFO)) { \
  73. printf("[INFO] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \
  74. __LINE__, ##args); \
  75. } \
  76. }
  77. #define MS_LOGW(fmt, args...) \
  78. { \
  79. if (mindspore::predict::IsPrint(mindspore::predict::WARN)) { \
  80. printf("[WARN] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \
  81. __LINE__, ##args); \
  82. } \
  83. }
  84. #define MS_LOGE(fmt, args...) \
  85. { \
  86. if (mindspore::predict::IsPrint(mindspore::predict::ERROR)) { \
  87. printf("[ERROR] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \
  88. __LINE__, ##args); \
  89. } \
  90. }
  91. #endif
  92. #else
  93. #define MS_LOGD(fmt, args...) \
  94. { \
  95. if (mindspore::predict::IsPrint(mindspore::predict::DEBUG)) \
  96. __android_log_print(ANDROID_LOG_DEBUG, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \
  97. __LINE__, ##args); \
  98. }
  99. #define MS_LOGI(fmt, args...) \
  100. { \
  101. if (mindspore::predict::IsPrint(mindspore::predict::INFO)) \
  102. __android_log_print(ANDROID_LOG_INFO, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \
  103. __LINE__, ##args); \
  104. }
  105. #define MS_LOGW(fmt, args...) \
  106. { \
  107. if (mindspore::predict::IsPrint(mindspore::predict::WARN)) \
  108. __android_log_print(ANDROID_LOG_WARN, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \
  109. __LINE__, ##args); \
  110. }
  111. #define MS_LOGE(fmt, args...) \
  112. { \
  113. if (mindspore::predict::IsPrint(mindspore::predict::ERROR)) \
  114. __android_log_print(ANDROID_LOG_ERROR, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \
  115. __LINE__, ##args); \
  116. }
  117. #endif
  118. #define MS_LOG(severity) std::cout << std::endl
  119. #define MS_DLOG(verboselevel) std::cout << std::endl
  120. // Kill the process for safe exiting.
  121. inline void KillProcess(const std::string &ret) {
  122. MS_LOG(ERROR) << "mindspore Exit Tip:" << ret;
  123. if (raise(SIGKILL) != 0) {
  124. MS_LOGE("Send SIGKILL to kill process failed");
  125. }
  126. }
  127. } // namespace predict
  128. } // namespace mindspore
  129. #define MS_ASSERT(expression) \
  130. do { \
  131. if (!(expression)) { \
  132. std::stringstream ss; \
  133. ss << "Assertion failed: " << #expression << ", file: " << __FILE__ << ", line: " << __LINE__; \
  134. mindspore::predict::KillProcess(ss.str()); \
  135. } \
  136. } while (0)
  137. #define MS_EXIT(ret) \
  138. do { \
  139. std::stringstream ss; \
  140. ss << (ret) << " ( file: " << __FILE__ << ", line: " << __LINE__ << " )."; \
  141. mindspore::predict::KillProcess(ss.str()); \
  142. } while (0)
  143. #define MS_PRINT_ERROR(fmt, args...) \
  144. printf(#fmt "\n", ##args); \
  145. MS_LOGE(fmt, ##args);
  146. #define MS_PRINT_INFO(fmt, args...) \
  147. printf(fmt "\n", ##args); \
  148. MS_LOGI(fmt, ##args);
  149. constexpr int LOG_CHECK_EVERY_FIRSTNUM = 10;
  150. constexpr int LOG_CHECK_EVERY_NUM1 = 10;
  151. constexpr int LOG_CHECK_EVERY_NUM2 = 100;
  152. constexpr int LOG_CHECK_EVERY_NUM3 = 1000;
  153. constexpr int LOG_CHECK_EVERY_NUM4 = 10000;
  154. #define LOG_CHECK_ID_CONCAT(word1, word2) word1##word2
  155. #define LOG_CHECK_ID LOG_CHECK_ID_CONCAT(__FUNCTION__, __LINE__)
  156. #define LOG_CHECK_FIRST_N \
  157. [](uint32_t firstNum) { \
  158. static uint32_t LOG_CHECK_ID = 0; \
  159. ++LOG_CHECK_ID; \
  160. return (LOG_CHECK_ID <= firstNum); \
  161. }
  162. #define LOG_CHECK_EVERY_N1 \
  163. [](uint32_t firstNum, uint32_t num) { \
  164. static uint32_t LOG_CHECK_ID = 0; \
  165. ++LOG_CHECK_ID; \
  166. return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID % num == 0)); \
  167. }
  168. #define LOG_CHECK_EVERY_N2 \
  169. [](uint32_t firstNum, uint32_t num1, uint32_t num2) { \
  170. static uint32_t LOG_CHECK_ID = 0; \
  171. ++LOG_CHECK_ID; \
  172. return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
  173. (LOG_CHECK_ID % num2 == 0)); \
  174. }
  175. #define LOG_CHECK_EVERY_N3 \
  176. [](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3) { \
  177. static uint32_t LOG_CHECK_ID = 0; \
  178. ++LOG_CHECK_ID; \
  179. return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
  180. (LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID % num3 == 0)); \
  181. }
  182. #define LOG_CHECK_EVERY_N4 \
  183. [](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4) { \
  184. static uint32_t LOG_CHECK_ID = 0; \
  185. ++LOG_CHECK_ID; \
  186. return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
  187. (LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID < num4 && LOG_CHECK_ID % num3 == 0) || \
  188. (LOG_CHECK_ID % num4 == 0)); \
  189. }
  190. #define LOG_CHECK_EVERY_N \
  191. []() { \
  192. static uint32_t LOG_CHECK_ID = 0; \
  193. ++LOG_CHECK_ID; \
  194. return ((LOG_CHECK_ID <= LOG_CHECK_EVERY_FIRSTNUM) || \
  195. (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM2 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM1 == 0) || \
  196. (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM3 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM2 == 0) || \
  197. (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM4 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM3 == 0) || \
  198. (LOG_CHECK_ID % LOG_CHECK_EVERY_NUM4 == 0)); \
  199. }
  200. #endif // PREDICT_COMMON_MSLOG_H_