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.

log.cc 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright 2020 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. #include "common/log.h"
  17. #include <sys/time.h>
  18. #include "glog/logging.h"
  19. namespace mindspore {
  20. namespace serving {
  21. #undef Dlog
  22. #define Dlog(module_id, level, format, ...) \
  23. do { \
  24. DlogInner((module_id), (level), (format), ##__VA_ARGS__); \
  25. } while (0)
  26. static std::string GetTime() {
  27. #define BUFLEN 80
  28. static char buf[BUFLEN];
  29. #if defined(_WIN32) || defined(_WIN64)
  30. time_t time_seconds = time(0);
  31. struct tm now_time;
  32. localtime_s(&now_time, &time_seconds);
  33. sprintf_s(buf, BUFLEN, "%d-%d-%d %d:%d:%d", now_time.tm_year + 1900, now_time.tm_mon + 1, now_time.tm_mday,
  34. now_time.tm_hour, now_time.tm_min, now_time.tm_sec);
  35. #else
  36. struct timeval cur_time;
  37. (void)gettimeofday(&cur_time, nullptr);
  38. struct tm now;
  39. (void)localtime_r(&cur_time.tv_sec, &now);
  40. (void)strftime(buf, BUFLEN, "%Y-%m-%d-%H:%M:%S", &now); // format date and time
  41. // set micro-second
  42. buf[27] = '\0';
  43. int idx = 26;
  44. auto num = cur_time.tv_usec;
  45. for (int i = 5; i >= 0; i--) {
  46. buf[idx--] = static_cast<char>(num % 10 + '0');
  47. num /= 10;
  48. if (i % 3 == 0) {
  49. buf[idx--] = '.';
  50. }
  51. }
  52. #endif
  53. return std::string(buf);
  54. }
  55. static std::string GetProcName() {
  56. #if defined(__APPLE__) || defined(__FreeBSD__)
  57. const char *appname = getprogname();
  58. #elif defined(_GNU_SOURCE)
  59. const char *appname = program_invocation_name;
  60. #else
  61. const char *appname = "?";
  62. #endif
  63. // some times, the appname is an absolute path, its too long
  64. std::string app_name(appname);
  65. std::size_t pos = app_name.rfind("/");
  66. if (pos == std::string::npos) {
  67. return app_name;
  68. }
  69. if (pos + 1 >= app_name.size()) {
  70. return app_name;
  71. }
  72. return app_name.substr(pos + 1);
  73. }
  74. static std::string GetLogLevel(ERROR_LEVEL level) {
  75. switch (level) {
  76. case LOG_DEBUG:
  77. return "DEBUG";
  78. case LOG_INFO:
  79. return "INFO";
  80. case LOG_WARNING:
  81. return "WARNING";
  82. case LOG_ERROR:
  83. default:
  84. return "ERROR";
  85. }
  86. }
  87. // convert MsLogLevel to corresponding glog level
  88. static int GetGlogLevel(ERROR_LEVEL level) {
  89. switch (level) {
  90. case LOG_DEBUG:
  91. case LOG_INFO:
  92. return google::GLOG_INFO;
  93. case LOG_WARNING:
  94. return google::GLOG_WARNING;
  95. case LOG_ERROR:
  96. default:
  97. return google::GLOG_ERROR;
  98. }
  99. }
  100. void LogWriter::OutputLog(const std::ostringstream &msg) const {
  101. auto submodule_name = "Serving";
  102. google::LogMessage("", 0, GetGlogLevel(log_level_)).stream()
  103. << "[" << GetLogLevel(log_level_) << "] " << submodule_name << "(" << getpid() << "," << GetProcName()
  104. << "):" << GetTime() << " "
  105. << "[" << file_ << ":" << line_ << "] " << func_ << "] " << msg.str() << std::endl;
  106. }
  107. } // namespace serving
  108. } // namespace mindspore

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.