• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_LOG_H_
17 #define CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_LOG_H_
18 
19 #include <string>
20 #include <vector>
21 #include "hilog/log.h"
22 
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN 0xD001702
25 
26 #undef LOG_TAG
27 #define LOG_TAG "CGS"
28 
29 #define CGS_LOGD(...) HILOG_DEBUG(LOG_CORE, __VA_ARGS__)
30 #define CGS_LOGI(...) HILOG_INFO(LOG_CORE, __VA_ARGS__)
31 #define CGS_LOGW(...) HILOG_WARN(LOG_CORE, __VA_ARGS__)
32 #define CGS_LOGE(...) HILOG_ERROR(LOG_CORE, __VA_ARGS__)
33 #define CGS_LOGF(...) HILOG_FATAL(LOG_CORE, __VA_ARGS__)
34 
35 namespace OHOS {
36 namespace ResourceSchedule {
37 using Clock = std::chrono::high_resolution_clock;
38 using MilliSecondsType = std::chrono::duration<double, std::milli>;
39 
40 class ChronoScope {
41 public:
ChronoScope(const std::string outmsg)42     explicit ChronoScope(const std::string outmsg) : outmsg_(outmsg)
43     {
44         out_ = nullptr;
45         t1 = Clock::now();
46     }
47 
ChronoScope(const std::string outmsg,double * out)48     ChronoScope(const std::string outmsg, double* out) : outmsg_(outmsg), out_(out)
49     {
50         t1 = Clock::now();
51     }
52 
~ChronoScope()53     ~ChronoScope()
54     {
55         Clock::time_point t2 = Clock::now();
56         MilliSecondsType time_span = std::chrono::duration_cast<MilliSecondsType>(t2 - t1);
57         CGS_LOGD("[%{public}s] cost %{public}lf milliseconds.", outmsg_.c_str(), time_span.count());
58         if (out_) {
59             *out_ = time_span.count();
60         }
61     }
62 
63 private:
64     ChronoScope(const ChronoScope&) = delete;
65     ChronoScope& operator=(const ChronoScope &) = delete;
66     ChronoScope(ChronoScope&&) = delete;
67     ChronoScope& operator=(ChronoScope&&) = delete;
68 
69     std::string outmsg_;
70     double* out_;
71     Clock::time_point t1;
72 };
73 } // namespace ResourceSchedule
74 } // namespace OHOS
75 
76 #endif // CGROUP_SCHED_COMMON_INCLUDE_CGROUP_SCHED_LOG_H_
77