Skip to content

Commit 8b12f43

Browse files
Add verbosity command to samples (#453)
* Add verbosity command to samples * Use capitalized names for verbosity
1 parent b90ff7d commit 8b12f43

File tree

18 files changed

+92
-1
lines changed

18 files changed

+92
-1
lines changed

samples/device_defender/basic_report/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ int main(int argc, char *argv[])
8282
cmdUtils.RegisterCommand(
8383
"report_time", "<int>", "The frequency to send Device Defender reports in seconds (optional, default='60')");
8484
cmdUtils.RegisterCommand("count", "<int>", "The number of reports to send (optional, default='10')");
85+
cmdUtils.AddLoggingCommands();
8586
const char **const_argv = (const char **)argv;
8687
cmdUtils.SendArguments(const_argv, const_argv + argc);
88+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
8789

8890
if (cmdUtils.HasCommand("help"))
8991
{

samples/greengrass/basic_discovery/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ int main(int argc, char *argv[])
4545
cmdUtils.UpdateCommandHelp(
4646
"message",
4747
"The message to send. If no message is provided, you will be prompted to input one (optional, default='')");
48+
cmdUtils.AddLoggingCommands();
4849
cmdUtils.SendArguments(const_argv, const_argv + argc);
50+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
4951

5052
String certificatePath = cmdUtils.GetCommandRequired("cert");
5153
String thingName = cmdUtils.GetCommandRequired("thing_name");

samples/greengrass/ipc/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ int main(int argc, char *argv[])
2727
Utils::CommandLineUtils cmdUtils = Utils::CommandLineUtils();
2828
cmdUtils.RegisterProgramName("greengrass-ipc");
2929
cmdUtils.AddCommonTopicMessageCommands();
30+
cmdUtils.AddLoggingCommands();
3031
const char **const_argv = (const char **)argv;
3132
cmdUtils.SendArguments(const_argv, const_argv + argc);
33+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
3234

3335
String topic = cmdUtils.GetCommandOrDefault("topic", "test/topic");
3436
String message = cmdUtils.GetCommandOrDefault("message", "Hello World");

samples/identity/fleet_provisioning/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ int main(int argc, char *argv[])
7575
cmdUtils.RegisterCommand("template_name", "<str>", "The name of your provisioning template");
7676
cmdUtils.RegisterCommand("template_parameters", "<json>", "Template parameters json");
7777
cmdUtils.RegisterCommand("csr", "<path>", "Path to CSR in PEM format (optional)");
78+
cmdUtils.AddLoggingCommands();
7879
const char **const_argv = (const char **)argv;
7980
cmdUtils.SendArguments(const_argv, const_argv + argc);
81+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
8082

8183
String templateName = cmdUtils.GetCommandRequired("template_name");
8284
String templateParameters = cmdUtils.GetCommandRequired("template_parameters");

samples/jobs/describe_job_execution/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ int main(int argc, char *argv[])
4242
cmdUtils.RegisterCommand("cert", "<path>", "Path to your client certificate in PEM format.");
4343
cmdUtils.RegisterCommand("thing_name", "<str>", "The name of your IOT thing.");
4444
cmdUtils.RegisterCommand("job_id", "<str>", "The job id you want to describe.");
45+
cmdUtils.AddLoggingCommands();
4546
const char **const_argv = (const char **)argv;
4647
cmdUtils.SendArguments(const_argv, const_argv + argc);
48+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
4749

4850
String thingName = cmdUtils.GetCommandRequired("thing_name");
4951
String jobId = cmdUtils.GetCommandRequired("job_id");

samples/mqtt/basic_connect/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ int main(int argc, char *argv[])
2626
cmdUtils.RegisterCommand("cert", "<path>", "Path to your client certificate in PEM format.");
2727
cmdUtils.AddCommonProxyCommands();
2828
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
29+
cmdUtils.AddLoggingCommands();
2930
const char **const_argv = (const char **)argv;
3031
cmdUtils.SendArguments(const_argv, const_argv + argc);
32+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
3133

3234
// Make a MQTT client and create a connection using a certificate and key
3335
// Note: The data for the connection is gotten from cmdUtils

samples/mqtt/custom_authorizer_connect/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ int main(int argc, char *argv[])
2424
cmdUtils.AddCommonMQTTCommands();
2525
cmdUtils.AddCommonCustomAuthorizerCommands();
2626
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
27+
cmdUtils.RemoveCommand("ca_file");
28+
cmdUtils.AddLoggingCommands();
2729
const char **const_argv = (const char **)argv;
2830
cmdUtils.SendArguments(const_argv, const_argv + argc);
31+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
2932

3033
// Make a MQTT client and create a connection through a custom authorizer
3134
// Note: The data for the connection is gotten from cmdUtils

samples/mqtt/pkcs11_connect/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ int main(int argc, char *argv[])
2929
cmdUtils.RegisterCommand("slot_id", "<int>", "Slot ID containing PKCS#11 token to use (optional).");
3030
cmdUtils.RegisterCommand("key_label", "<str>", "Label of private key on the PKCS#11 token (optional).");
3131
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*').");
32+
cmdUtils.AddLoggingCommands();
3233
const char **const_argv = (const char **)argv;
3334
cmdUtils.SendArguments(const_argv, const_argv + argc);
35+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
3436

3537
// Make a MQTT client and create a connection using a PKCS11
3638
// Note: The data for the connection is gotten from cmdUtils

samples/mqtt/raw_connect/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ int main(int argc, char *argv[])
5151
"auth_params",
5252
"<comma delimited list>",
5353
"Comma delimited list of auth parameters. For websockets these will be set as headers (optional).");
54+
cmdUtils.AddLoggingCommands();
5455
const char **const_argv = (const char **)argv;
5556
cmdUtils.SendArguments(const_argv, const_argv + argc);
57+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
5658

5759
String endpoint = cmdUtils.GetCommandRequired("endpoint");
5860
String keyPath = cmdUtils.GetCommandOrDefault("key", "");

samples/mqtt/websocket_connect/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ int main(int argc, char *argv[])
3434
cmdUtils.AddCommonProxyCommands();
3535
cmdUtils.RegisterCommand("signing_region", "<str>", "The signing region used for the websocket signer");
3636
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
37+
cmdUtils.AddLoggingCommands();
3738
const char **const_argv = (const char **)argv;
3839
cmdUtils.SendArguments(const_argv, const_argv + argc);
40+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
3941

4042
// Make a MQTT client and create a connection using websockets
4143
// Note: The data for the connection is gotten from cmdUtils

samples/mqtt/windows_cert_connect/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ int main(int argc, char *argv[])
3131
"Your client certificate in the Windows certificate store. e.g. "
3232
"'CurrentUser\\MY\\6ac133ac58f0a88b83e9c794eba156a98da39b4c'");
3333
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*').");
34+
cmdUtils.AddLoggingCommands();
3435
const char **const_argv = (const char **)argv;
3536
cmdUtils.SendArguments(const_argv, const_argv + argc);
37+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
3638

