log_format.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef ABSL_LOG_INTERNAL_LOG_FORMAT_H_
  22. #define ABSL_LOG_INTERNAL_LOG_FORMAT_H_
  23. #include <stddef.h>
  24. #include <string>
  25. #include "absl/base/config.h"
  26. #include "absl/base/log_severity.h"
  27. #include "absl/log/internal/config.h"
  28. #include "absl/strings/string_view.h"
  29. #include "absl/time/civil_time.h"
  30. #include "absl/time/time.h"
  31. #include "absl/types/span.h"
  32. namespace absl {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace log_internal {
  35. enum class PrefixFormat {
  36. kNotRaw,
  37. kRaw,
  38. };
  39. // Formats log message based on provided data.
  40. std::string FormatLogMessage(absl::LogSeverity severity,
  41. absl::CivilSecond civil_second,
  42. absl::Duration subsecond, log_internal::Tid tid,
  43. absl::string_view basename, int line,
  44. PrefixFormat format, absl::string_view message);
  45. // Formats various entry metadata into a text string meant for use as a
  46. // prefix on a log message string. Writes into `buf`, advances `buf` to point
  47. // at the remainder of the buffer (i.e. past any written bytes), and returns the
  48. // number of bytes written.
  49. //
  50. // In addition to calling `buf->remove_prefix()` (or the equivalent), this
  51. // function may also do `buf->remove_suffix(buf->size())` in cases where no more
  52. // bytes (i.e. no message data) should be written into the buffer. For example,
  53. // if the prefix ought to be:
  54. // I0926 09:00:00.000000 1234567 foo.cc:123]
  55. // `buf` is too small, the function might fill the whole buffer:
  56. // I0926 09:00:00.000000 1234
  57. // (note the apparrently incorrect thread ID), or it might write less:
  58. // I0926 09:00:00.000000
  59. // In this case, it might also empty `buf` prior to returning to prevent
  60. // message data from being written into the space where a reader would expect to
  61. // see a thread ID.
  62. size_t FormatLogPrefix(absl::LogSeverity severity, absl::Time timestamp,
  63. log_internal::Tid tid, absl::string_view basename,
  64. int line, PrefixFormat format, absl::Span<char>& buf);
  65. } // namespace log_internal
  66. ABSL_NAMESPACE_END
  67. } // namespace absl
  68. #endif // ABSL_LOG_INTERNAL_LOG_FORMAT_H_