2 * Copyright 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_errorlog_tests"
20 #include <audio_utils/SimpleLog.h>
21 #include <gtest/gtest.h>
25 using namespace android;
27 static size_t countNewLines(const std::string &s) {
28 return std::count(s.begin(), s.end(), '\n');
31 TEST(audio_utils_simplelog, basic) {
32 auto slog = std::make_unique<SimpleLog>();
33 const int64_t oneSecond = 1000000000;
35 EXPECT_EQ((size_t)0, countNewLines(slog->dumpToString()));
38 slog->log("Hello %d", nine);
41 // two lines (no header)
42 EXPECT_EQ((size_t)2, countNewLines(slog->dumpToString()));
44 // another two lines (this is out of time order, but the log doesn't care)
45 slog->log(oneSecond /* nowNs */, "Hello World %d", 10);
46 slog->log(oneSecond * 2 /* nowNs */, "%s", "Goodbye");
48 EXPECT_EQ((size_t)4, countNewLines(slog->dumpToString()));
51 EXPECT_EQ((size_t)1, countNewLines(slog->dumpToString("" /* prefix */, 1 /* lines */)));
54 EXPECT_EQ((size_t)4, countNewLines(
55 slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond /* limitNs */)));
57 // truncate on time (more)
58 EXPECT_EQ((size_t)3, countNewLines(
59 slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 /* limitNs */)));
61 // truncate on time (more)
62 EXPECT_EQ((size_t)2, countNewLines(
63 slog->dumpToString("" /* prefix */, 0 /* lines */, oneSecond * 2 + 1 /* limitNs */)));
65 std::cout << slog->dumpToString() << std::flush;
67 slog->dump(0 /* fd (stdout) */, " "); // add a prefix
69 // The output below depends on the local time zone and current time.
70 // The indentation below is exact, check alignment.
72 03-27 14:47:43.567 Hello 9
73 03-27 14:47:43.567 World
74 12-31 16:00:01.000 Hello World 10
75 12-31 16:00:02.000 Goodbye
76 03-27 14:47:43.567 Hello 9
77 03-27 14:47:43.567 World
78 12-31 16:00:01.000 Hello World 10
79 12-31 16:00:02.000 Goodbye