1 /*
2  * Copyright (C) 2019 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 #include <iostream>
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 
22 #ifndef FUZZ_LOG_TAG
23 #error "Must define FUZZ_LOG_TAG"
24 #endif
25 
26 #define FUZZ_LOG() FuzzLog(FUZZ_LOG_TAG).log()
27 
28 #ifdef ENABLE_LOG_FUZZ
29 class FuzzLog {
30 public:
FuzzLog(const char * tag)31     FuzzLog(const char* tag) : mTag(tag) {}
~FuzzLog()32     ~FuzzLog() { std::cout << mTag << ": " << mOs.str() << std::endl; }
33 
log()34     std::stringstream& log() { return mOs; }
35 
36 private:
37     const char* mTag = nullptr;
38     std::stringstream mOs;
39 };
40 #else
41 class FuzzLog {
42 public:
FuzzLog(const char *)43     FuzzLog(const char* /*tag*/) {}
44     template <typename T>
45     FuzzLog& operator<<(const T& /*t*/) {
46         return *this;
47     }
log()48     FuzzLog& log() { return *this; }
49 };
50 #endif
51 
52 std::string hexString(const void* bytes, size_t len);
53 std::string hexString(const std::vector<uint8_t>& bytes);
54