status_payload_printer.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2019 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. #ifndef ABSL_STATUS_STATUS_PAYLOAD_PRINTER_H_
  15. #define ABSL_STATUS_STATUS_PAYLOAD_PRINTER_H_
  16. #include <string>
  17. #include "absl/base/nullability.h"
  18. #include "absl/strings/cord.h"
  19. #include "absl/strings/string_view.h"
  20. #include "absl/types/optional.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace status_internal {
  24. // By default, `Status::ToString` and `operator<<(Status)` print a payload by
  25. // dumping the type URL and the raw bytes. To help debugging, we provide an
  26. // extension point, which is a global printer function that can be set by users
  27. // to specify how to print payloads. The function takes the type URL and the
  28. // payload as input, and should return a valid human-readable string on success
  29. // or `absl::nullopt` on failure (in which case it falls back to the default
  30. // approach of printing the raw bytes).
  31. // NOTE: This is an internal API and the design is subject to change in the
  32. // future in a non-backward-compatible way. Since it's only meant for debugging
  33. // purpose, you should not rely on it in any critical logic.
  34. using StatusPayloadPrinter = absl::Nullable<absl::optional<std::string> (*)(
  35. absl::string_view, const absl::Cord&)>;
  36. // Sets the global payload printer. Only one printer should be set per process.
  37. // If multiple printers are set, it's undefined which one will be used.
  38. void SetStatusPayloadPrinter(StatusPayloadPrinter);
  39. // Returns the global payload printer if previously set, otherwise `nullptr`.
  40. StatusPayloadPrinter GetStatusPayloadPrinter();
  41. } // namespace status_internal
  42. ABSL_NAMESPACE_END
  43. } // namespace absl
  44. #endif // ABSL_STATUS_STATUS_PAYLOAD_PRINTER_H_