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

386 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/logging.h"
  11. #include <string.h>
  12. #include <algorithm>
  13. #include "rtc_base/arraysize.h"
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/event.h"
  16. #include "rtc_base/platform_thread.h"
  17. #include "rtc_base/stream.h"
  18. #include "rtc_base/time_utils.h"
  19. #include "test/gtest.h"
  20. namespace rtc {
  21. namespace {
  22. class StringStream : public StreamInterface {
  23. public:
  24. explicit StringStream(std::string* str);
  25. explicit StringStream(const std::string& str);
  26. StreamState GetState() const override;
  27. StreamResult Read(void* buffer,
  28. size_t buffer_len,
  29. size_t* read,
  30. int* error) override;
  31. StreamResult Write(const void* data,
  32. size_t data_len,
  33. size_t* written,
  34. int* error) override;
  35. void Close() override;
  36. private:
  37. std::string& str_;
  38. size_t read_pos_;
  39. bool read_only_;
  40. };
  41. StringStream::StringStream(std::string* str)
  42. : str_(*str), read_pos_(0), read_only_(false) {}
  43. StringStream::StringStream(const std::string& str)
  44. : str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
  45. StreamState StringStream::GetState() const {
  46. return SS_OPEN;
  47. }
  48. StreamResult StringStream::Read(void* buffer,
  49. size_t buffer_len,
  50. size_t* read,
  51. int* error) {
  52. size_t available = std::min(buffer_len, str_.size() - read_pos_);
  53. if (!available)
  54. return SR_EOS;
  55. memcpy(buffer, str_.data() + read_pos_, available);
  56. read_pos_ += available;
  57. if (read)
  58. *read = available;
  59. return SR_SUCCESS;
  60. }
  61. StreamResult StringStream::Write(const void* data,
  62. size_t data_len,
  63. size_t* written,
  64. int* error) {
  65. if (read_only_) {
  66. if (error) {
  67. *error = -1;
  68. }
  69. return SR_ERROR;
  70. }
  71. str_.append(static_cast<const char*>(data),
  72. static_cast<const char*>(data) + data_len);
  73. if (written)
  74. *written = data_len;
  75. return SR_SUCCESS;
  76. }
  77. void StringStream::Close() {}
  78. } // namespace
  79. template <typename Base>
  80. class LogSinkImpl : public LogSink, public Base {
  81. public:
  82. LogSinkImpl() {}
  83. template <typename P>
  84. explicit LogSinkImpl(P* p) : Base(p) {}
  85. private:
  86. void OnLogMessage(const std::string& message) override {
  87. static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
  88. nullptr);
  89. }
  90. };
  91. class LogMessageForTesting : public LogMessage {
  92. public:
  93. LogMessageForTesting(const char* file,
  94. int line,
  95. LoggingSeverity sev,
  96. LogErrorContext err_ctx = ERRCTX_NONE,
  97. int err = 0)
  98. : LogMessage(file, line, sev, err_ctx, err) {}
  99. const std::string& get_extra() const { return extra_; }
  100. #if defined(WEBRTC_ANDROID)
  101. const char* get_tag() const { return tag_; }
  102. #endif
  103. // Returns the contents of the internal log stream.
  104. // Note that parts of the stream won't (as is) be available until *after* the
  105. // dtor of the parent class has run. So, as is, this only represents a
  106. // partially built stream.
  107. std::string GetPrintStream() {
  108. RTC_DCHECK(!is_finished_);
  109. is_finished_ = true;
  110. FinishPrintStream();
  111. return print_stream_.Release();
  112. }
  113. private:
  114. bool is_finished_ = false;
  115. };
  116. // Test basic logging operation. We should get the INFO log but not the VERBOSE.
  117. // We should restore the correct global state at the end.
  118. TEST(LogTest, SingleStream) {
  119. int sev = LogMessage::GetLogToStream(nullptr);
  120. std::string str;
  121. LogSinkImpl<StringStream> stream(&str);
  122. LogMessage::AddLogToStream(&stream, LS_INFO);
  123. EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
  124. RTC_LOG(LS_INFO) << "INFO";
  125. RTC_LOG(LS_VERBOSE) << "VERBOSE";
  126. EXPECT_NE(std::string::npos, str.find("INFO"));
  127. EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
  128. int i = 1;
  129. long l = 2l;
  130. long long ll = 3ll;
  131. unsigned int u = 4u;
  132. unsigned long ul = 5ul;
  133. unsigned long long ull = 6ull;
  134. std::string s1 = "char*";
  135. std::string s2 = "std::string";
  136. std::string s3 = "absl::stringview";
  137. const char* null_string = nullptr;
  138. void* p = reinterpret_cast<void*>(0xabcd);
  139. // Log all suported types(except doubles/floats) as a sanity-check.
  140. RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
  141. << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
  142. << absl::string_view(s3) << "|" << p << "|" << null_string
  143. << "|";
  144. // Signed integers
  145. EXPECT_NE(std::string::npos, str.find("|1|"));
  146. EXPECT_NE(std::string::npos, str.find("|2|"));
  147. EXPECT_NE(std::string::npos, str.find("|3|"));
  148. // Unsigned integers
  149. EXPECT_NE(std::string::npos, str.find("|4|"));
  150. EXPECT_NE(std::string::npos, str.find("|5|"));
  151. EXPECT_NE(std::string::npos, str.find("|6|"));
  152. // Strings
  153. EXPECT_NE(std::string::npos, str.find("|char*|"));
  154. EXPECT_NE(std::string::npos, str.find("|std::string|"));
  155. EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
  156. // void*
  157. EXPECT_NE(std::string::npos, str.find("|abcd|"));
  158. // null char*
  159. EXPECT_NE(std::string::npos, str.find("|(null)|"));
  160. LogMessage::RemoveLogToStream(&stream);
  161. EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
  162. EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
  163. }
  164. #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
  165. TEST(LogTest, Checks) {
  166. EXPECT_DEATH(FATAL() << "message",
  167. "\n\n#\n"
  168. "# Fatal error in: \\S+, line \\w+\n"
  169. "# last system error: \\w+\n"
  170. "# Check failed: FATAL\\(\\)\n"
  171. "# message");
  172. int a = 1, b = 2;
  173. EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
  174. "\n\n#\n"
  175. "# Fatal error in: \\S+, line \\w+\n"
  176. "# last system error: \\w+\n"
  177. "# Check failed: a == b \\(1 vs. 2\\)\n"
  178. "# 12");
  179. RTC_CHECK_EQ(5, 5);
  180. RTC_CHECK(true) << "Shouldn't crash" << 1;
  181. EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
  182. "\n\n#\n"
  183. "# Fatal error in: \\S+, line \\w+\n"
  184. "# last system error: \\w+\n"
  185. "# Check failed: false\n"
  186. "# Hi there!");
  187. }
  188. #endif
  189. // Test using multiple log streams. The INFO stream should get the INFO message,
  190. // the VERBOSE stream should get the INFO and the VERBOSE.
  191. // We should restore the correct global state at the end.
  192. TEST(LogTest, MultipleStreams) {
  193. int sev = LogMessage::GetLogToStream(nullptr);
  194. std::string str1, str2;
  195. LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
  196. LogMessage::AddLogToStream(&stream1, LS_INFO);
  197. LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
  198. EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
  199. EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
  200. RTC_LOG(LS_INFO) << "INFO";
  201. RTC_LOG(LS_VERBOSE) << "VERBOSE";
  202. EXPECT_NE(std::string::npos, str1.find("INFO"));
  203. EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
  204. EXPECT_NE(std::string::npos, str2.find("INFO"));
  205. EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
  206. LogMessage::RemoveLogToStream(&stream2);
  207. LogMessage::RemoveLogToStream(&stream1);
  208. EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
  209. EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
  210. EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
  211. }
  212. class LogThread {
  213. public:
  214. LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
  215. ~LogThread() { thread_.Stop(); }
  216. void Start() { thread_.Start(); }
  217. private:
  218. void Run() { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }
  219. static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
  220. PlatformThread thread_;
  221. Event event_;
  222. };
  223. // Ensure we don't crash when adding/removing streams while threads are going.
  224. // We should restore the correct global state at the end.
  225. TEST(LogTest, MultipleThreads) {
  226. int sev = LogMessage::GetLogToStream(nullptr);
  227. LogThread thread1, thread2, thread3;
  228. thread1.Start();
  229. thread2.Start();
  230. thread3.Start();
  231. std::string s1, s2, s3;
  232. LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
  233. for (int i = 0; i < 1000; ++i) {
  234. LogMessage::AddLogToStream(&stream1, LS_WARNING);
  235. LogMessage::AddLogToStream(&stream2, LS_INFO);
  236. LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
  237. LogMessage::RemoveLogToStream(&stream1);
  238. LogMessage::RemoveLogToStream(&stream2);
  239. LogMessage::RemoveLogToStream(&stream3);
  240. }
  241. EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
  242. }
  243. TEST(LogTest, WallClockStartTime) {
  244. uint32_t time = LogMessage::WallClockStartTime();
  245. // Expect the time to be in a sensible range, e.g. > 2012-01-01.
  246. EXPECT_GT(time, 1325376000u);
  247. }
  248. TEST(LogTest, CheckExtraErrorField) {
  249. LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
  250. ERRCTX_ERRNO, 0xD);
  251. log_msg.stream() << "This gets added at dtor time";
  252. const std::string& extra = log_msg.get_extra();
  253. const size_t length_to_check = arraysize("[0x12345678]") - 1;
  254. ASSERT_GE(extra.length(), length_to_check);
  255. EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
  256. }
  257. TEST(LogTest, CheckFilePathParsed) {
  258. LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
  259. log_msg.stream() << "<- Does this look right?";
  260. const std::string stream = log_msg.GetPrintStream();
  261. #if defined(WEBRTC_ANDROID)
  262. const char* tag = log_msg.get_tag();
  263. EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
  264. EXPECT_NE(std::string::npos, stream.find("100"));
  265. #else
  266. EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
  267. #endif
  268. }
  269. #if defined(WEBRTC_ANDROID)
  270. TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
  271. std::string str;
  272. LogSinkImpl<StringStream> stream(&str);
  273. LogMessage::AddLogToStream(&stream, LS_INFO);
  274. EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
  275. RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
  276. EXPECT_NE(std::string::npos, str.find("INFO"));
  277. EXPECT_NE(std::string::npos, str.find("my_tag"));
  278. }
  279. #endif
  280. // Test the time required to write 1000 80-character logs to a string.
  281. TEST(LogTest, Perf) {
  282. std::string str;
  283. LogSinkImpl<StringStream> stream(&str);
  284. LogMessage::AddLogToStream(&stream, LS_VERBOSE);
  285. const std::string message(80, 'X');
  286. { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
  287. // We now know how many bytes the logging framework will tag onto every msg.
  288. const size_t logging_overhead = str.size();
  289. // Reset the stream to 0 size.
  290. str.clear();
  291. str.reserve(120000);
  292. static const int kRepetitions = 1000;
  293. int64_t start = TimeMillis(), finish;
  294. for (int i = 0; i < kRepetitions; ++i) {
  295. LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
  296. }
  297. finish = TimeMillis();
  298. LogMessage::RemoveLogToStream(&stream);
  299. stream.Close();
  300. EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
  301. RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
  302. << " total bytes logged: " << str.size();
  303. }
  304. TEST(LogTest, EnumsAreSupported) {
  305. enum class TestEnum { kValue0 = 0, kValue1 = 1 };
  306. std::string str;
  307. LogSinkImpl<StringStream> stream(&str);
  308. LogMessage::AddLogToStream(&stream, LS_INFO);
  309. RTC_LOG(LS_INFO) << "[" << TestEnum::kValue0 << "]";
  310. EXPECT_NE(std::string::npos, str.find("[0]"));
  311. EXPECT_EQ(std::string::npos, str.find("[1]"));
  312. RTC_LOG(LS_INFO) << "[" << TestEnum::kValue1 << "]";
  313. EXPECT_NE(std::string::npos, str.find("[1]"));
  314. LogMessage::RemoveLogToStream(&stream);
  315. stream.Close();
  316. }
  317. } // namespace rtc