2014-07-25 10:29:08 -06:00
|
|
|
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
|
2014-03-03 15:07:58 -07:00
|
|
|
// All rights reserved.
|
2014-07-25 10:29:08 -06:00
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
|
|
// documentation and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Andrey N. Sabelnikov nor the
|
|
|
|
// names of its contributors may be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
|
|
|
|
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2014-03-03 15:07:58 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-01-29 15:10:53 -07:00
|
|
|
#include "misc_log_ex.h"
|
2017-11-25 15:25:05 -07:00
|
|
|
#include "string_tools.h"
|
2014-03-20 05:46:11 -06:00
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2015-01-29 15:10:53 -07:00
|
|
|
#include <iostream>
|
2016-01-21 11:18:26 -07:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
2016-02-18 14:30:10 -07:00
|
|
|
#include <boost/thread.hpp>
|
2017-11-25 15:25:05 -07:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2014-03-20 05:46:11 -06:00
|
|
|
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
#include "readline_buffer.h"
|
|
|
|
#endif
|
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
|
|
|
#define MONERO_DEFAULT_LOG_CATEGORY "console_handler"
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
namespace epee
|
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
class async_stdin_reader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
async_stdin_reader()
|
|
|
|
: m_run(true)
|
|
|
|
, m_has_read_request(false)
|
|
|
|
, m_read_status(state_init)
|
|
|
|
{
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
m_readline_buffer.start();
|
|
|
|
#endif
|
2017-08-21 05:55:06 -06:00
|
|
|
m_reader_thread = boost::thread(std::bind(&async_stdin_reader::reader_thread_func, this));
|
2014-03-20 05:46:11 -06:00
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
~async_stdin_reader()
|
|
|
|
{
|
2018-09-29 14:17:00 -06:00
|
|
|
try { stop(); }
|
|
|
|
catch (...) { /* ignore */ }
|
2014-03-20 05:46:11 -06:00
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
rdln::readline_buffer& get_readline_buffer()
|
|
|
|
{
|
|
|
|
return m_readline_buffer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
// Not thread safe. Only one thread can call this method at once.
|
|
|
|
bool get_line(std::string& line)
|
|
|
|
{
|
|
|
|
if (!start_read())
|
|
|
|
return false;
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2015-05-30 12:24:15 -06:00
|
|
|
if (state_eos == m_read_status)
|
|
|
|
return false;
|
|
|
|
|
2016-03-11 05:25:28 -07:00
|
|
|
boost::unique_lock<boost::mutex> lock(m_response_mutex);
|
2014-03-20 05:46:11 -06:00
|
|
|
while (state_init == m_read_status)
|
|
|
|
{
|
|
|
|
m_response_cv.wait(lock);
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
bool res = false;
|
|
|
|
if (state_success == m_read_status)
|
|
|
|
{
|
|
|
|
line = m_line;
|
|
|
|
res = true;
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
if (!eos() && m_read_status != state_cancelled)
|
2015-05-30 12:24:15 -06:00
|
|
|
m_read_status = state_init;
|
2014-03-20 05:46:11 -06:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-30 12:24:15 -06:00
|
|
|
bool eos() const { return m_read_status == state_eos; }
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
if (m_run)
|
|
|
|
{
|
|
|
|
m_run.store(false, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
::CloseHandle(::GetStdHandle(STD_INPUT_HANDLE));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_request_cv.notify_one();
|
|
|
|
m_reader_thread.join();
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
m_readline_buffer.stop();
|
|
|
|
#endif
|
2014-03-20 05:46:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
void cancel()
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(m_response_mutex);
|
|
|
|
m_read_status = state_cancelled;
|
|
|
|
m_has_read_request = false;
|
|
|
|
m_response_cv.notify_one();
|
|
|
|
}
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
private:
|
|
|
|
bool start_read()
|
|
|
|
{
|
2016-03-11 05:25:28 -07:00
|
|
|
boost::unique_lock<boost::mutex> lock(m_request_mutex);
|
2014-03-20 05:46:11 -06:00
|
|
|
if (!m_run.load(std::memory_order_relaxed) || m_has_read_request)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_has_read_request = true;
|
|
|
|
m_request_cv.notify_one();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wait_read()
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2016-03-11 05:25:28 -07:00
|
|
|
boost::unique_lock<boost::mutex> lock(m_request_mutex);
|
2014-03-20 05:46:11 -06:00
|
|
|
while (m_run.load(std::memory_order_relaxed) && !m_has_read_request)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
m_request_cv.wait(lock);
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
if (m_has_read_request)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
m_has_read_request = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wait_stdin_data()
|
|
|
|
{
|
|
|
|
#if !defined(WIN32)
|
2017-01-04 18:11:05 -07:00
|
|
|
#if defined(__OpenBSD__) || defined(__ANDROID__)
|
2016-01-21 11:18:26 -07:00
|
|
|
int stdin_fileno = fileno(stdin);
|
|
|
|
#else
|
2014-03-20 05:46:11 -06:00
|
|
|
int stdin_fileno = ::fileno(stdin);
|
2016-01-21 11:18:26 -07:00
|
|
|
#endif
|
2014-03-20 05:46:11 -06:00
|
|
|
|
|
|
|
while (m_run.load(std::memory_order_relaxed))
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2019-05-06 02:44:50 -06:00
|
|
|
if (m_read_status == state_cancelled)
|
|
|
|
return false;
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
fd_set read_set;
|
|
|
|
FD_ZERO(&read_set);
|
|
|
|
FD_SET(stdin_fileno, &read_set);
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 100 * 1000;
|
|
|
|
|
|
|
|
int retval = ::select(stdin_fileno + 1, &read_set, NULL, NULL, &tv);
|
|
|
|
if (retval < 0)
|
|
|
|
return false;
|
|
|
|
else if (0 < retval)
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-17 15:08:32 -07:00
|
|
|
#else
|
|
|
|
while (m_run.load(std::memory_order_relaxed))
|
|
|
|
{
|
2019-05-06 02:44:50 -06:00
|
|
|
if (m_read_status == state_cancelled)
|
|
|
|
return false;
|
|
|
|
|
2016-11-17 15:08:32 -07:00
|
|
|
int retval = ::WaitForSingleObject(::GetStdHandle(STD_INPUT_HANDLE), 100);
|
|
|
|
switch (retval)
|
|
|
|
{
|
|
|
|
case WAIT_FAILED:
|
|
|
|
return false;
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-20 05:46:11 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reader_thread_func()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!wait_read())
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
bool read_ok = true;
|
2017-08-21 05:55:06 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
reread:
|
|
|
|
#endif
|
2014-03-20 05:46:11 -06:00
|
|
|
if (wait_stdin_data())
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
if (m_run.load(std::memory_order_relaxed))
|
|
|
|
{
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
2017-08-21 05:55:06 -06:00
|
|
|
switch (m_readline_buffer.get_line(line))
|
|
|
|
{
|
|
|
|
case rdln::empty: goto eof;
|
|
|
|
case rdln::partial: goto reread;
|
|
|
|
case rdln::full: break;
|
|
|
|
}
|
2017-05-29 16:39:49 -06:00
|
|
|
#else
|
2019-05-06 02:44:50 -06:00
|
|
|
if (m_read_status != state_cancelled)
|
|
|
|
std::getline(std::cin, line);
|
2017-05-29 16:39:49 -06:00
|
|
|
#endif
|
2014-03-20 05:46:11 -06:00
|
|
|
read_ok = !std::cin.eof() && !std::cin.fail();
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
2014-03-20 05:46:11 -06:00
|
|
|
else
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
read_ok = false;
|
|
|
|
}
|
2015-05-30 12:24:15 -06:00
|
|
|
if (std::cin.eof()) {
|
2017-08-21 05:55:06 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
eof:
|
|
|
|
#endif
|
2015-05-30 12:24:15 -06:00
|
|
|
m_read_status = state_eos;
|
|
|
|
m_response_cv.notify_one();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2014-03-20 05:46:11 -06:00
|
|
|
{
|
2016-03-11 05:25:28 -07:00
|
|
|
boost::unique_lock<boost::mutex> lock(m_response_mutex);
|
2014-03-20 05:46:11 -06:00
|
|
|
if (m_run.load(std::memory_order_relaxed))
|
|
|
|
{
|
|
|
|
m_line = std::move(line);
|
|
|
|
m_read_status = read_ok ? state_success : state_error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_read_status = state_cancelled;
|
|
|
|
}
|
|
|
|
m_response_cv.notify_one();
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-20 05:46:11 -06:00
|
|
|
|
|
|
|
enum t_state
|
|
|
|
{
|
|
|
|
state_init,
|
|
|
|
state_success,
|
|
|
|
state_error,
|
2015-05-30 12:24:15 -06:00
|
|
|
state_cancelled,
|
|
|
|
state_eos
|
2014-03-20 05:46:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2016-02-18 14:30:10 -07:00
|
|
|
boost::thread m_reader_thread;
|
2014-03-20 05:46:11 -06:00
|
|
|
std::atomic<bool> m_run;
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
rdln::readline_buffer m_readline_buffer;
|
|
|
|
#endif
|
2014-03-20 05:46:11 -06:00
|
|
|
|
|
|
|
std::string m_line;
|
|
|
|
bool m_has_read_request;
|
|
|
|
t_state m_read_status;
|
|
|
|
|
2016-03-11 05:25:28 -07:00
|
|
|
boost::mutex m_request_mutex;
|
|
|
|
boost::mutex m_response_mutex;
|
|
|
|
boost::condition_variable m_request_cv;
|
|
|
|
boost::condition_variable m_response_cv;
|
2014-03-20 05:46:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class t_server>
|
|
|
|
bool empty_commands_handler(t_server* psrv, const std::string& command)
|
|
|
|
{
|
2014-03-03 15:07:58 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
|
|
|
|
class async_console_handler
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
public:
|
|
|
|
async_console_handler()
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
template<class t_server, class chain_handler>
|
2017-08-02 07:44:42 -06:00
|
|
|
bool run(t_server* psrv, chain_handler ch_handler, std::function<std::string(void)> prompt, const std::string& usage = "")
|
2014-03-20 05:46:11 -06:00
|
|
|
{
|
|
|
|
return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(psrv, cmd); }, [&] { psrv->send_stop_signal(); });
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
template<class chain_handler>
|
2017-08-02 07:44:42 -06:00
|
|
|
bool run(chain_handler ch_handler, std::function<std::string(void)> prompt, const std::string& usage = "", std::function<void(void)> exit_handler = NULL)
|
2014-03-20 05:46:11 -06:00
|
|
|
{
|
2019-05-06 02:44:50 -06:00
|
|
|
return run(prompt, usage, [&](const boost::optional<std::string>& cmd) { return ch_handler(cmd); }, exit_handler);
|
2014-03-20 05:46:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
2014-06-12 22:01:26 -06:00
|
|
|
m_running = false;
|
2014-03-20 05:46:11 -06:00
|
|
|
m_stdin_reader.stop();
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
void cancel()
|
|
|
|
{
|
|
|
|
m_cancel = true;
|
|
|
|
m_stdin_reader.cancel();
|
|
|
|
}
|
|
|
|
|
2015-11-28 05:38:58 -07:00
|
|
|
void print_prompt()
|
|
|
|
{
|
2017-08-02 07:44:42 -06:00
|
|
|
std::string prompt = m_prompt();
|
|
|
|
if (!prompt.empty())
|
2015-11-28 05:38:58 -07:00
|
|
|
{
|
2017-05-29 16:39:49 -06:00
|
|
|
#ifdef HAVE_READLINE
|
2017-08-02 07:44:42 -06:00
|
|
|
std::string color_prompt = "\001\033[1;33m\002" + prompt;
|
|
|
|
if (' ' != prompt.back())
|
2017-06-20 07:22:55 -06:00
|
|
|
color_prompt += " ";
|
|
|
|
color_prompt += "\001\033[0m\002";
|
|
|
|
m_stdin_reader.get_readline_buffer().set_prompt(color_prompt);
|
2017-05-29 16:39:49 -06:00
|
|
|
#else
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
|
|
|
epee::set_console_color(epee::console_color_yellow, true);
|
2017-08-02 07:44:42 -06:00
|
|
|
std::cout << prompt;
|
|
|
|
if (' ' != prompt.back())
|
2015-11-28 05:38:58 -07:00
|
|
|
std::cout << ' ';
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
|
|
|
epee::reset_console_color();
|
2015-11-28 05:38:58 -07:00
|
|
|
std::cout.flush();
|
2017-05-29 16:39:49 -06:00
|
|
|
#endif
|
2015-11-28 05:38:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
private:
|
2015-06-03 15:42:30 -06:00
|
|
|
template<typename t_cmd_handler>
|
2017-08-02 07:44:42 -06:00
|
|
|
bool run(std::function<std::string(void)> prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function<void(void)> exit_handler)
|
2014-03-20 05:46:11 -06:00
|
|
|
{
|
|
|
|
bool continue_handle = true;
|
2015-11-28 05:38:58 -07:00
|
|
|
m_prompt = prompt;
|
2014-03-20 05:46:11 -06:00
|
|
|
while(continue_handle)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2015-12-06 15:03:53 -07:00
|
|
|
try
|
2014-06-12 22:01:26 -06:00
|
|
|
{
|
2015-12-06 15:03:53 -07:00
|
|
|
if (!m_running)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
print_prompt();
|
2014-03-20 05:46:11 -06:00
|
|
|
|
2015-12-06 15:03:53 -07:00
|
|
|
std::string command;
|
|
|
|
bool get_line_ret = m_stdin_reader.get_line(command);
|
2018-10-20 03:12:55 -06:00
|
|
|
if (!m_running)
|
|
|
|
break;
|
|
|
|
if (m_stdin_reader.eos())
|
2015-12-06 15:03:53 -07:00
|
|
|
{
|
2018-10-20 03:12:55 -06:00
|
|
|
MGINFO("EOF on stdin, exiting");
|
2019-03-13 10:51:09 -06:00
|
|
|
std::cout << std::endl;
|
2015-12-06 15:03:53 -07:00
|
|
|
break;
|
|
|
|
}
|
2019-05-06 02:44:50 -06:00
|
|
|
|
|
|
|
if (m_cancel)
|
|
|
|
{
|
|
|
|
MDEBUG("Input cancelled");
|
|
|
|
cmd_handler(boost::none);
|
|
|
|
m_cancel = false;
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-06 15:03:53 -07:00
|
|
|
if (!get_line_ret)
|
|
|
|
{
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 09:34:23 -07:00
|
|
|
MERROR("Failed to read line.");
|
2015-12-06 15:03:53 -07:00
|
|
|
}
|
2019-05-06 02:44:50 -06:00
|
|
|
|
2015-12-06 15:03:53 -07:00
|
|
|
string_tools::trim(command);
|
2014-03-20 05:46:11 -06:00
|
|
|
|
2015-12-06 15:03:53 -07:00
|
|
|
LOG_PRINT_L2("Read command: " << command);
|
2019-05-06 02:44:50 -06:00
|
|
|
if(cmd_handler(command))
|
2015-12-06 15:03:53 -07:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(0 == command.compare("exit") || 0 == command.compare("q"))
|
|
|
|
{
|
|
|
|
continue_handle = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-07 13:40:32 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
rdln::suspend_readline pause_readline;
|
|
|
|
#endif
|
2015-12-06 15:03:53 -07:00
|
|
|
std::cout << "unknown command: " << command << std::endl;
|
|
|
|
std::cout << usage;
|
|
|
|
}
|
2015-03-27 06:01:30 -06:00
|
|
|
}
|
2015-12-06 15:03:53 -07:00
|
|
|
catch (const std::exception &ex)
|
2014-03-20 05:46:11 -06:00
|
|
|
{
|
2015-12-06 15:03:53 -07:00
|
|
|
LOG_ERROR("Exception at [console_handler], what=" << ex.what());
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 03:53:20 -06:00
|
|
|
if (exit_handler)
|
|
|
|
exit_handler();
|
2014-03-20 05:46:11 -06:00
|
|
|
return true;
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
private:
|
|
|
|
async_stdin_reader m_stdin_reader;
|
2015-01-29 15:10:53 -07:00
|
|
|
std::atomic<bool> m_running = {true};
|
2019-05-06 02:44:50 -06:00
|
|
|
std::atomic<bool> m_cancel = {false};
|
2017-08-02 07:44:42 -06:00
|
|
|
std::function<std::string(void)> m_prompt;
|
2014-03-20 05:46:11 -06:00
|
|
|
};
|
2014-03-03 15:07:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
template<class t_server, class t_handler>
|
2018-05-26 12:32:18 -06:00
|
|
|
bool start_default_console(t_server* ptsrv, t_handler handlr, std::function<std::string(void)> prompt, const std::string& usage = "")
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
std::shared_ptr<async_console_handler> console_handler = std::make_shared<async_console_handler>();
|
2014-04-07 09:02:15 -06:00
|
|
|
boost::thread([=](){console_handler->run<t_server, t_handler>(ptsrv, handlr, prompt, usage);}).detach();
|
2014-03-03 15:07:58 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-26 12:32:18 -06:00
|
|
|
template<class t_server, class t_handler>
|
|
|
|
bool start_default_console(t_server* ptsrv, t_handler handlr, const std::string& prompt, const std::string& usage = "")
|
|
|
|
{
|
|
|
|
return start_default_console(ptsrv, handlr, [prompt](){ return prompt; }, usage);
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
template<class t_server>
|
2014-03-20 05:46:11 -06:00
|
|
|
bool start_default_console(t_server* ptsrv, const std::string& prompt, const std::string& usage = "")
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
return start_default_console(ptsrv, empty_commands_handler<t_server>, prompt, usage);
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class t_server, class t_handler>
|
|
|
|
bool no_srv_param_adapter(t_server* ptsrv, const std::string& cmd, t_handler handlr)
|
|
|
|
{
|
|
|
|
return handlr(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class t_server, class t_handler>
|
2018-05-26 12:32:18 -06:00
|
|
|
bool run_default_console_handler_no_srv_param(t_server* ptsrv, t_handler handlr, std::function<std::string(void)> prompt, const std::string& usage = "")
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
async_console_handler console_handler;
|
|
|
|
return console_handler.run(ptsrv, boost::bind<bool>(no_srv_param_adapter<t_server, t_handler>, _1, _2, handlr), prompt, usage);
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class t_server, class t_handler>
|
2018-05-26 12:32:18 -06:00
|
|
|
bool run_default_console_handler_no_srv_param(t_server* ptsrv, t_handler handlr, const std::string& prompt, const std::string& usage = "")
|
|
|
|
{
|
|
|
|
return run_default_console_handler_no_srv_param(ptsrv, handlr, [prompt](){return prompt;},usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class t_server, class t_handler>
|
|
|
|
bool start_default_console_handler_no_srv_param(t_server* ptsrv, t_handler handlr, std::function<std::string(void)> prompt, const std::string& usage = "")
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
boost::thread( boost::bind(run_default_console_handler_no_srv_param<t_server, t_handler>, ptsrv, handlr, prompt, usage) );
|
2014-03-03 15:07:58 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-26 12:32:18 -06:00
|
|
|
template<class t_server, class t_handler>
|
|
|
|
bool start_default_console_handler_no_srv_param(t_server* ptsrv, t_handler handlr, const std::string& prompt, const std::string& usage = "")
|
|
|
|
{
|
|
|
|
return start_default_console_handler_no_srv_param(ptsrv, handlr, [prompt](){return prompt;}, usage);
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
/*template<class a>
|
|
|
|
bool f(int i, a l)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}*/
|
|
|
|
/*
|
|
|
|
template<class chain_handler>
|
|
|
|
bool default_console_handler2(chain_handler ch_handler, const std::string usage)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*template<class t_handler>
|
|
|
|
bool start_default_console2(t_handler handlr, const std::string& usage = "")
|
|
|
|
{
|
|
|
|
//std::string usage_local = usage;
|
|
|
|
boost::thread( boost::bind(default_console_handler2<t_handler>, handlr, usage) );
|
|
|
|
//boost::function<bool ()> p__ = boost::bind(f<t_handler>, 1, handlr);
|
|
|
|
//boost::function<bool ()> p__ = boost::bind(default_console_handler2<t_handler>, handlr, usage);
|
|
|
|
//boost::thread tr(p__);
|
|
|
|
return true;
|
|
|
|
}*/
|
|
|
|
|
2015-01-29 15:10:53 -07:00
|
|
|
class command_handler {
|
2014-03-20 05:46:11 -06:00
|
|
|
public:
|
2015-01-29 15:10:53 -07:00
|
|
|
typedef boost::function<bool (const std::vector<std::string> &)> callback;
|
2019-05-06 02:44:50 -06:00
|
|
|
typedef boost::function<bool (void)> empty_callback;
|
2017-11-22 05:53:18 -07:00
|
|
|
typedef std::map<std::string, std::pair<callback, std::pair<std::string, std::string>>> lookup;
|
2015-01-29 15:10:53 -07:00
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
command_handler():
|
|
|
|
m_unknown_command_handler([](const std::vector<std::string>&){return false;}),
|
|
|
|
m_empty_command_handler([](){return true;}),
|
|
|
|
m_cancel_handler([](){return true;})
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
std::string get_usage()
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2014-03-20 05:46:11 -06:00
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
for(auto& x:m_command_handlers)
|
|
|
|
{
|
2017-11-22 05:53:18 -07:00
|
|
|
ss << x.second.second.first << ENDL;
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
2015-01-29 15:10:53 -07:00
|
|
|
|
2017-11-22 05:53:18 -07:00
|
|
|
std::pair<std::string, std::string> get_documentation(const std::vector<std::string>& cmd)
|
|
|
|
{
|
|
|
|
if(cmd.empty())
|
|
|
|
return std::make_pair("", "");
|
|
|
|
auto it = m_command_handlers.find(cmd.front());
|
|
|
|
if(it == m_command_handlers.end())
|
|
|
|
return std::make_pair("", "");
|
|
|
|
return it->second.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_handler(const std::string& cmd, const callback& hndlr, const std::string& usage = "", const std::string& description = "")
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2015-01-29 15:10:53 -07:00
|
|
|
lookup::mapped_type & vt = m_command_handlers[cmd];
|
2014-03-03 15:07:58 -07:00
|
|
|
vt.first = hndlr;
|
2017-11-22 05:53:18 -07:00
|
|
|
vt.second.first = description.empty() ? cmd : usage;
|
|
|
|
vt.second.second = description.empty() ? usage : description;
|
2017-07-07 13:40:32 -06:00
|
|
|
#ifdef HAVE_READLINE
|
|
|
|
rdln::readline_buffer::add_completion(cmd);
|
|
|
|
#endif
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
2015-01-29 15:10:53 -07:00
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
void set_unknown_command_handler(const callback& hndlr)
|
|
|
|
{
|
|
|
|
m_unknown_command_handler = hndlr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_empty_command_handler(const empty_callback& hndlr)
|
|
|
|
{
|
|
|
|
m_empty_command_handler = hndlr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_cancel_handler(const empty_callback& hndlr)
|
|
|
|
{
|
|
|
|
m_cancel_handler = hndlr;
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:07:58 -07:00
|
|
|
bool process_command_vec(const std::vector<std::string>& cmd)
|
|
|
|
{
|
2019-05-06 02:44:50 -06:00
|
|
|
if(!cmd.size() || (cmd.size() == 1 && !cmd[0].size()))
|
|
|
|
return m_empty_command_handler();
|
2014-03-03 15:07:58 -07:00
|
|
|
auto it = m_command_handlers.find(cmd.front());
|
|
|
|
if(it == m_command_handlers.end())
|
2019-05-06 02:44:50 -06:00
|
|
|
return m_unknown_command_handler(cmd);
|
2014-03-03 15:07:58 -07:00
|
|
|
std::vector<std::string> cmd_local(cmd.begin()+1, cmd.end());
|
|
|
|
return it->second.first(cmd_local);
|
|
|
|
}
|
|
|
|
|
2019-05-06 02:44:50 -06:00
|
|
|
bool process_command_str(const boost::optional<std::string>& cmd)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2019-05-06 02:44:50 -06:00
|
|
|
if (!cmd)
|
|
|
|
return m_cancel_handler();
|
2014-03-03 15:07:58 -07:00
|
|
|
std::vector<std::string> cmd_v;
|
2019-05-06 02:44:50 -06:00
|
|
|
boost::split(cmd_v,*cmd,boost::is_any_of(" "), boost::token_compress_on);
|
2014-03-03 15:07:58 -07:00
|
|
|
return process_command_vec(cmd_v);
|
|
|
|
}
|
2015-01-29 15:10:53 -07:00
|
|
|
private:
|
|
|
|
lookup m_command_handlers;
|
2019-05-06 02:44:50 -06:00
|
|
|
callback m_unknown_command_handler;
|
|
|
|
empty_callback m_empty_command_handler;
|
|
|
|
empty_callback m_cancel_handler;
|
2015-01-29 15:10:53 -07:00
|
|
|
};
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2015-01-29 15:10:53 -07:00
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
class console_handlers_binder : public command_handler
|
|
|
|
{
|
|
|
|
typedef command_handler::callback console_command_handler;
|
|
|
|
typedef command_handler::lookup command_handlers_map;
|
|
|
|
std::unique_ptr<boost::thread> m_console_thread;
|
|
|
|
async_console_handler m_console_handler;
|
|
|
|
public:
|
2019-11-12 09:05:17 -07:00
|
|
|
~console_handlers_binder() {
|
|
|
|
stop_handling();
|
|
|
|
if (m_console_thread.get() != nullptr)
|
|
|
|
{
|
|
|
|
m_console_thread->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 07:44:42 -06:00
|
|
|
bool start_handling(std::function<std::string(void)> prompt, const std::string& usage_string = "", std::function<void(void)> exit_handler = NULL)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2015-06-03 15:42:30 -06:00
|
|
|
m_console_thread.reset(new boost::thread(boost::bind(&console_handlers_binder::run_handling, this, prompt, usage_string, exit_handler)));
|
2014-03-03 15:07:58 -07:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-02 07:44:42 -06:00
|
|
|
bool start_handling(const std::string &prompt, const std::string& usage_string = "", std::function<void(void)> exit_handler = NULL)
|
|
|
|
{
|
|
|
|
return start_handling([prompt](){ return prompt; }, usage_string, exit_handler);
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
|
2014-03-20 05:46:11 -06:00
|
|
|
void stop_handling()
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2014-03-20 05:46:11 -06:00
|
|
|
m_console_handler.stop();
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
|
|
|
|
2017-08-02 07:44:42 -06:00
|
|
|
bool run_handling(std::function<std::string(void)> prompt, const std::string& usage_string, std::function<void(void)> exit_handler = NULL)
|
2014-03-03 15:07:58 -07:00
|
|
|
{
|
2015-06-03 15:42:30 -06:00
|
|
|
return m_console_handler.run(boost::bind(&console_handlers_binder::process_command_str, this, _1), prompt, usage_string, exit_handler);
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|
2015-11-28 05:38:58 -07:00
|
|
|
|
|
|
|
void print_prompt()
|
|
|
|
{
|
|
|
|
m_console_handler.print_prompt();
|
|
|
|
}
|
2019-05-06 02:44:50 -06:00
|
|
|
|
|
|
|
void cancel_input()
|
|
|
|
{
|
|
|
|
m_console_handler.cancel();
|
|
|
|
}
|
2014-03-03 15:07:58 -07:00
|
|
|
};
|
|
|
|
|
2015-01-29 15:10:53 -07:00
|
|
|
///* work around because of broken boost bind */
|
|
|
|
//template<class t_server>
|
|
|
|
//class srv_console_handlers_binder: public command_handler
|
|
|
|
//{
|
|
|
|
// async_console_handler m_console_handler;
|
|
|
|
//public:
|
|
|
|
// bool start_handling(t_server* psrv, const std::string& prompt, const std::string& usage_string = "")
|
|
|
|
// {
|
|
|
|
// boost::thread(boost::bind(&srv_console_handlers_binder<t_server>::run_handling, this, psrv, prompt, usage_string)).detach();
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// bool run_handling(t_server* psrv, const std::string& prompt, const std::string& usage_string)
|
|
|
|
// {
|
|
|
|
// return m_console_handler.run(psrv, boost::bind(&srv_console_handlers_binder<t_server>::process_command_str, this, _1, _2), prompt, usage_string);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// void stop_handling()
|
|
|
|
// {
|
|
|
|
// m_console_handler.stop();
|
|
|
|
// }
|
|
|
|
//private:
|
|
|
|
// bool process_command_str(t_server* /*psrv*/, const std::string& cmd)
|
|
|
|
// {
|
|
|
|
// return console_handlers_binder::process_command_str(cmd);
|
|
|
|
// }
|
|
|
|
//};
|
2014-03-03 15:07:58 -07:00
|
|
|
}
|