blob: 65143c726eb0d6dda4111058f1041b46d8379470 [file] [log] [blame]
Andreas Gampe8f89b762018-10-19 11:56:01 -07001/*
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 Gamped2448a72018-10-19 22:08:36 -070024#include <cstring>
Andreas Gampe8f89b762018-10-19 11:56:01 -070025#include <iomanip>
26#include <iostream>
27#include <sstream>
28
Andreas Gamped2448a72018-10-19 22:08:36 -070029#include <errno.h>
30
31template <typename T>
32class BaseStringLog {
Andreas Gampe8f89b762018-10-19 11:56:01 -070033 public:
Andreas Gamped2448a72018-10-19 22:08:36 -070034 BaseStringLog() {}
Andreas Gampe8f89b762018-10-19 11:56:01 -070035
36 // Pipe in values.
Dario Freni88b859b2018-11-06 17:23:36 +000037 template <class U>
Andreas Gamped2448a72018-10-19 22:08:36 -070038 T& operator<<(const U& t) {
Andreas Gampe8f89b762018-10-19 11:56:01 -070039 os_stream << t;
Andreas Gamped2448a72018-10-19 22:08:36 -070040 return static_cast<T&>(*this);
Andreas Gampe8f89b762018-10-19 11:56:01 -070041 }
42
43 // Pipe in modifiers.
Andreas Gamped2448a72018-10-19 22:08:36 -070044 T& operator<<(std::ostream& (*f)(std::ostream&)) {
Andreas Gampe8f89b762018-10-19 11:56:01 -070045 os_stream << f;
Andreas Gamped2448a72018-10-19 22:08:36 -070046 return static_cast<T&>(*this);
Andreas Gampe8f89b762018-10-19 11:56:01 -070047 }
48
49 // Get the current string.
Chih-Hung Hsiehab73a442019-01-02 11:28:05 -080050 // NOLINTNEXTLINE(google-explicit-constructor)
Dario Freni88b859b2018-11-06 17:23:36 +000051 operator std::string() const { return os_stream.str(); }
Andreas Gampe8f89b762018-10-19 11:56:01 -070052
53 private:
54 std::ostringstream os_stream;
55};
56
Dario Freni88b859b2018-11-06 17:23:36 +000057class StringLog : public BaseStringLog<StringLog> {};
Andreas Gamped2448a72018-10-19 22:08:36 -070058
59class PStringLog : public BaseStringLog<PStringLog> {
60 public:
61 PStringLog() : errno_(errno) {}
62
63 // Get the current string.
Chih-Hung Hsiehab73a442019-01-02 11:28:05 -080064 // NOLINTNEXTLINE(google-explicit-constructor)
Andreas Gamped2448a72018-10-19 22:08:36 -070065 operator std::string() const {
Dario Freni88b859b2018-11-06 17:23:36 +000066 return (BaseStringLog::operator std::string())
67 .append(": ")
68 .append(strerror(errno_));
Andreas Gamped2448a72018-10-19 22:08:36 -070069 }
70
71 private:
72 int errno_;
73};
74
Andreas Gampe8f89b762018-10-19 11:56:01 -070075#endif // ANDROID_APEXD_STRING_LOG_H_