Merge pull request #521

4cede18 console_handler: catch exception inside the input loop (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2015-12-08 20:09:27 +02:00
commit cab6f1239e
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
1 changed files with 37 additions and 32 deletions

View File

@ -268,52 +268,57 @@ namespace epee
template<typename t_cmd_handler> template<typename t_cmd_handler>
bool run(const std::string& prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function<void(void)> exit_handler) bool run(const std::string& prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function<void(void)> exit_handler)
{ {
TRY_ENTRY();
bool continue_handle = true; bool continue_handle = true;
m_prompt = prompt; m_prompt = prompt;
while(continue_handle) while(continue_handle)
{ {
if (!m_running) try
{ {
break; if (!m_running)
} {
print_prompt(); break;
}
print_prompt();
std::string command; std::string command;
bool get_line_ret = m_stdin_reader.get_line(command); bool get_line_ret = m_stdin_reader.get_line(command);
if (!m_running || m_stdin_reader.eos()) if (!m_running || m_stdin_reader.eos())
{ {
break; break;
} }
if (!get_line_ret) if (!get_line_ret)
{ {
LOG_PRINT("Failed to read line.", LOG_LEVEL_0); LOG_PRINT("Failed to read line.", LOG_LEVEL_0);
} }
string_tools::trim(command); string_tools::trim(command);
LOG_PRINT_L2("Read command: " << command); LOG_PRINT_L2("Read command: " << command);
if (command.empty()) if (command.empty())
{ {
continue; continue;
}
else if(cmd_handler(command))
{
continue;
}
else if(0 == command.compare("exit") || 0 == command.compare("q"))
{
continue_handle = false;
}
else
{
std::cout << "unknown command: " << command << std::endl;
std::cout << usage;
}
} }
else if(cmd_handler(command)) catch (const std::exception &ex)
{ {
continue; LOG_ERROR("Exception at [console_handler], what=" << ex.what());
}
else if(0 == command.compare("exit") || 0 == command.compare("q"))
{
continue_handle = false;
}
else
{
std::cout << "unknown command: " << command << std::endl;
std::cout << usage;
} }
} }
if (exit_handler) if (exit_handler)
exit_handler(); exit_handler();
return true; return true;
CATCH_ENTRY_L0("console_handler", false);
} }
private: private: