1 /*
2 * Copyright (c) 2021-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 #include "option_debug.h"
17 #include "debug_logger.h"
18 #include "option.h"
19 namespace OHOS {
20 namespace Developtools {
21 namespace HiPerf {
OnVerboseLevel(const std::vector<std::string> & debugLevel)22 static bool OnVerboseLevel(const std::vector<std::string> &debugLevel)
23 {
24 if (debugLevel.size() == 0) {
25 return false;
26 }
27 DebugLogger::GetInstance()->SetLogLevel(LEVEL_VERBOSE);
28 DebugLogger::GetInstance()->Disable(false);
29 return true;
30 }
31
OnMuchLevel(const std::vector<std::string> & debugLevel)32 static bool OnMuchLevel(const std::vector<std::string> &debugLevel)
33 {
34 if (debugLevel.size() == 0) {
35 return false;
36 }
37 DebugLogger::GetInstance()->SetLogLevel(LEVEL_MUCH);
38 DebugLogger::GetInstance()->Disable(false);
39 return true;
40 }
41
OnDebugLevel(const std::vector<std::string> & debugLevel)42 static bool OnDebugLevel(const std::vector<std::string> &debugLevel)
43 {
44 if (debugLevel.size() == 0) {
45 return false;
46 }
47 DebugLogger::GetInstance()->SetLogLevel(LEVEL_DEBUG);
48 DebugLogger::GetInstance()->Disable(false);
49 return true;
50 }
51
OnNoDebug(const std::vector<std::string> & debugLevel)52 static bool OnNoDebug(const std::vector<std::string> &debugLevel)
53 {
54 if (debugLevel.size() == 0) {
55 return false;
56 }
57 DebugLogger::GetInstance()->Disable();
58 return true;
59 }
60
OnMixLogOutput(const std::vector<std::string> & debugLevel)61 static bool OnMixLogOutput(const std::vector<std::string> &debugLevel)
62 {
63 if (debugLevel.size() == 0) {
64 return false;
65 }
66 DebugLogger::GetInstance()->SetMixLogOutput(true);
67 return true;
68 }
69
OnLogPath(std::vector<std::string> & args)70 static bool OnLogPath(std::vector<std::string> &args)
71 {
72 if (args.size() > 0) {
73 DebugLogger::GetInstance()->SetLogPath(args[0]);
74 args.erase(args.begin());
75 } else {
76 return false;
77 }
78 return true;
79 }
80
OnLogTag(std::vector<std::string> & args)81 static bool OnLogTag(std::vector<std::string> &args)
82 {
83 if (args.size() > 0) {
84 DebugLogger::GetInstance()->SetLogTags(args[0]);
85 args.erase(args.begin());
86 } else {
87 return false;
88 }
89 return true;
90 }
91 #if is_ohos && !is_double_framework
OnHiLog(const std::vector<std::string> & args)92 static bool OnHiLog(const std::vector<std::string> &args)
93 {
94 DebugLogger::GetInstance()->EnableHiLog();
95 return true;
96 }
97 #endif
RegisterMainCommandDebug()98 void RegisterMainCommandDebug()
99 {
100 Option::RegisterMainOption("--nodebug", "disable debug log, usage format: --nodebug [command] [args]",
101 OnNoDebug);
102 Option::RegisterMainOption("--debug", "show debug log, usage format: --debug [command] [args]",
103 OnDebugLevel);
104 Option::RegisterMainOption("--verbose", "show debug log, usage format: --verbose [command] [args]",
105 OnVerboseLevel);
106 Option::RegisterMainOption("--much", "show extremely much debug log, usage format: --much [command] [args]",
107 OnMuchLevel);
108 Option::RegisterMainOption("--mixlog", "mix the log in output, usage format: --much [command] [args]",
109 OnMixLogOutput);
110 Option::RegisterMainOption("--logpath",
111 "log file name full path, usage format: --logpath [filepath] [command] [args]",
112 OnLogPath);
113 std::string tagUsage = StringPrintf("%s\t%-20s\t%s\t%-20s\t%s",
114 "enable log level for HILOG_TAG, usage format: --logtag <tag>[:level][,<tag>[:level]] [command] [args]\n", " ",
115 "tag: Dump, Report, Record, Stat... level: D, V, M...\n", " ",
116 "example: hiperf --verbose --logtag Record:D [command] [args]");
117 Option::RegisterMainOption("--logtag", tagUsage.c_str(), OnLogTag);
118 #if is_ohos && !is_double_framework
119 Option::RegisterMainOption("--hilog", "use hilog not file to record log", OnHiLog);
120 #endif
121 }
122 } // namespace HiPerf
123 } // namespace Developtools
124 } // namespace OHOS
125