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