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 "battery_stats_dumper.h"
17 
18 #include "battery_stats_service.h"
19 #include "stats_common.h"
20 
21 namespace OHOS {
22 namespace PowerMgr {
23 namespace {
24 constexpr const char* ARGS_HELP = "-h";
25 constexpr const char* ARGS_STATS = "-batterystats";
26 constexpr const char* ARGS_POWER_AVERAGE = "-poweraverage";
27 }
28 
Dump(const std::vector<std::string> & args,std::string & result)29 bool BatteryStatsDumper::Dump(const std::vector<std::string>& args, std::string& result)
30 {
31     result.clear();
32     auto argc = args.size();
33     if ((argc == 0) || (args[0] == ARGS_HELP)) {
34         ShowUsage(result);
35         return true;
36     }
37     auto bss = BatteryStatsService::GetInstance();
38     if (bss == nullptr) {
39         return true;
40     }
41     for (auto it = args.begin(); it != args.end(); it++) {
42         if (*it == ARGS_STATS) {
43             auto core = bss->GetBatteryStatsCore();
44             if (core == nullptr) {
45                 continue;
46             }
47             core->DumpInfo(result);
48         } else if (*it == ARGS_POWER_AVERAGE) {
49             auto parser = bss->GetBatteryStatsParser();
50             if (parser == nullptr) {
51                 continue;
52             }
53             parser->DumpInfo(result);
54         }
55     }
56     return true;
57 }
58 
ShowUsage(std::string & result)59 void BatteryStatsDumper::ShowUsage(std::string& result)
60 {
61     std::string HELP_COMMAND_MSG =
62         "usage: statistics <command> [<options>]\n"
63         "command list:\n"
64         "  -h              :    Show this help menu. \n"
65         "  -batterystats   :    Show all the information of battery stats.\n"
66         "  -poweraverage   :    Show all the information of power average configuration.\n";
67     result.append(HELP_COMMAND_MSG);
68 }
69 } // namespace PowerMgr
70 } // namespace OHOS
71