3739
String endpoint = cmdUtils.GetCommandRequired("endpoint");
3840
String windowsCertStorePath = cmdUtils.GetCommandRequired("cert");

samples/mqtt/x509_credentials_provider_connect/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ int main(int argc, char *argv[])
3636
cmdUtils.RegisterCommand("signing_region", "<str>", "Used for websocket signer");
3737
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
3838
cmdUtils.RegisterCommand("count", "<int>", "The number of messages to send (optional, default='10')");
39+
cmdUtils.AddLoggingCommands();
3940
const char **const_argv = (const char **)argv;
4041
cmdUtils.SendArguments(const_argv, const_argv + argc);
42+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
4143

4244
// Make a MQTT client and create a connection using websockets and x509
4345
// Note: The data for the connection is gotten from cmdUtils

samples/pub_sub/basic_pub_sub/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ int main(int argc, char *argv[])
3939
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
4040
cmdUtils.RegisterCommand("count", "<int>", "The number of messages to send (optional, default='10')");
4141
cmdUtils.RegisterCommand("port_override", "<int>", "The port override to use when connecting (optional)");
42+
cmdUtils.AddLoggingCommands();
4243
const char **const_argv = (const char **)argv;
4344
cmdUtils.SendArguments(const_argv, const_argv + argc);
45+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
4446

4547
String topic = cmdUtils.GetCommandOrDefault("topic", "test/topic");
4648
String clientId = cmdUtils.GetCommandOrDefault("client_id", String("test-") + Aws::Crt::UUID().ToString());

samples/secure_tunneling/secure_tunnel/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ int main(int argc, char *argv[])
4949
"proxy_user_name", "<str>", "User name passed if proxy server requires a user name (optional)");
5050
cmdUtils.RegisterCommand(
5151
"proxy_password", "<str>", "Password passed if proxy server requires a password (optional)");
52+
cmdUtils.AddLoggingCommands();
5253
const char **const_argv = (const char **)argv;
5354
cmdUtils.SendArguments(const_argv, const_argv + argc);
55+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
5456

