1 /* 2 * Copyright (c) 2021 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_tracker.h" 17 18 #include "base/log/log.h" 19 #include "base/utils/time_util.h" 20 21 namespace OHOS::Ace { 22 23 std::unique_ptr<JsonValue> AceTracker::trackInfo_ = nullptr; 24 Start()25void AceTracker::Start() 26 { 27 trackInfo_ = JsonUtil::Create(true); 28 } 29 Stop()30std::string AceTracker::Stop() 31 { 32 if (trackInfo_) { 33 auto info = trackInfo_->ToString(); 34 trackInfo_.reset(nullptr); 35 return info; 36 } 37 return "{}"; 38 } 39 AceScopedTracker(const std::string & tag)40AceScopedTracker::AceScopedTracker(const std::string& tag) : tag_(tag) 41 { 42 if (AceTracker::trackInfo_) { 43 // micro sec. 44 markTime_ = GetMicroTickCount(); 45 } 46 } ~AceScopedTracker()47AceScopedTracker::~AceScopedTracker() 48 { 49 if (AceTracker::trackInfo_) { 50 // convert micro sec to ms with 1000. 51 AceTracker::trackInfo_->Put(tag_.c_str(), (GetMicroTickCount() - markTime_) / 1000.0); 52 } 53 } 54 55 } // namespace OHOS::Ace 56