1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_TRACE_H_
17 #define PANDA_TRACE_H_
18
19 #include <string>
20 #include <sstream>
21 #include "macros.h"
22
23 namespace ark::trace {
24
25 namespace internal {
26 extern int g_traceMarkerFd;
27 bool DoInit();
28 void DoBeginTracePoint(const char *str);
29 void DoEndTracePoint();
30 void DoIntTracePoint(const char *str, int32_t val);
31 void DoInt64TracePoint(const char *str, int64_t val);
32 } // namespace internal
33
IsEnabled()34 inline bool IsEnabled()
35 {
36 return internal::g_traceMarkerFd != -1;
37 }
38
BeginTracePoint(const char * str)39 inline void BeginTracePoint(const char *str)
40 {
41 if (UNLIKELY(IsEnabled())) {
42 internal::DoBeginTracePoint(str);
43 }
44 }
45
EndTracePoint()46 inline void EndTracePoint()
47 {
48 if (UNLIKELY(IsEnabled())) {
49 internal::DoEndTracePoint();
50 }
51 }
52
IntTracePoint(const char * str,int32_t val)53 inline void IntTracePoint(const char *str, int32_t val)
54 {
55 if (UNLIKELY(IsEnabled())) {
56 internal::DoIntTracePoint(str, val);
57 }
58 }
59
Int64TracePoint(const char * str,int64_t val)60 inline void Int64TracePoint(const char *str, int64_t val)
61 {
62 if (UNLIKELY(IsEnabled())) {
63 internal::DoInt64TracePoint(str, val);
64 }
65 }
66
67 class ScopedTrace {
68 public:
ScopedTrace(const char * str)69 explicit ScopedTrace(const char *str)
70 {
71 BeginTracePoint(str);
72 }
ScopedTrace(const std::string & str)73 explicit ScopedTrace(const std::string &str) : ScopedTrace(str.c_str()) {}
74
~ScopedTrace()75 ~ScopedTrace()
76 {
77 EndTracePoint();
78 }
79
80 NO_COPY_SEMANTIC(ScopedTrace);
81 NO_MOVE_SEMANTIC(ScopedTrace);
82 };
83
84 namespace internal {
85 class ScopeTraceStremHelperBegin {
86 public:
87 ScopeTraceStremHelperBegin() = default;
~ScopeTraceStremHelperBegin()88 ~ScopeTraceStremHelperBegin()
89 {
90 BeginTracePoint(messageBuffer_.str().c_str());
91 }
92
GetStream()93 std::ostream &GetStream()
94 {
95 return messageBuffer_;
96 }
97
98 NO_COPY_SEMANTIC(ScopeTraceStremHelperBegin);
99 NO_MOVE_SEMANTIC(ScopeTraceStremHelperBegin);
100
101 private:
102 std::ostringstream messageBuffer_;
103 };
104
105 class ScopeTraceStremHelperEnd {
106 public:
107 ScopeTraceStremHelperEnd() = default;
~ScopeTraceStremHelperEnd()108 ~ScopeTraceStremHelperEnd()
109 {
110 EndTracePoint();
111 }
112
113 NO_COPY_SEMANTIC(ScopeTraceStremHelperEnd);
114 NO_MOVE_SEMANTIC(ScopeTraceStremHelperEnd);
115 };
116 } // namespace internal
117
118 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
119 #define SCOPED_TRACE_STREAM \
120 ::ark::trace::internal::ScopeTraceStremHelperEnd MERGE_WORDS(end_trace_point, __LINE__); \
121 UNLIKELY(::ark::trace::IsEnabled()) && ::ark::trace::internal::ScopeTraceStremHelperBegin().GetStream()
122
123 } // namespace ark::trace
124
125 #endif // PANDA_TRACE_H_
126