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