5557
/*
5658
* Generate secure tunneling endpoint using region

samples/secure_tunneling/tunnel_notification/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ int main(int argc, char *argv[])
4545
cmdUtils.RegisterCommand("key", "<path>", "Path to your key in PEM format.");
4646
cmdUtils.RegisterCommand("cert", "<path>", "Path to your client certificate in PEM format.");
4747
cmdUtils.RegisterCommand("thing_name", "<str>", "The name of your IOT thing");
48+
cmdUtils.AddLoggingCommands();
4849
const char **const_argv = (const char **)argv;
4950
cmdUtils.SendArguments(const_argv, const_argv + argc);
51+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
5052

5153
String thingName = cmdUtils.GetCommandRequired("thing_name");
5254

samples/shadow/shadow_sync/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ int main(int argc, char *argv[])
103103
cmdUtils.RegisterCommand("cert", "<path>", "Path to your client certificate in PEM format.");
104104
cmdUtils.RegisterCommand("thing_name", "<str>", "The name of your IOT thing.");
105105
cmdUtils.RegisterCommand("shadow_property", "<str>", "The name of the shadow property you want to change.");
106+
cmdUtils.AddLoggingCommands();
106107
const char **const_argv = (const char **)argv;
107108
cmdUtils.SendArguments(const_argv, const_argv + argc);
109+
cmdUtils.StartLoggingBasedOnCommand(&apiHandle);
108110

109111
String thingName = cmdUtils.GetCommandRequired("thing_name");
110112
String shadowProperty = cmdUtils.GetCommandRequired("shadow_property");

samples/utils/CommandLineUtils.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,52 @@ namespace Utils
198198
"The password to send when connecting through a custom authorizer (optional)");
199199
}
200200

201+
void CommandLineUtils::AddLoggingCommands()
202+
{
203+
RegisterCommand(
204+
m_cmd_verbosity,
205+
"<log level>",
206+
"The logging level to use. Choices are 'Trace', 'Debug', 'Info', 'Warn', 'Error', 'Fatal', and 'None'. "
207+
"(optional, default='none')");
208+
}
209+
210+
void CommandLineUtils::StartLoggingBasedOnCommand(Aws::Crt::ApiHandle *apiHandle)
211+
{
212+
// Process logging command
213+
if (HasCommand("verbosity"))
214+
{
215+
Aws::Crt::String verbosity = GetCommand(m_cmd_verbosity);
216+
if (verbosity == "Fatal")
217+
{
218+
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Fatal, stderr);
219+
}
220+
else if (verbosity == "Error")
221+
{
222+
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Error, stderr);
223+
}
224+
else if (verbosity == "Warn")
225+
{
226+
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Warn, stderr);
227+
}
228+
else if (verbosity == "Info")
229+
{
230+
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Info, stderr);
231+
}
232+
else if (verbosity == "Debug")
233+
{
234+
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Debug, stderr);
235+
}
236+
else if (verbosity == "Trace")
237+
{
238+
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Trace, stderr);
239+
}
240+
else
241+
{
242+
// If none or unknown, then do nothing
243+
}
244+
}
245+
}
246+
201247
std::shared_ptr<Aws::Crt::Mqtt::MqttConnection> CommandLineUtils::BuildPKCS11MQTTConnection(
202248
Aws::Iot::MqttClient *client)
203249
{

samples/utils/CommandLineUtils.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0.
55
*/
66

7+
#include <aws/crt/Api.h>
78
#include <aws/crt/Types.h>
89
#include <aws/iot/MqttClient.h>
910

@@ -154,11 +155,21 @@ namespace Utils
154155
void AddCommonTopicMessageCommands();
155156

156157
/**
157-
* A helper function taht adds custom_auth_username, custom_auth_authorizer_name,
158+
* A helper function that adds custom_auth_username, custom_auth_authorizer_name,
158159
* custom_auth_authorizer_signature and custom_auth_password commands
159160
*/
160161
void AddCommonCustomAuthorizerCommands();
161162

163+
/**
164+
* A helper function that adds the verbosity command for controlling logging in the samples
165+
*/
166+
void AddLoggingCommands();
167+
168+
/**
169+
* Starts logging based on the result of the verbosity command
170+
*/
171+
void StartLoggingBasedOnCommand(Aws::Crt::ApiHandle *apiHandle);
172+
162173
/**
163174
* A helper function that builds and returns a PKCS11 direct MQTT connection.
164175
*
@@ -269,5 +280,6 @@ namespace Utils
269280
const Aws::Crt::String m_cmd_custom_auth_authorizer_name = "custom_auth_authorizer_name";
270281
const Aws::Crt::String m_cmd_custom_auth_authorizer_signature = "custom_auth_authorizer_signature";
271282
const Aws::Crt::String m_cmd_custom_auth_password = "custom_auth_password";
283+
const Aws::Crt::String m_cmd_verbosity = "verbosity";
272284
};
273285
} // namespace Utils

0 commit comments

Comments
 (0)