1 /*
2  * Copyright (c) 2021-2022 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 "base/log/ace_scoring_log.h"
17 
18 #ifdef LINUX_PLATFORM
19 #include <mutex>
20 #endif
21 
22 #include "base/utils/time_util.h"
23 #include "core/common/ace_application_info.h"
24 
25 namespace OHOS::Ace {
26 
27 std::string AceScoringLog::procName_;
28 bool AceScoringLog::isDebuggingEnabled_ = false;
29 
AceScoringLog(const std::string & eventName)30 AceScoringLog::AceScoringLog(const std::string& eventName)
31 {
32     Init();
33     if (!AceScoringLog::isDebuggingEnabled_) {
34         return;
35     }
36     logInfo_ = eventName;
37 }
38 
AceScoringLog(const std::string & pageName,const std::string & componentType,const std::string & procType)39 AceScoringLog::AceScoringLog(const std::string& pageName, const std::string& componentType, const std::string& procType)
40 {
41     Init();
42     if (!AceScoringLog::isDebuggingEnabled_) {
43         return;
44     }
45     logInfo_ = pageName;
46     logInfo_.append(" ");
47     logInfo_.append(componentType);
48     logInfo_.append(" ");
49     logInfo_.append(procType);
50 }
51 
Init()52 void AceScoringLog::Init()
53 {
54     static std::once_flag onceFlag;
55     std::call_once(onceFlag, []() {
56         AceScoringLog::procName_ = AceApplicationInfo::GetInstance().GetProcessName().empty()
57                                        ? AceApplicationInfo::GetInstance().GetPackageName()
58                                        : AceApplicationInfo::GetInstance().GetProcessName();
59         AceScoringLog::isDebuggingEnabled_ = SystemProperties::IsScoringEnabled(AceScoringLog::procName_);
60     });
61     startTime_ = static_cast<uint64_t>(GetSysTimestamp());
62 }
63 
~AceScoringLog()64 AceScoringLog::~AceScoringLog()
65 {
66     if (!AceScoringLog::isDebuggingEnabled_) {
67         return;
68     }
69     endTime_ = static_cast<uint64_t>(GetSysTimestamp());
70 }
71 
72 } // namespace OHOS::Ace
73