1 // Copyright 2022 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: log/internal/log_format.h
17 // -----------------------------------------------------------------------------
18 //
19 // This file declares routines implementing formatting of log message and log
20 // prefix.
21 
22 #ifndef ABSL_LOG_INTERNAL_LOG_FORMAT_H_
23 #define ABSL_LOG_INTERNAL_LOG_FORMAT_H_
24 
25 #include <stddef.h>
26 
27 #include <string>
28 
29 #include "absl/base/config.h"
30 #include "absl/base/log_severity.h"
31 #include "absl/log/internal/config.h"
32 #include "absl/strings/string_view.h"
33 #include "absl/time/civil_time.h"
34 #include "absl/time/time.h"
35 #include "absl/types/span.h"
36 
37 namespace absl {
38 ABSL_NAMESPACE_BEGIN
39 namespace log_internal {
40 
41 enum class PrefixFormat {
42   kNotRaw,
43   kRaw,
44 };
45 
46 // Formats log message based on provided data.
47 std::string FormatLogMessage(absl::LogSeverity severity,
48                              absl::CivilSecond civil_second,
49                              absl::Duration subsecond, log_internal::Tid tid,
50                              absl::string_view basename, int line,
51                              PrefixFormat format, absl::string_view message);
52 
53 // Formats various entry metadata into a text string meant for use as a
54 // prefix on a log message string.  Writes into `buf`, advances `buf` to point
55 // at the remainder of the buffer (i.e. past any written bytes), and returns the
56 // number of bytes written.
57 //
58 // In addition to calling `buf->remove_prefix()` (or the equivalent), this
59 // function may also do `buf->remove_suffix(buf->size())` in cases where no more
60 // bytes (i.e. no message data) should be written into the buffer.  For example,
61 // if the prefix ought to be:
62 //   I0926 09:00:00.000000 1234567 foo.cc:123]
63 // `buf` is too small, the function might fill the whole buffer:
64 //   I0926 09:00:00.000000 1234
65 // (note the apparrently incorrect thread ID), or it might write less:
66 //   I0926 09:00:00.000000
67 // In this case, it might also empty `buf` prior to returning to prevent
68 // message data from being written into the space where a reader would expect to
69 // see a thread ID.
70 size_t FormatLogPrefix(absl::LogSeverity severity, absl::Time timestamp,
71                        log_internal::Tid tid, absl::string_view basename,
72                        int line, PrefixFormat format, absl::Span<char>& buf);
73 
74 }  // namespace log_internal
75 ABSL_NAMESPACE_END
76 }  // namespace absl
77 
78 #endif  // ABSL_LOG_INTERNAL_LOG_FORMAT_H_
79