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.

149 lines
5.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/fileutils.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/pathutils.h"
  31. #include "talk/base/stream.h"
  32. #include "talk/base/thread.h"
  33. namespace talk_base {
  34. // Test basic logging operation. We should get the INFO log but not the VERBOSE.
  35. // We should restore the correct global state at the end.
  36. TEST(LogTest, SingleStream) {
  37. int sev = LogMessage::GetLogToStream(NULL);
  38. std::string str;
  39. StringStream stream(str);
  40. LogMessage::AddLogToStream(&stream, LS_INFO);
  41. EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
  42. LOG(LS_INFO) << "INFO";
  43. LOG(LS_VERBOSE) << "VERBOSE";
  44. EXPECT_NE(std::string::npos, str.find("INFO"));
  45. EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
  46. LogMessage::RemoveLogToStream(&stream);
  47. EXPECT_EQ(LogMessage::NO_LOGGING, LogMessage::GetLogToStream(&stream));
  48. EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
  49. }
  50. // Test using multiple log streams. The INFO stream should get the INFO message,
  51. // the VERBOSE stream should get the INFO and the VERBOSE.
  52. // We should restore the correct global state at the end.
  53. TEST(LogTest, MultipleStreams) {
  54. int sev = LogMessage::GetLogToStream(NULL);
  55. std::string str1, str2;
  56. StringStream stream1(str1), stream2(str2);
  57. LogMessage::AddLogToStream(&stream1, LS_INFO);
  58. LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
  59. EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
  60. EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
  61. LOG(LS_INFO) << "INFO";
  62. LOG(LS_VERBOSE) << "VERBOSE";
  63. EXPECT_NE(std::string::npos, str1.find("INFO"));
  64. EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
  65. EXPECT_NE(std::string::npos, str2.find("INFO"));
  66. EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
  67. LogMessage::RemoveLogToStream(&stream2);
  68. LogMessage::RemoveLogToStream(&stream1);
  69. EXPECT_EQ(LogMessage::NO_LOGGING, LogMessage::GetLogToStream(&stream2));
  70. EXPECT_EQ(LogMessage::NO_LOGGING, LogMessage::GetLogToStream(&stream1));
  71. EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
  72. }
  73. // Ensure we don't crash when adding/removing streams while threads are going.
  74. // We should restore the correct global state at the end.
  75. class LogThread : public Thread {
  76. void Run() {
  77. // LS_SENSITIVE to avoid cluttering up any real logging going on
  78. LOG(LS_SENSITIVE) << "LOG";
  79. }
  80. };
  81. TEST(LogTest, MultipleThreads) {
  82. int sev = LogMessage::GetLogToStream(NULL);
  83. LogThread thread1, thread2, thread3;
  84. thread1.Start();
  85. thread2.Start();
  86. thread3.Start();
  87. NullStream stream1, stream2, stream3;
  88. for (int i = 0; i < 1000; ++i) {
  89. LogMessage::AddLogToStream(&stream1, LS_INFO);
  90. LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
  91. LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
  92. LogMessage::RemoveLogToStream(&stream1);
  93. LogMessage::RemoveLogToStream(&stream2);
  94. LogMessage::RemoveLogToStream(&stream3);
  95. }
  96. EXPECT_EQ(sev, LogMessage::GetLogToStream(NULL));
  97. }
  98. TEST(LogTest, WallClockStartTime) {
  99. uint32 time = LogMessage::WallClockStartTime();
  100. // Expect the time to be in a sensible range, e.g. > 2012-01-01.
  101. EXPECT_GT(time, 1325376000u);
  102. }
  103. // Test the time required to write 1000 80-character logs to an unbuffered file.
  104. TEST(LogTest, Perf) {
  105. Pathname path;
  106. EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
  107. path.SetPathname(Filesystem::TempFilename(path, "ut"));
  108. FileStream stream;
  109. EXPECT_TRUE(stream.Open(path.pathname(), "wb", NULL));
  110. stream.DisableBuffering();
  111. LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
  112. uint32 start = Time(), finish;
  113. std::string message('X', 80);
  114. for (int i = 0; i < 1000; ++i) {
  115. LOG(LS_SENSITIVE) << message;
  116. }
  117. finish = Time();
  118. LogMessage::RemoveLogToStream(&stream);
  119. stream.Close();
  120. Filesystem::DeleteFile(path);
  121. LOG(LS_INFO) << "Average log time: " << TimeDiff(finish, start) << " us";
  122. }
  123. } // namespace talk_base