François Degros | 899487c | 2019-07-12 11:57:52 +1000 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CROS_DISKS_QUOTE_H_ |
| 6 | #define CROS_DISKS_QUOTE_H_ |
| 7 | |
| 8 | #include <ostream> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/files/file_path.h> |
François Degros | ca45086 | 2021-01-14 14:40:02 +1100 | [diff] [blame] | 13 | #include <base/logging.h> |
François Degros | 899487c | 2019-07-12 11:57:52 +1000 | [diff] [blame] | 14 | |
| 15 | namespace cros_disks { |
| 16 | |
| 17 | // Helper struct that holds a non-owning const reference to a T and allows to |
| 18 | // print a quoted version of it to an output stream. |
| 19 | // |
| 20 | // Don't use it directly. Call quote() instead. |
| 21 | template <typename T> |
| 22 | struct Quoter { |
| 23 | const T& ref; |
François Degros | ca45086 | 2021-01-14 14:40:02 +1100 | [diff] [blame] | 24 | const bool redacted = false; |
François Degros | 899487c | 2019-07-12 11:57:52 +1000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | // Allows to print a quoted version of its argument to an output stream for |
| 28 | // logging purpose. |
| 29 | // |
| 30 | // T must be a quotable type, ie a string, a FilePath or a vector of quotable |
| 31 | // elements. |
| 32 | // |
| 33 | // The returned Quoter should be streamed directly without being stored. |
| 34 | // The typical usage pattern is: |
| 35 | // |
| 36 | // LOG(ERROR) << "Cannot do something with " << quote(stuff) << ": Reason why"; |
| 37 | template <typename T> |
| 38 | Quoter<T> quote(const T& ref) { |
| 39 | return {ref}; |
| 40 | } |
| 41 | |
François Degros | ca45086 | 2021-01-14 14:40:02 +1100 | [diff] [blame] | 42 | // Allows to print a quoted version of its argument to an output stream for |
| 43 | // logging purpose, or a redacted version if the current log level is less than |
| 44 | // INFO. |
| 45 | // |
| 46 | // T must be a quotable type, ie a string, a FilePath or a vector of quotable |
| 47 | // elements. |
| 48 | // |
| 49 | // The returned Quoter should be streamed directly without being stored. |
| 50 | // The typical usage pattern is: |
| 51 | // |
| 52 | // LOG(ERROR) << "Cannot do something with " << redact(stuff) << ": Reason why"; |
| 53 | template <typename T> |
| 54 | Quoter<T> redact(const T& ref, const bool redacted = !LOG_IS_ON(INFO)) { |
| 55 | return {ref, redacted}; |
| 56 | } |
| 57 | |
François Degros | 899487c | 2019-07-12 11:57:52 +1000 | [diff] [blame] | 58 | // Outputs a quoted C-style string, or (null) for a null pointer. |
| 59 | std::ostream& operator<<(std::ostream& out, Quoter<const char*> quoter); |
| 60 | |
| 61 | // Outputs a quoted standard string. |
| 62 | std::ostream& operator<<(std::ostream& out, Quoter<std::string> quoter); |
| 63 | |
| 64 | // Outputs a quoted file path. |
| 65 | std::ostream& operator<<(std::ostream& out, Quoter<base::FilePath> quoter); |
| 66 | |
| 67 | // Outputs a quoted string literal. |
| 68 | // This is needed because quote("some string literal") doesn't decay the passed |
| 69 | // string literal to a const char* but keeps it as a reference to a const |
| 70 | // char[N]. |
| 71 | template <size_t N> |
| 72 | std::ostream& operator<<(std::ostream& out, Quoter<char[N]> quoter) { |
| 73 | const char* const s = quoter.ref; |
François Degros | ca45086 | 2021-01-14 14:40:02 +1100 | [diff] [blame] | 74 | return out << redact(s, quoter.redacted); |
François Degros | 899487c | 2019-07-12 11:57:52 +1000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Outputs a sequence of quoted items. |
| 78 | template <typename T, typename = decltype(std::declval<const T&>().begin())> |
| 79 | std::ostream& operator<<(std::ostream& out, Quoter<T> quoter) { |
| 80 | out << '['; |
| 81 | const char* sep = ""; |
| 82 | for (const auto& item : quoter.ref) { |
François Degros | ca45086 | 2021-01-14 14:40:02 +1100 | [diff] [blame] | 83 | out << std::exchange(sep, ", ") << redact(item, quoter.redacted); |
François Degros | 899487c | 2019-07-12 11:57:52 +1000 | [diff] [blame] | 84 | } |
| 85 | return out << ']'; |
| 86 | } |
| 87 | |
| 88 | } // namespace cros_disks |
| 89 | |
| 90 | #endif // CROS_DISKS_QUOTE_H_ |