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 } 27 Dump(const std::vector<std::string> & args,std::string & result)28bool BatteryStatsDumper::Dump(const std::vector<std::string>& args, std::string& result) 29 { 30 result.clear(); 31 auto argc = args.size(); 32 if ((argc == 0) || (args[0] == ARGS_HELP)) { 33 ShowUsage(result); 34 return true; 35 } 36 auto bss = DelayedStatsSpSingleton<BatteryStatsService>::GetInstance(); 37 if (bss == nullptr) { 38 return true; 39 } 40 for (auto it = args.begin(); it != args.end(); it++) { 41 if (*it == ARGS_STATS) { 42 auto core = bss->GetBatteryStatsCore(); 43 if (core == nullptr) { 44 continue; 45 } 46 core->DumpInfo(result); 47 } 48 } 49 return true; 50 } 51 ShowUsage(std::string & result)52void BatteryStatsDumper::ShowUsage(std::string& result) 53 { 54 std::string HELP_COMMAND_MSG = 55 "usage: statistics <command> [<options>]\n" 56 "command list:\n" 57 " -h : Show this help menu. \n" 58 " -batterystats : Show all the information of battery stats.\n"; 59 result.append(HELP_COMMAND_MSG); 60 } 61 } // namespace PowerMgr 62 } // namespace OHOS 63