logger.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved.
  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. #pragma once
  17. #include <cstdlib>
  18. #include <map>
  19. #include <string>
  20. #include "string_utils.h"
  21. namespace fastertransformer {
  22. class Logger {
  23. public:
  24. enum Level {
  25. TRACE = 0,
  26. DEBUG = 10,
  27. INFO = 20,
  28. WARNING = 30,
  29. ERROR = 40
  30. };
  31. static Logger& getLogger()
  32. {
  33. thread_local Logger instance;
  34. return instance;
  35. }
  36. Logger(Logger const&) = delete;
  37. void operator=(Logger const&) = delete;
  38. template<typename... Args>
  39. void log(const Level level, const std::string format, const Args&... args)
  40. {
  41. if (level_ <= level) {
  42. std::string fmt = getPrefix(level) + format + "\n";
  43. FILE* out = level_ < WARNING ? stdout : stderr;
  44. std::string logstr = fmtstr(fmt, args...);
  45. fprintf(out, "%s", logstr.c_str());
  46. }
  47. }
  48. template<typename... Args>
  49. void log(const Level level, const int rank, const std::string format, const Args&... args)
  50. {
  51. if (level_ <= level) {
  52. std::string fmt = getPrefix(level, rank) + format + "\n";
  53. FILE* out = level_ < WARNING ? stdout : stderr;
  54. std::string logstr = fmtstr(fmt, args...);
  55. fprintf(out, "%s", logstr.c_str());
  56. }
  57. }
  58. void setLevel(const Level level)
  59. {
  60. level_ = level;
  61. log(INFO, "Set logger level by %s", getLevelName(level).c_str());
  62. }
  63. int getLevel() const
  64. {
  65. return level_;
  66. }
  67. private:
  68. const std::string PREFIX = "[FT]";
  69. const std::map<const Level, const std::string> level_name_ = {
  70. {TRACE, "TRACE"}, {DEBUG, "DEBUG"}, {INFO, "INFO"}, {WARNING, "WARNING"}, {ERROR, "ERROR"}};
  71. #ifndef NDEBUG
  72. const Level DEFAULT_LOG_LEVEL = DEBUG;
  73. #else
  74. const Level DEFAULT_LOG_LEVEL = INFO;
  75. #endif
  76. Level level_ = DEFAULT_LOG_LEVEL;
  77. Logger();
  78. inline const std::string getLevelName(const Level level)
  79. {
  80. return level_name_.at(level);
  81. }
  82. inline const std::string getPrefix(const Level level)
  83. {
  84. return PREFIX + "[" + getLevelName(level) + "] ";
  85. }
  86. inline const std::string getPrefix(const Level level, const int rank)
  87. {
  88. return PREFIX + "[" + getLevelName(level) + "][" + std::to_string(rank) + "] ";
  89. }
  90. };
  91. #define FT_LOG(level, ...) \
  92. do { \
  93. if (fastertransformer::Logger::getLogger().getLevel() <= level) { \
  94. fastertransformer::Logger::getLogger().log(level, __VA_ARGS__); \
  95. } \
  96. } while (0)
  97. #define FT_LOG_TRACE(...) FT_LOG(fastertransformer::Logger::TRACE, __VA_ARGS__)
  98. #define FT_LOG_DEBUG(...) FT_LOG(fastertransformer::Logger::DEBUG, __VA_ARGS__)
  99. #define FT_LOG_INFO(...) FT_LOG(fastertransformer::Logger::INFO, __VA_ARGS__)
  100. #define FT_LOG_WARNING(...) FT_LOG(fastertransformer::Logger::WARNING, __VA_ARGS__)
  101. #define FT_LOG_ERROR(...) FT_LOG(fastertransformer::Logger::ERROR, __VA_ARGS__)
  102. } // namespace fastertransformer