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