1 /*
2 * Copyright (c) 2023 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 #include "ffrt_trace.h"
17 #include "internal_inc/osal.h"
18
19 namespace ffrt {
TraceLevelManager()20 TraceLevelManager::TraceLevelManager()
21 {
22 traceLevel_ = FFRT_TRACE_LEVEL;
23 std::string trace = GetEnv("FFRT_TRACE_LEVEL");
24 if (trace.size() == 0) {
25 return;
26 }
27
28 int traceLevel = std::stoi(trace);
29 if (traceLevel >= TRACE_LEVEL_MAX || traceLevel < TRACE_LEVEL0) {
30 FFRT_LOGE("get invalid trace level, %d", traceLevel);
31 return;
32 }
33 traceLevel_ = static_cast<uint8_t>(traceLevel);
34 FFRT_LOGW("set trace level %d", traceLevel_);
35 }
36
37 // make sure TraceLevelManager last free
TraceInit(void)38 static __attribute__((constructor)) void TraceInit(void)
39 {
40 ffrt::TraceLevelManager::Instance();
41 }
42
ScopedTrace(uint64_t level,const char * name)43 ScopedTrace::ScopedTrace(uint64_t level, const char* name)
44 : isTraceEnable_(false)
45 {
46 if (level <= TraceLevelManager::Instance()->GetTraceLevel()) {
47 isTraceEnable_ = true;
48 FFRT_TRACE_BEGIN(name);
49 }
50 }
51
~ScopedTrace()52 ScopedTrace::~ScopedTrace()
53 {
54 if (!isTraceEnable_) {
55 return;
56 }
57
58 FFRT_TRACE_END();
59 }
60 } // namespace ffrt