• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Formats log message based on provided data.
42 std::string FormatLogMessage(absl::LogSeverity severity,
43                              absl::CivilSecond civil_second,
44                              absl::Duration subsecond, log_internal::Tid tid,
45                              absl::string_view basename, int line,
46                              absl::string_view message);
47 
48 // Formats various entry metadata into a text string meant for use as a
49 // prefix on a log message string.  Writes into `buf`, advances `buf` to point
50 // at the remainder of the buffer (i.e. past any written bytes), and returns the
51 // number of bytes written.
52 //
53 // In addition to calling `buf->remove_prefix()` (or the equivalent), this
54 // function may also do `buf->remove_suffix(buf->size())` in cases where no more
55 // bytes (i.e. no message data) should be written into the buffer.  For example,
56 // if the prefix ought to be:
57 //   I0926 09:00:00.000000 1234567 foo.cc:123]
58 // `buf` is too small, the function might fill the whole buffer:
59 //   I0926 09:00:00.000000 1234
60 // (note the apparrently incorrect thread ID), or it might write less:
61 //   I0926 09:00:00.000000
62 // In this case, it might also empty `buf` prior to returning to prevent
63 // message data from being written into the space where a reader would expect to
64 // see a thread ID.
65 size_t FormatLogPrefix(absl::LogSeverity severity, absl::Time timestamp,
66                        log_internal::Tid tid, absl::string_view basename,
67                        int line, absl::Span<char>& buf);
68 
69 }  // namespace log_internal
70 ABSL_NAMESPACE_END
71 }  // namespace absl
72 
73 #endif  // ABSL_LOG_INTERNAL_LOG_FORMAT_H_
74