xref: /aosp_15_r20/external/abseil-cpp/absl/log/log_entry_test.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker //
2*9356374aSAndroid Build Coastguard Worker // Copyright 2022 The Abseil Authors.
3*9356374aSAndroid Build Coastguard Worker //
4*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*9356374aSAndroid Build Coastguard Worker //
8*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
9*9356374aSAndroid Build Coastguard Worker //
10*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*9356374aSAndroid Build Coastguard Worker // limitations under the License.
15*9356374aSAndroid Build Coastguard Worker 
16*9356374aSAndroid Build Coastguard Worker #include "absl/log/log_entry.h"
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <stddef.h>
19*9356374aSAndroid Build Coastguard Worker #include <stdint.h>
20*9356374aSAndroid Build Coastguard Worker 
21*9356374aSAndroid Build Coastguard Worker #include <cstring>
22*9356374aSAndroid Build Coastguard Worker #include <limits>
23*9356374aSAndroid Build Coastguard Worker #include <string>
24*9356374aSAndroid Build Coastguard Worker #include <type_traits>
25*9356374aSAndroid Build Coastguard Worker #include <utility>
26*9356374aSAndroid Build Coastguard Worker #include <vector>
27*9356374aSAndroid Build Coastguard Worker 
28*9356374aSAndroid Build Coastguard Worker #include "gmock/gmock.h"
29*9356374aSAndroid Build Coastguard Worker #include "gtest/gtest.h"
30*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
31*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
32*9356374aSAndroid Build Coastguard Worker #include "absl/base/log_severity.h"
33*9356374aSAndroid Build Coastguard Worker #include "absl/log/internal/append_truncated.h"
34*9356374aSAndroid Build Coastguard Worker #include "absl/log/internal/log_format.h"
35*9356374aSAndroid Build Coastguard Worker #include "absl/log/internal/test_helpers.h"
36*9356374aSAndroid Build Coastguard Worker #include "absl/strings/numbers.h"
37*9356374aSAndroid Build Coastguard Worker #include "absl/strings/str_split.h"
38*9356374aSAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
39*9356374aSAndroid Build Coastguard Worker #include "absl/time/civil_time.h"
40*9356374aSAndroid Build Coastguard Worker #include "absl/time/time.h"
41*9356374aSAndroid Build Coastguard Worker #include "absl/types/span.h"
42*9356374aSAndroid Build Coastguard Worker 
43*9356374aSAndroid Build Coastguard Worker namespace {
44*9356374aSAndroid Build Coastguard Worker using ::absl::log_internal::LogEntryTestPeer;
45*9356374aSAndroid Build Coastguard Worker using ::testing::Eq;
46*9356374aSAndroid Build Coastguard Worker using ::testing::IsTrue;
47*9356374aSAndroid Build Coastguard Worker using ::testing::StartsWith;
48*9356374aSAndroid Build Coastguard Worker using ::testing::StrEq;
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
51*9356374aSAndroid Build Coastguard Worker     new absl::log_internal::LogTestEnvironment);
52*9356374aSAndroid Build Coastguard Worker }  // namespace
53*9356374aSAndroid Build Coastguard Worker 
54*9356374aSAndroid Build Coastguard Worker namespace absl {
55*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
56*9356374aSAndroid Build Coastguard Worker namespace log_internal {
57*9356374aSAndroid Build Coastguard Worker 
58*9356374aSAndroid Build Coastguard Worker class LogEntryTestPeer {
59*9356374aSAndroid Build Coastguard Worker  public:
LogEntryTestPeer(absl::string_view base_filename,int line,bool prefix,absl::LogSeverity severity,absl::string_view timestamp,absl::LogEntry::tid_t tid,PrefixFormat format,absl::string_view text_message)60*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer(absl::string_view base_filename, int line, bool prefix,
61*9356374aSAndroid Build Coastguard Worker                    absl::LogSeverity severity, absl::string_view timestamp,
62*9356374aSAndroid Build Coastguard Worker                    absl::LogEntry::tid_t tid, PrefixFormat format,
63*9356374aSAndroid Build Coastguard Worker                    absl::string_view text_message)
64*9356374aSAndroid Build Coastguard Worker       : format_{format}, buf_(15000, '\0') {
65*9356374aSAndroid Build Coastguard Worker     entry_.base_filename_ = base_filename;
66*9356374aSAndroid Build Coastguard Worker     entry_.line_ = line;
67*9356374aSAndroid Build Coastguard Worker     entry_.prefix_ = prefix;
68*9356374aSAndroid Build Coastguard Worker     entry_.severity_ = severity;
69*9356374aSAndroid Build Coastguard Worker     std::string time_err;
70*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
71*9356374aSAndroid Build Coastguard Worker         absl::ParseTime("%Y-%m-%d%ET%H:%M:%E*S", timestamp,
72*9356374aSAndroid Build Coastguard Worker                         absl::LocalTimeZone(), &entry_.timestamp_, &time_err),
73*9356374aSAndroid Build Coastguard Worker         IsTrue())
74*9356374aSAndroid Build Coastguard Worker         << "Failed to parse time " << timestamp << ": " << time_err;
75*9356374aSAndroid Build Coastguard Worker     entry_.tid_ = tid;
76*9356374aSAndroid Build Coastguard Worker     std::pair<absl::string_view, std::string> timestamp_bits =
77*9356374aSAndroid Build Coastguard Worker         absl::StrSplit(timestamp, absl::ByChar('.'));
78*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(absl::ParseCivilTime(timestamp_bits.first, &ci_.cs), IsTrue())
79*9356374aSAndroid Build Coastguard Worker         << "Failed to parse time " << timestamp_bits.first;
80*9356374aSAndroid Build Coastguard Worker     timestamp_bits.second.resize(9, '0');
81*9356374aSAndroid Build Coastguard Worker     int64_t nanos = 0;
82*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(absl::SimpleAtoi(timestamp_bits.second, &nanos), IsTrue())
83*9356374aSAndroid Build Coastguard Worker         << "Failed to parse time " << timestamp_bits.first;
84*9356374aSAndroid Build Coastguard Worker     ci_.subsecond = absl::Nanoseconds(nanos);
85*9356374aSAndroid Build Coastguard Worker 
86*9356374aSAndroid Build Coastguard Worker     absl::Span<char> view = absl::MakeSpan(buf_);
87*9356374aSAndroid Build Coastguard Worker     view.remove_suffix(2);
88*9356374aSAndroid Build Coastguard Worker     entry_.prefix_len_ =
89*9356374aSAndroid Build Coastguard Worker         entry_.prefix_
90*9356374aSAndroid Build Coastguard Worker             ? log_internal::FormatLogPrefix(
91*9356374aSAndroid Build Coastguard Worker                   entry_.log_severity(), entry_.timestamp(), entry_.tid(),
92*9356374aSAndroid Build Coastguard Worker                   entry_.source_basename(), entry_.source_line(), format_, view)
93*9356374aSAndroid Build Coastguard Worker             : 0;
94*9356374aSAndroid Build Coastguard Worker 
95*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry_.prefix_len_,
96*9356374aSAndroid Build Coastguard Worker                 Eq(static_cast<size_t>(view.data() - buf_.data())));
97*9356374aSAndroid Build Coastguard Worker     log_internal::AppendTruncated(text_message, view);
98*9356374aSAndroid Build Coastguard Worker     view = absl::Span<char>(view.data(), view.size() + 2);
99*9356374aSAndroid Build Coastguard Worker     view[0] = '\n';
100*9356374aSAndroid Build Coastguard Worker     view[1] = '\0';
101*9356374aSAndroid Build Coastguard Worker     view.remove_prefix(2);
102*9356374aSAndroid Build Coastguard Worker     buf_.resize(static_cast<size_t>(view.data() - buf_.data()));
103*9356374aSAndroid Build Coastguard Worker     entry_.text_message_with_prefix_and_newline_and_nul_ = absl::MakeSpan(buf_);
104*9356374aSAndroid Build Coastguard Worker   }
105*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer(const LogEntryTestPeer&) = delete;
106*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer& operator=(const LogEntryTestPeer&) = delete;
107*9356374aSAndroid Build Coastguard Worker 
FormatLogMessage() const108*9356374aSAndroid Build Coastguard Worker   std::string FormatLogMessage() const {
109*9356374aSAndroid Build Coastguard Worker     return log_internal::FormatLogMessage(
110*9356374aSAndroid Build Coastguard Worker         entry_.log_severity(), ci_.cs, ci_.subsecond, entry_.tid(),
111*9356374aSAndroid Build Coastguard Worker         entry_.source_basename(), entry_.source_line(), format_,
112*9356374aSAndroid Build Coastguard Worker         entry_.text_message());
113*9356374aSAndroid Build Coastguard Worker   }
FormatPrefixIntoSizedBuffer(size_t sz)114*9356374aSAndroid Build Coastguard Worker   std::string FormatPrefixIntoSizedBuffer(size_t sz) {
115*9356374aSAndroid Build Coastguard Worker     std::string str(sz, '\0');
116*9356374aSAndroid Build Coastguard Worker     absl::Span<char> buf(&str[0], str.size());
117*9356374aSAndroid Build Coastguard Worker     const size_t prefix_size = log_internal::FormatLogPrefix(
118*9356374aSAndroid Build Coastguard Worker         entry_.log_severity(), entry_.timestamp(), entry_.tid(),
119*9356374aSAndroid Build Coastguard Worker         entry_.source_basename(), entry_.source_line(), format_, buf);
120*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(prefix_size, Eq(static_cast<size_t>(buf.data() - str.data())));
121*9356374aSAndroid Build Coastguard Worker     str.resize(prefix_size);
122*9356374aSAndroid Build Coastguard Worker     return str;
123*9356374aSAndroid Build Coastguard Worker   }
entry() const124*9356374aSAndroid Build Coastguard Worker   const absl::LogEntry& entry() const { return entry_; }
125*9356374aSAndroid Build Coastguard Worker 
126*9356374aSAndroid Build Coastguard Worker  private:
127*9356374aSAndroid Build Coastguard Worker   absl::LogEntry entry_;
128*9356374aSAndroid Build Coastguard Worker   PrefixFormat format_;
129*9356374aSAndroid Build Coastguard Worker   absl::TimeZone::CivilInfo ci_;
130*9356374aSAndroid Build Coastguard Worker   std::vector<char> buf_;
131*9356374aSAndroid Build Coastguard Worker };
132*9356374aSAndroid Build Coastguard Worker 
133*9356374aSAndroid Build Coastguard Worker }  // namespace log_internal
134*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
135*9356374aSAndroid Build Coastguard Worker }  // namespace absl
136*9356374aSAndroid Build Coastguard Worker 
137*9356374aSAndroid Build Coastguard Worker namespace {
138*9356374aSAndroid Build Coastguard Worker constexpr bool kUsePrefix = true, kNoPrefix = false;
139*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,Baseline)140*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, Baseline) {
141*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer entry("foo.cc", 1234, kUsePrefix, absl::LogSeverity::kInfo,
142*9356374aSAndroid Build Coastguard Worker                          "2020-01-02T03:04:05.6789", 451,
143*9356374aSAndroid Build Coastguard Worker                          absl::log_internal::PrefixFormat::kNotRaw,
144*9356374aSAndroid Build Coastguard Worker                          "hello world");
145*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatLogMessage(),
146*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] hello world"));
147*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
148*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] "));
149*9356374aSAndroid Build Coastguard Worker   for (size_t sz = strlen("I0102 03:04:05.678900     451 foo.cc:1234] ") + 20;
150*9356374aSAndroid Build Coastguard Worker        sz != std::numeric_limits<size_t>::max(); sz--)
151*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT("I0102 03:04:05.678900     451 foo.cc:1234] ",
152*9356374aSAndroid Build Coastguard Worker                 StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
153*9356374aSAndroid Build Coastguard Worker 
154*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix_and_newline(),
155*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] hello world\n"));
156*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
157*9356374aSAndroid Build Coastguard Worker       entry.entry().text_message_with_prefix_and_newline_c_str(),
158*9356374aSAndroid Build Coastguard Worker       StrEq("I0102 03:04:05.678900     451 foo.cc:1234] hello world\n"));
159*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix(),
160*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] hello world"));
161*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message(), Eq("hello world"));
162*9356374aSAndroid Build Coastguard Worker }
163*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,NoPrefix)164*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, NoPrefix) {
165*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer entry("foo.cc", 1234, kNoPrefix, absl::LogSeverity::kInfo,
166*9356374aSAndroid Build Coastguard Worker                          "2020-01-02T03:04:05.6789", 451,
167*9356374aSAndroid Build Coastguard Worker                          absl::log_internal::PrefixFormat::kNotRaw,
168*9356374aSAndroid Build Coastguard Worker                          "hello world");
169*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatLogMessage(),
170*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] hello world"));
171*9356374aSAndroid Build Coastguard Worker   // These methods are not responsible for honoring `prefix()`.
172*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
173*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] "));
174*9356374aSAndroid Build Coastguard Worker   for (size_t sz = strlen("I0102 03:04:05.678900     451 foo.cc:1234] ") + 20;
175*9356374aSAndroid Build Coastguard Worker        sz != std::numeric_limits<size_t>::max(); sz--)
176*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT("I0102 03:04:05.678900     451 foo.cc:1234] ",
177*9356374aSAndroid Build Coastguard Worker                 StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
178*9356374aSAndroid Build Coastguard Worker 
179*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix_and_newline(),
180*9356374aSAndroid Build Coastguard Worker               Eq("hello world\n"));
181*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix_and_newline_c_str(),
182*9356374aSAndroid Build Coastguard Worker               StrEq("hello world\n"));
183*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix(), Eq("hello world"));
184*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message(), Eq("hello world"));
185*9356374aSAndroid Build Coastguard Worker }
186*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,EmptyFields)187*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, EmptyFields) {
188*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer entry("", 0, kUsePrefix, absl::LogSeverity::kInfo,
189*9356374aSAndroid Build Coastguard Worker                          "2020-01-02T03:04:05", 0,
190*9356374aSAndroid Build Coastguard Worker                          absl::log_internal::PrefixFormat::kNotRaw, "");
191*9356374aSAndroid Build Coastguard Worker   const std::string format_message = entry.FormatLogMessage();
192*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(format_message, Eq("I0102 03:04:05.000000       0 :0] "));
193*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000), Eq(format_message));
194*9356374aSAndroid Build Coastguard Worker   for (size_t sz = format_message.size() + 20;
195*9356374aSAndroid Build Coastguard Worker        sz != std::numeric_limits<size_t>::max(); sz--)
196*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(format_message,
197*9356374aSAndroid Build Coastguard Worker                 StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
198*9356374aSAndroid Build Coastguard Worker 
199*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix_and_newline(),
200*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.000000       0 :0] \n"));
201*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix_and_newline_c_str(),
202*9356374aSAndroid Build Coastguard Worker               StrEq("I0102 03:04:05.000000       0 :0] \n"));
203*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix(),
204*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.000000       0 :0] "));
205*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message(), Eq(""));
206*9356374aSAndroid Build Coastguard Worker }
207*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,NegativeFields)208*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, NegativeFields) {
209*9356374aSAndroid Build Coastguard Worker   // When Abseil's minimum C++ version is C++17, this conditional can be
210*9356374aSAndroid Build Coastguard Worker   // converted to a constexpr if and the static_cast below removed.
211*9356374aSAndroid Build Coastguard Worker   if (std::is_signed<absl::LogEntry::tid_t>::value) {
212*9356374aSAndroid Build Coastguard Worker     LogEntryTestPeer entry(
213*9356374aSAndroid Build Coastguard Worker         "foo.cc", -1234, kUsePrefix, absl::LogSeverity::kInfo,
214*9356374aSAndroid Build Coastguard Worker         "2020-01-02T03:04:05.6789", static_cast<absl::LogEntry::tid_t>(-451),
215*9356374aSAndroid Build Coastguard Worker         absl::log_internal::PrefixFormat::kNotRaw, "hello world");
216*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.FormatLogMessage(),
217*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678900    -451 foo.cc:-1234] hello world"));
218*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
219*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678900    -451 foo.cc:-1234] "));
220*9356374aSAndroid Build Coastguard Worker     for (size_t sz =
221*9356374aSAndroid Build Coastguard Worker              strlen("I0102 03:04:05.678900    -451 foo.cc:-1234] ") + 20;
222*9356374aSAndroid Build Coastguard Worker          sz != std::numeric_limits<size_t>::max(); sz--)
223*9356374aSAndroid Build Coastguard Worker       EXPECT_THAT("I0102 03:04:05.678900    -451 foo.cc:-1234] ",
224*9356374aSAndroid Build Coastguard Worker                   StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
225*9356374aSAndroid Build Coastguard Worker 
226*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
227*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline(),
228*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678900    -451 foo.cc:-1234] hello world\n"));
229*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
230*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline_c_str(),
231*9356374aSAndroid Build Coastguard Worker         StrEq("I0102 03:04:05.678900    -451 foo.cc:-1234] hello world\n"));
232*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.entry().text_message_with_prefix(),
233*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678900    -451 foo.cc:-1234] hello world"));
234*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.entry().text_message(), Eq("hello world"));
235*9356374aSAndroid Build Coastguard Worker   } else {
236*9356374aSAndroid Build Coastguard Worker     LogEntryTestPeer entry("foo.cc", -1234, kUsePrefix,
237*9356374aSAndroid Build Coastguard Worker                            absl::LogSeverity::kInfo, "2020-01-02T03:04:05.6789",
238*9356374aSAndroid Build Coastguard Worker                            451, absl::log_internal::PrefixFormat::kNotRaw,
239*9356374aSAndroid Build Coastguard Worker                            "hello world");
240*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.FormatLogMessage(),
241*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678900     451 foo.cc:-1234] hello world"));
242*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
243*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678900     451 foo.cc:-1234] "));
244*9356374aSAndroid Build Coastguard Worker     for (size_t sz =
245*9356374aSAndroid Build Coastguard Worker              strlen("I0102 03:04:05.678900     451 foo.cc:-1234] ") + 20;
246*9356374aSAndroid Build Coastguard Worker          sz != std::numeric_limits<size_t>::max(); sz--)
247*9356374aSAndroid Build Coastguard Worker       EXPECT_THAT("I0102 03:04:05.678900     451 foo.cc:-1234] ",
248*9356374aSAndroid Build Coastguard Worker                   StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
249*9356374aSAndroid Build Coastguard Worker 
250*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
251*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline(),
252*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678900     451 foo.cc:-1234] hello world\n"));
253*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
254*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline_c_str(),
255*9356374aSAndroid Build Coastguard Worker         StrEq("I0102 03:04:05.678900     451 foo.cc:-1234] hello world\n"));
256*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.entry().text_message_with_prefix(),
257*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678900     451 foo.cc:-1234] hello world"));
258*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.entry().text_message(), Eq("hello world"));
259*9356374aSAndroid Build Coastguard Worker   }
260*9356374aSAndroid Build Coastguard Worker }
261*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,LongFields)262*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, LongFields) {
263*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer entry(
264*9356374aSAndroid Build Coastguard Worker       "I am the very model of a modern Major-General / "
265*9356374aSAndroid Build Coastguard Worker       "I've information vegetable, animal, and mineral.",
266*9356374aSAndroid Build Coastguard Worker       2147483647, kUsePrefix, absl::LogSeverity::kInfo,
267*9356374aSAndroid Build Coastguard Worker       "2020-01-02T03:04:05.678967896789", 2147483647,
268*9356374aSAndroid Build Coastguard Worker       absl::log_internal::PrefixFormat::kNotRaw,
269*9356374aSAndroid Build Coastguard Worker       "I know the kings of England, and I quote the fights historical / "
270*9356374aSAndroid Build Coastguard Worker       "From Marathon to Waterloo, in order categorical.");
271*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatLogMessage(),
272*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
273*9356374aSAndroid Build Coastguard Worker                  "modern Major-General / I've information vegetable, animal, "
274*9356374aSAndroid Build Coastguard Worker                  "and mineral.:2147483647] I know the kings of England, and I "
275*9356374aSAndroid Build Coastguard Worker                  "quote the fights historical / From Marathon to Waterloo, in "
276*9356374aSAndroid Build Coastguard Worker                  "order categorical."));
277*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
278*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
279*9356374aSAndroid Build Coastguard Worker                  "modern Major-General / I've information vegetable, animal, "
280*9356374aSAndroid Build Coastguard Worker                  "and mineral.:2147483647] "));
281*9356374aSAndroid Build Coastguard Worker   for (size_t sz =
282*9356374aSAndroid Build Coastguard Worker            strlen("I0102 03:04:05.678967 2147483647 I am the very model of a "
283*9356374aSAndroid Build Coastguard Worker                   "modern Major-General / I've information vegetable, animal, "
284*9356374aSAndroid Build Coastguard Worker                   "and mineral.:2147483647] ") +
285*9356374aSAndroid Build Coastguard Worker            20;
286*9356374aSAndroid Build Coastguard Worker        sz != std::numeric_limits<size_t>::max(); sz--)
287*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
288*9356374aSAndroid Build Coastguard Worker         "I0102 03:04:05.678967 2147483647 I am the very model of a "
289*9356374aSAndroid Build Coastguard Worker         "modern Major-General / I've information vegetable, animal, "
290*9356374aSAndroid Build Coastguard Worker         "and mineral.:2147483647] ",
291*9356374aSAndroid Build Coastguard Worker         StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
292*9356374aSAndroid Build Coastguard Worker 
293*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix_and_newline(),
294*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
295*9356374aSAndroid Build Coastguard Worker                  "modern Major-General / I've information vegetable, animal, "
296*9356374aSAndroid Build Coastguard Worker                  "and mineral.:2147483647] I know the kings of England, and I "
297*9356374aSAndroid Build Coastguard Worker                  "quote the fights historical / From Marathon to Waterloo, in "
298*9356374aSAndroid Build Coastguard Worker                  "order categorical.\n"));
299*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
300*9356374aSAndroid Build Coastguard Worker       entry.entry().text_message_with_prefix_and_newline_c_str(),
301*9356374aSAndroid Build Coastguard Worker       StrEq("I0102 03:04:05.678967 2147483647 I am the very model of a "
302*9356374aSAndroid Build Coastguard Worker             "modern Major-General / I've information vegetable, animal, "
303*9356374aSAndroid Build Coastguard Worker             "and mineral.:2147483647] I know the kings of England, and I "
304*9356374aSAndroid Build Coastguard Worker             "quote the fights historical / From Marathon to Waterloo, in "
305*9356374aSAndroid Build Coastguard Worker             "order categorical.\n"));
306*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message_with_prefix(),
307*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
308*9356374aSAndroid Build Coastguard Worker                  "modern Major-General / I've information vegetable, animal, "
309*9356374aSAndroid Build Coastguard Worker                  "and mineral.:2147483647] I know the kings of England, and I "
310*9356374aSAndroid Build Coastguard Worker                  "quote the fights historical / From Marathon to Waterloo, in "
311*9356374aSAndroid Build Coastguard Worker                  "order categorical."));
312*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
313*9356374aSAndroid Build Coastguard Worker       entry.entry().text_message(),
314*9356374aSAndroid Build Coastguard Worker       Eq("I know the kings of England, and I quote the fights historical / "
315*9356374aSAndroid Build Coastguard Worker          "From Marathon to Waterloo, in order categorical."));
316*9356374aSAndroid Build Coastguard Worker }
317*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,LongNegativeFields)318*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, LongNegativeFields) {
319*9356374aSAndroid Build Coastguard Worker   // When Abseil's minimum C++ version is C++17, this conditional can be
320*9356374aSAndroid Build Coastguard Worker   // converted to a constexpr if and the static_cast below removed.
321*9356374aSAndroid Build Coastguard Worker   if (std::is_signed<absl::LogEntry::tid_t>::value) {
322*9356374aSAndroid Build Coastguard Worker     LogEntryTestPeer entry(
323*9356374aSAndroid Build Coastguard Worker         "I am the very model of a modern Major-General / "
324*9356374aSAndroid Build Coastguard Worker         "I've information vegetable, animal, and mineral.",
325*9356374aSAndroid Build Coastguard Worker         -2147483647, kUsePrefix, absl::LogSeverity::kInfo,
326*9356374aSAndroid Build Coastguard Worker         "2020-01-02T03:04:05.678967896789",
327*9356374aSAndroid Build Coastguard Worker         static_cast<absl::LogEntry::tid_t>(-2147483647),
328*9356374aSAndroid Build Coastguard Worker         absl::log_internal::PrefixFormat::kNotRaw,
329*9356374aSAndroid Build Coastguard Worker         "I know the kings of England, and I quote the fights historical / "
330*9356374aSAndroid Build Coastguard Worker         "From Marathon to Waterloo, in order categorical.");
331*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
332*9356374aSAndroid Build Coastguard Worker         entry.FormatLogMessage(),
333*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678967 -2147483647 I am the very model of a "
334*9356374aSAndroid Build Coastguard Worker            "modern Major-General / I've information vegetable, animal, "
335*9356374aSAndroid Build Coastguard Worker            "and mineral.:-2147483647] I know the kings of England, and I "
336*9356374aSAndroid Build Coastguard Worker            "quote the fights historical / From Marathon to Waterloo, in "
337*9356374aSAndroid Build Coastguard Worker            "order categorical."));
338*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
339*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678967 -2147483647 I am the very model of a "
340*9356374aSAndroid Build Coastguard Worker                    "modern Major-General / I've information vegetable, animal, "
341*9356374aSAndroid Build Coastguard Worker                    "and mineral.:-2147483647] "));
342*9356374aSAndroid Build Coastguard Worker     for (size_t sz =
343*9356374aSAndroid Build Coastguard Worker              strlen(
344*9356374aSAndroid Build Coastguard Worker                  "I0102 03:04:05.678967 -2147483647 I am the very model of a "
345*9356374aSAndroid Build Coastguard Worker                  "modern Major-General / I've information vegetable, animal, "
346*9356374aSAndroid Build Coastguard Worker                  "and mineral.:-2147483647] ") +
347*9356374aSAndroid Build Coastguard Worker              20;
348*9356374aSAndroid Build Coastguard Worker          sz != std::numeric_limits<size_t>::max(); sz--)
349*9356374aSAndroid Build Coastguard Worker       EXPECT_THAT(
350*9356374aSAndroid Build Coastguard Worker           "I0102 03:04:05.678967 -2147483647 I am the very model of a "
351*9356374aSAndroid Build Coastguard Worker           "modern Major-General / I've information vegetable, animal, "
352*9356374aSAndroid Build Coastguard Worker           "and mineral.:-2147483647] ",
353*9356374aSAndroid Build Coastguard Worker           StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
354*9356374aSAndroid Build Coastguard Worker 
355*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
356*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline(),
357*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678967 -2147483647 I am the very model of a "
358*9356374aSAndroid Build Coastguard Worker            "modern Major-General / I've information vegetable, animal, "
359*9356374aSAndroid Build Coastguard Worker            "and mineral.:-2147483647] I know the kings of England, and I "
360*9356374aSAndroid Build Coastguard Worker            "quote the fights historical / From Marathon to Waterloo, in "
361*9356374aSAndroid Build Coastguard Worker            "order categorical.\n"));
362*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
363*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline_c_str(),
364*9356374aSAndroid Build Coastguard Worker         StrEq("I0102 03:04:05.678967 -2147483647 I am the very model of a "
365*9356374aSAndroid Build Coastguard Worker               "modern Major-General / I've information vegetable, animal, "
366*9356374aSAndroid Build Coastguard Worker               "and mineral.:-2147483647] I know the kings of England, and I "
367*9356374aSAndroid Build Coastguard Worker               "quote the fights historical / From Marathon to Waterloo, in "
368*9356374aSAndroid Build Coastguard Worker               "order categorical.\n"));
369*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
370*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix(),
371*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678967 -2147483647 I am the very model of a "
372*9356374aSAndroid Build Coastguard Worker            "modern Major-General / I've information vegetable, animal, "
373*9356374aSAndroid Build Coastguard Worker            "and mineral.:-2147483647] I know the kings of England, and I "
374*9356374aSAndroid Build Coastguard Worker            "quote the fights historical / From Marathon to Waterloo, in "
375*9356374aSAndroid Build Coastguard Worker            "order categorical."));
376*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
377*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message(),
378*9356374aSAndroid Build Coastguard Worker         Eq("I know the kings of England, and I quote the fights historical / "
379*9356374aSAndroid Build Coastguard Worker            "From Marathon to Waterloo, in order categorical."));
380*9356374aSAndroid Build Coastguard Worker   } else {
381*9356374aSAndroid Build Coastguard Worker     LogEntryTestPeer entry(
382*9356374aSAndroid Build Coastguard Worker         "I am the very model of a modern Major-General / "
383*9356374aSAndroid Build Coastguard Worker         "I've information vegetable, animal, and mineral.",
384*9356374aSAndroid Build Coastguard Worker         -2147483647, kUsePrefix, absl::LogSeverity::kInfo,
385*9356374aSAndroid Build Coastguard Worker         "2020-01-02T03:04:05.678967896789", 2147483647,
386*9356374aSAndroid Build Coastguard Worker         absl::log_internal::PrefixFormat::kNotRaw,
387*9356374aSAndroid Build Coastguard Worker         "I know the kings of England, and I quote the fights historical / "
388*9356374aSAndroid Build Coastguard Worker         "From Marathon to Waterloo, in order categorical.");
389*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
390*9356374aSAndroid Build Coastguard Worker         entry.FormatLogMessage(),
391*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
392*9356374aSAndroid Build Coastguard Worker            "modern Major-General / I've information vegetable, animal, "
393*9356374aSAndroid Build Coastguard Worker            "and mineral.:-2147483647] I know the kings of England, and I "
394*9356374aSAndroid Build Coastguard Worker            "quote the fights historical / From Marathon to Waterloo, in "
395*9356374aSAndroid Build Coastguard Worker            "order categorical."));
396*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
397*9356374aSAndroid Build Coastguard Worker                 Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
398*9356374aSAndroid Build Coastguard Worker                    "modern Major-General / I've information vegetable, animal, "
399*9356374aSAndroid Build Coastguard Worker                    "and mineral.:-2147483647] "));
400*9356374aSAndroid Build Coastguard Worker     for (size_t sz =
401*9356374aSAndroid Build Coastguard Worker              strlen(
402*9356374aSAndroid Build Coastguard Worker                  "I0102 03:04:05.678967 2147483647 I am the very model of a "
403*9356374aSAndroid Build Coastguard Worker                  "modern Major-General / I've information vegetable, animal, "
404*9356374aSAndroid Build Coastguard Worker                  "and mineral.:-2147483647] ") +
405*9356374aSAndroid Build Coastguard Worker              20;
406*9356374aSAndroid Build Coastguard Worker          sz != std::numeric_limits<size_t>::max(); sz--)
407*9356374aSAndroid Build Coastguard Worker       EXPECT_THAT(
408*9356374aSAndroid Build Coastguard Worker           "I0102 03:04:05.678967 2147483647 I am the very model of a "
409*9356374aSAndroid Build Coastguard Worker           "modern Major-General / I've information vegetable, animal, "
410*9356374aSAndroid Build Coastguard Worker           "and mineral.:-2147483647] ",
411*9356374aSAndroid Build Coastguard Worker           StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
412*9356374aSAndroid Build Coastguard Worker 
413*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
414*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline(),
415*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
416*9356374aSAndroid Build Coastguard Worker            "modern Major-General / I've information vegetable, animal, "
417*9356374aSAndroid Build Coastguard Worker            "and mineral.:-2147483647] I know the kings of England, and I "
418*9356374aSAndroid Build Coastguard Worker            "quote the fights historical / From Marathon to Waterloo, in "
419*9356374aSAndroid Build Coastguard Worker            "order categorical.\n"));
420*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
421*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix_and_newline_c_str(),
422*9356374aSAndroid Build Coastguard Worker         StrEq("I0102 03:04:05.678967 2147483647 I am the very model of a "
423*9356374aSAndroid Build Coastguard Worker               "modern Major-General / I've information vegetable, animal, "
424*9356374aSAndroid Build Coastguard Worker               "and mineral.:-2147483647] I know the kings of England, and I "
425*9356374aSAndroid Build Coastguard Worker               "quote the fights historical / From Marathon to Waterloo, in "
426*9356374aSAndroid Build Coastguard Worker               "order categorical.\n"));
427*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
428*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message_with_prefix(),
429*9356374aSAndroid Build Coastguard Worker         Eq("I0102 03:04:05.678967 2147483647 I am the very model of a "
430*9356374aSAndroid Build Coastguard Worker            "modern Major-General / I've information vegetable, animal, "
431*9356374aSAndroid Build Coastguard Worker            "and mineral.:-2147483647] I know the kings of England, and I "
432*9356374aSAndroid Build Coastguard Worker            "quote the fights historical / From Marathon to Waterloo, in "
433*9356374aSAndroid Build Coastguard Worker            "order categorical."));
434*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT(
435*9356374aSAndroid Build Coastguard Worker         entry.entry().text_message(),
436*9356374aSAndroid Build Coastguard Worker         Eq("I know the kings of England, and I quote the fights historical / "
437*9356374aSAndroid Build Coastguard Worker            "From Marathon to Waterloo, in order categorical."));
438*9356374aSAndroid Build Coastguard Worker   }
439*9356374aSAndroid Build Coastguard Worker }
440*9356374aSAndroid Build Coastguard Worker 
TEST(LogEntryTest,Raw)441*9356374aSAndroid Build Coastguard Worker TEST(LogEntryTest, Raw) {
442*9356374aSAndroid Build Coastguard Worker   LogEntryTestPeer entry("foo.cc", 1234, kUsePrefix, absl::LogSeverity::kInfo,
443*9356374aSAndroid Build Coastguard Worker                          "2020-01-02T03:04:05.6789", 451,
444*9356374aSAndroid Build Coastguard Worker                          absl::log_internal::PrefixFormat::kRaw, "hello world");
445*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
446*9356374aSAndroid Build Coastguard Worker       entry.FormatLogMessage(),
447*9356374aSAndroid Build Coastguard Worker       Eq("I0102 03:04:05.678900     451 foo.cc:1234] RAW: hello world"));
448*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.FormatPrefixIntoSizedBuffer(1000),
449*9356374aSAndroid Build Coastguard Worker               Eq("I0102 03:04:05.678900     451 foo.cc:1234] RAW: "));
450*9356374aSAndroid Build Coastguard Worker   for (size_t sz =
451*9356374aSAndroid Build Coastguard Worker            strlen("I0102 03:04:05.678900     451 foo.cc:1234] RAW: ") + 20;
452*9356374aSAndroid Build Coastguard Worker        sz != std::numeric_limits<size_t>::max(); sz--)
453*9356374aSAndroid Build Coastguard Worker     EXPECT_THAT("I0102 03:04:05.678900     451 foo.cc:1234] RAW: ",
454*9356374aSAndroid Build Coastguard Worker                 StartsWith(entry.FormatPrefixIntoSizedBuffer(sz)));
455*9356374aSAndroid Build Coastguard Worker 
456*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
457*9356374aSAndroid Build Coastguard Worker       entry.entry().text_message_with_prefix_and_newline(),
458*9356374aSAndroid Build Coastguard Worker       Eq("I0102 03:04:05.678900     451 foo.cc:1234] RAW: hello world\n"));
459*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
460*9356374aSAndroid Build Coastguard Worker       entry.entry().text_message_with_prefix_and_newline_c_str(),
461*9356374aSAndroid Build Coastguard Worker       StrEq("I0102 03:04:05.678900     451 foo.cc:1234] RAW: hello world\n"));
462*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(
463*9356374aSAndroid Build Coastguard Worker       entry.entry().text_message_with_prefix(),
464*9356374aSAndroid Build Coastguard Worker       Eq("I0102 03:04:05.678900     451 foo.cc:1234] RAW: hello world"));
465*9356374aSAndroid Build Coastguard Worker   EXPECT_THAT(entry.entry().text_message(), Eq("hello world"));
466*9356374aSAndroid Build Coastguard Worker }
467*9356374aSAndroid Build Coastguard Worker 
468*9356374aSAndroid Build Coastguard Worker }  // namespace
469