status_payload_printer.h 2.2 KB

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