logging.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. 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. #include "logging.h"
  19. #include <ctime>
  20. #include <cstring>
  21. #include <thread>
  22. #include <iostream>
  23. #include <sstream>
  24. namespace hdfs
  25. {
  26. LogManager::LogManager() {}
  27. std::unique_ptr<LoggerInterface> LogManager::logger_impl_(new StderrLogger());
  28. std::mutex LogManager::impl_lock_;
  29. uint32_t LogManager::component_mask_ = 0xFFFFFFFF;
  30. uint32_t LogManager::level_threshold_ = kWarning;
  31. void LogManager::DisableLogForComponent(LogSourceComponent c) {
  32. // AND with all bits other than one we want to unset
  33. std::lock_guard<std::mutex> impl_lock(impl_lock_);
  34. component_mask_ &= ~c;
  35. }
  36. void LogManager::EnableLogForComponent(LogSourceComponent c) {
  37. // OR with bit to set
  38. std::lock_guard<std::mutex> impl_lock(impl_lock_);
  39. component_mask_ |= c;
  40. }
  41. void LogManager::SetLogLevel(LogLevel level) {
  42. std::lock_guard<std::mutex> impl_lock(impl_lock_);
  43. level_threshold_ = level;
  44. }
  45. void LogManager::Write(const LogMessage& msg) {
  46. std::lock_guard<std::mutex> impl_lock(impl_lock_);
  47. if(logger_impl_)
  48. logger_impl_->Write(msg);
  49. }
  50. void LogManager::SetLoggerImplementation(std::unique_ptr<LoggerInterface> impl) {
  51. std::lock_guard<std::mutex> impl_lock(impl_lock_);
  52. logger_impl_.reset(impl.release());
  53. }
  54. /**
  55. * Simple plugin to dump logs to stderr
  56. **/
  57. void StderrLogger::Write(const LogMessage& msg) {
  58. std::stringstream formatted;
  59. if(show_level_)
  60. formatted << msg.level_string();
  61. if(show_component_)
  62. formatted << msg.component_string();
  63. if(show_timestamp_) {
  64. time_t current_time = std::time(nullptr);
  65. char timestr[128];
  66. memset(timestr, 0, 128);
  67. int res = std::strftime(timestr, 128, "%a %b %e %H:%M:%S %Y", std::localtime(&current_time));
  68. if(res > 0) {
  69. formatted << '[' << (const char*)timestr << ']';
  70. } else {
  71. formatted << "[Error formatting timestamp]";
  72. }
  73. }
  74. if(show_component_) {
  75. formatted << "[Thread id = " << std::this_thread::get_id() << ']';
  76. }
  77. if(show_file_) {
  78. // __FILE__ contains absolute path, which is giant if doing a build inside the
  79. // Hadoop tree. Trim down to relative to libhdfspp/
  80. std::string abs_path(msg.file_name());
  81. size_t rel_path_idx = abs_path.find("libhdfspp/");
  82. // Default to whole string if library is being built in an odd way
  83. if(rel_path_idx == std::string::npos)
  84. rel_path_idx = 0;
  85. formatted << '[' << (const char*)&abs_path[rel_path_idx] << ":" << msg.file_line() << ']';
  86. }
  87. std::cerr << formatted.str() << " " << msg.MsgString() << std::endl;
  88. }
  89. void StderrLogger::set_show_timestamp(bool show) {
  90. show_timestamp_ = show;
  91. }
  92. void StderrLogger::set_show_level(bool show) {
  93. show_level_ = show;
  94. }
  95. void StderrLogger::set_show_thread(bool show) {
  96. show_thread_ = show;
  97. }
  98. void StderrLogger::set_show_component(bool show) {
  99. show_component_ = show;
  100. }
  101. LogMessage::~LogMessage() {
  102. LogManager::Write(*this);
  103. }
  104. LogMessage& LogMessage::operator<<(const std::string *str) {
  105. if(str)
  106. msg_buffer_ << str;
  107. else
  108. msg_buffer_ << "<nullptr>";
  109. return *this;
  110. }
  111. LogMessage& LogMessage::operator<<(const std::string& str) {
  112. msg_buffer_ << str;
  113. return *this;
  114. }
  115. LogMessage& LogMessage::operator<<(const char *str) {
  116. if(str)
  117. msg_buffer_ << str;
  118. else
  119. msg_buffer_ << "<nullptr>";
  120. return *this;
  121. }
  122. LogMessage& LogMessage::operator<<(bool val) {
  123. if(val)
  124. msg_buffer_ << "true";
  125. else
  126. msg_buffer_ << "false";
  127. return *this;
  128. }
  129. LogMessage& LogMessage::operator<<(int32_t val) {
  130. msg_buffer_ << val;
  131. return *this;
  132. }
  133. LogMessage& LogMessage::operator<<(uint32_t val) {
  134. msg_buffer_ << val;
  135. return *this;
  136. }
  137. LogMessage& LogMessage::operator<<(int64_t val) {
  138. msg_buffer_ << val;
  139. return *this;
  140. }
  141. LogMessage& LogMessage::operator<<(uint64_t val) {
  142. msg_buffer_ << val;
  143. return *this;
  144. }
  145. LogMessage& LogMessage::operator<<(void *ptr) {
  146. msg_buffer_ << ptr;
  147. return *this;
  148. }
  149. std::string LogMessage::MsgString() const {
  150. return msg_buffer_.str();
  151. }
  152. const char * kLevelStrings[5] = {
  153. "[TRACE ]",
  154. "[DEBUG ]",
  155. "[INFO ]",
  156. "[WARN ]",
  157. "[ERROR ]"
  158. };
  159. const char * LogMessage::level_string() const {
  160. return kLevelStrings[level_];
  161. }
  162. const char * kComponentStrings[5] = {
  163. "[Unknown ]",
  164. "[RPC ]",
  165. "[BlockReader ]",
  166. "[FileHandle ]",
  167. "[FileSystem ]"
  168. };
  169. const char * LogMessage::component_string() const {
  170. switch(component_) {
  171. case kRPC: return kComponentStrings[1];
  172. case kBlockReader: return kComponentStrings[2];
  173. case kFileHandle: return kComponentStrings[3];
  174. case kFileSystem: return kComponentStrings[4];
  175. default: return kComponentStrings[0];
  176. }
  177. }
  178. }