• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IDMAP2_INCLUDE_IDMAP2_SYSTRACE_H_
18 #define IDMAP2_INCLUDE_IDMAP2_SYSTRACE_H_
19 
20 #include <sstream>
21 #include <vector>
22 
23 #include "cutils/trace.h"
24 
25 namespace android::idmap2::utils {
26 #ifdef __ANDROID__
27 
28 class ScopedTraceNoStart {
29  public:
30   ~ScopedTraceNoStart();
31 };
32 
33 class ScopedTraceMessageHelper {
34  public:
35   ~ScopedTraceMessageHelper();
36 
stream()37   std::ostream& stream() {
38     return buffer_;
39   }
40 
41  private:
42   std::ostringstream buffer_;
43 };
44 
45 bool atrace_enabled();
46 
47 #define SYSTRACE                                               \
48   android::idmap2::utils::ScopedTraceNoStart _trace##__LINE__; \
49   android::idmap2::utils::atrace_enabled() \
50   && android::idmap2::utils::ScopedTraceMessageHelper().stream()
51 
52 #else
53 
54 class DummyStream {
55  public:
56   std::ostream& stream() {
57     return buffer_;
58   }
59 
60  private:
61   std::ostringstream buffer_;
62 };
63 
64 #define SYSTRACE android::idmap2::utils::DummyStream().stream()
65 
66 #endif
67 }  // namespace android::idmap2::utils
68 
69 template <typename T>
70 std::ostream& operator<<(std::ostream& stream, const std::vector<T>& vector) {
71   bool first = true;
72   stream << "[";
73   for (const auto& item : vector) {
74     if (!first) {
75       stream << ", ";
76     }
77     stream << item;
78     first = false;
79   }
80   stream << "]";
81   return stream;
82 }
83 
84 #endif  // IDMAP2_INCLUDE_IDMAP2_SYSTRACE_H_
85