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