Skip to content

Commit 5c0fba9

Browse files
authored
Add option to save logs to file (#490)
* Add command line option to save logs to file in samples
1 parent 2e557d3 commit 5c0fba9

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

samples/utils/CommandLineUtils.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,41 +205,56 @@ namespace Utils
205205
"<log level>",
206206
"The logging level to use. Choices are 'Trace', 'Debug', 'Info', 'Warn', 'Error', 'Fatal', and 'None'. "
207207
"(optional, default='none')");
208+
RegisterCommand(
209+
m_cmd_log_file,
210+
"<str>",
211+
"File to write logs to. If not provided, logs will be written to stdout. "
212+
"(optional, default='none')");
208213
}
209214

210215
void CommandLineUtils::StartLoggingBasedOnCommand(Aws::Crt::ApiHandle *apiHandle)
211216
{
212217
// Process logging command
213218
if (HasCommand("verbosity"))
214219
{
220+
Aws::Crt::LogLevel logLevel = Aws::Crt::LogLevel::None;
215221
Aws::Crt::String verbosity = GetCommand(m_cmd_verbosity);
216222
if (verbosity == "Fatal")
217223
{
218-
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Fatal, stderr);
224+
logLevel = Aws::Crt::LogLevel::Fatal;
219225
}
220226
else if (verbosity == "Error")
221227
{
222-
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Error, stderr);
228+
logLevel = Aws::Crt::LogLevel::Error;
223229
}
224230
else if (verbosity == "Warn")
225231
{
226-
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Warn, stderr);
232+
logLevel = Aws::Crt::LogLevel::Warn;
227233
}
228234
else if (verbosity == "Info")
229235
{
230-
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Info, stderr);
236+
logLevel = Aws::Crt::LogLevel::Info;
231237
}
232238
else if (verbosity == "Debug")
233239
{
234-
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Debug, stderr);
240+
logLevel = Aws::Crt::LogLevel::Debug;
235241
}
236242
else if (verbosity == "Trace")
237243
{
238-
apiHandle->InitializeLogging(Aws::Crt::LogLevel::Trace, stderr);
244+
logLevel = Aws::Crt::LogLevel::Trace;
245+
}
246+
else
247+
{
248+
logLevel = Aws::Crt::LogLevel::None;
249+
}
250+
251+
if (HasCommand("log_file"))
252+
{
253+
apiHandle->InitializeLogging(logLevel, GetCommand(m_cmd_log_file).c_str());
239254
}
240255
else
241256
{
242-
// If none or unknown, then do nothing
257+
apiHandle->InitializeLogging(logLevel, stderr);
243258
}
244259
}
245260
}

samples/utils/CommandLineUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,6 @@ namespace Utils
281281
const Aws::Crt::String m_cmd_custom_auth_authorizer_signature = "custom_auth_authorizer_signature";
282282
const Aws::Crt::String m_cmd_custom_auth_password = "custom_auth_password";
283283
const Aws::Crt::String m_cmd_verbosity = "verbosity";
284+
const Aws::Crt::String m_cmd_log_file = "log_file";
284285
};
285286
} // namespace Utils

0 commit comments

Comments
 (0)