simplelog_tests.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2017 The Android Open Source Project
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_errorlog_tests"
  18. #include <audio_utils/SimpleLog.h>
  19. #include <gtest/gtest.h>
  20. #include <iostream>
  21. #include <log/log.h>
  22. using namespace android;
  23. static size_t countNewLines(const std::string &s) {
  24. return std::count(s.begin(), s.end(), '\n');
  25. }
  26. TEST(audio_utils_simplelog, basic) {
  27. auto slog = std::make_unique<SimpleLog>();
  28. const int64_t oneSecond = 1000000000;
  29. EXPECT_EQ((size_t)0, countNewLines(slog->dumpToString()));
  30. const int nine = 9;
  31. slog->log("Hello %d", nine);
  32. slog->log("World");
  33. slog->logs(-1 /* nowNs */, std::string("ABC")); // may take a std::string as well
  34. // two lines (no header)
  35. EXPECT_EQ((size_t)3, countNewLines(slog->dumpToString()));
  36. // another two lines (this is out of time order, but the log doesn't care)
  37. slog->log(oneSecond /* nowNs */, "Hello World %d", 10);
  38. slog->log(oneSecond * 2 /* nowNs */, "%s", "Goodbye");
  39. EXPECT_EQ((size_t)5, countNewLines(slog->dumpToString()));
  40. // truncate on lines
  41. EXPECT_EQ((size_t)1, countNewLines(slog->dumpToString("" /* prefix */, 1 /* lines */)));
  42. // truncate on time
  43. EXPECT_EQ((size_t)5, countNewLines(
  44. slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond /* limitNs */)));
  45. // truncate on time (more)
  46. EXPECT_EQ((size_t)4, countNewLines(
  47. slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 /* limitNs */)));
  48. // truncate on time (more)
  49. EXPECT_EQ((size_t)3, countNewLines(
  50. slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 + 1 /* limitNs */)));
  51. std::cout << slog->dumpToString() << std::flush;
  52. slog->dump(0 /* fd (stdout) */, " "); // add a prefix
  53. // The output below depends on the local time zone and current time.
  54. // The indentation below is exact, check alignment.
  55. /*
  56. 08-28 11:11:30.057 Hello 9
  57. 08-28 11:11:30.057 World
  58. 08-28 11:11:30.057 ABC
  59. 12-31 16:00:01.000 Hello World 10
  60. 12-31 16:00:02.000 Goodbye
  61. 08-28 11:11:30.057 Hello 9
  62. 08-28 11:11:30.057 World
  63. 08-28 11:11:30.057 ABC
  64. 12-31 16:00:01.000 Hello World 10
  65. 12-31 16:00:02.000 Goodbye
  66. */
  67. }