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/utils/time_util.h"
19
20namespace OHOS::Ace {
21
22std::unique_ptr<JsonValue> AceTracker::trackInfo_ = nullptr;
23
24void AceTracker::Start()
25{
26    trackInfo_ = JsonUtil::Create(true);
27}
28
29std::string AceTracker::Stop()
30{
31    if (trackInfo_) {
32        auto info = trackInfo_->ToString();
33        trackInfo_.reset(nullptr);
34        return info;
35    }
36    return "{}";
37}
38
39AceScopedTracker::AceScopedTracker(const std::string& tag) : tag_(tag)
40{
41    if (AceTracker::trackInfo_) {
42        // micro sec.
43        markTime_ = GetMicroTickCount();
44    }
45}
46
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