Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_APEXD_STRING_LOG_H_ |
| 18 | #define ANDROID_APEXD_STRING_LOG_H_ |
| 19 | |
| 20 | // Simple helper class to create strings similar to LOGs. |
| 21 | // Usage sample: |
| 22 | // std::string msg = StringLog() << "Hello " << std::hex << 1234; |
| 23 | |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 24 | #include <cstring> |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 25 | #include <iomanip> |
| 26 | #include <iostream> |
| 27 | #include <sstream> |
| 28 | |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | |
| 31 | template <typename T> |
| 32 | class BaseStringLog { |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 33 | public: |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 34 | BaseStringLog() {} |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 35 | |
| 36 | // Pipe in values. |
Dario Freni | 88b859b | 2018-11-06 17:23:36 +0000 | [diff] [blame] | 37 | template <class U> |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 38 | T& operator<<(const U& t) { |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 39 | os_stream << t; |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 40 | return static_cast<T&>(*this); |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Pipe in modifiers. |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 44 | T& operator<<(std::ostream& (*f)(std::ostream&)) { |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 45 | os_stream << f; |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 46 | return static_cast<T&>(*this); |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Get the current string. |
Chih-Hung Hsieh | ab73a44 | 2019-01-02 11:28:05 -0800 | [diff] [blame] | 50 | // NOLINTNEXTLINE(google-explicit-constructor) |
Dario Freni | 88b859b | 2018-11-06 17:23:36 +0000 | [diff] [blame] | 51 | operator std::string() const { return os_stream.str(); } |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | std::ostringstream os_stream; |
| 55 | }; |
| 56 | |
Dario Freni | 88b859b | 2018-11-06 17:23:36 +0000 | [diff] [blame] | 57 | class StringLog : public BaseStringLog<StringLog> {}; |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 58 | |
| 59 | class PStringLog : public BaseStringLog<PStringLog> { |
| 60 | public: |
| 61 | PStringLog() : errno_(errno) {} |
| 62 | |
| 63 | // Get the current string. |
Chih-Hung Hsieh | ab73a44 | 2019-01-02 11:28:05 -0800 | [diff] [blame] | 64 | // NOLINTNEXTLINE(google-explicit-constructor) |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 65 | operator std::string() const { |
Dario Freni | 88b859b | 2018-11-06 17:23:36 +0000 | [diff] [blame] | 66 | return (BaseStringLog::operator std::string()) |
| 67 | .append(": ") |
| 68 | .append(strerror(errno_)); |
Andreas Gampe | d2448a7 | 2018-10-19 22:08:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private: |
| 72 | int errno_; |
| 73 | }; |
| 74 | |
Andreas Gampe | 8f89b76 | 2018-10-19 11:56:01 -0700 | [diff] [blame] | 75 | #endif // ANDROID_APEXD_STRING_LOG_H_ |