diff --git a/src/App.cpp b/src/App.cpp index 53c10f21..04ad9dde 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -90,7 +90,7 @@ App::App(int argc, char **argv) : } if (m_options->ccUseRemoteLogging()) { - Log::add(new RemoteLog(m_options->ccRemoteLoggingMaxRows())); + Log::add(new RemoteLog()); } # ifdef HAVE_SYSLOG_H diff --git a/src/Options.cpp b/src/Options.cpp index 11812ddd..0fa73004 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -97,8 +97,7 @@ Options:\n" --cc-access-token=T access token for CC Server\n\ --cc-worker-id=ID custom worker-id for CC Server\n\ --cc-update-interval-s=N status update interval in seconds (default: 10 min: 1)\n\ - --cc-use-remote-logging enable remote logging on CC Server\n\ - --cc-remote-logging-max-rows=N maximum last n-log rows to send CC Server\n" + --cc-use-remote-logging enable remote logging on CC Server\n" # endif # endif @@ -183,7 +182,6 @@ static struct option const options[] = { { "cc-key-file", 1, nullptr, 4015 }, { "cc-use-tls", 0, nullptr, 4016 }, { "cc-use-remote-logging", 0, nullptr, 4017 }, - { "cc-remote-logging-max-rows", 1, nullptr, 4018 }, { "daemonized", 0, nullptr, 4011 }, { "doublehash-thread-mask", 1, nullptr, 4013 }, { "multihash-thread-mask", 1, nullptr, 4013 }, @@ -246,7 +244,6 @@ static struct option const cc_client_options[] = { { "update-interval-s", 1, nullptr, 4012 }, { "use-tls", 0, nullptr, 4016 }, { "use-remote-logging", 0, nullptr, 4017 }, - { "remote-logging-max-rows",1, nullptr, 4018 }, { nullptr, 0, nullptr, 0 } }; @@ -349,7 +346,6 @@ Options::Options(int argc, char **argv) : m_threads(0), m_ccUpdateInterval(10), m_ccPort(0), - m_ccRemoteLoggingMaxRows(25), m_affinity(-1L), m_multiHashThreadMask(-1L) { @@ -562,8 +558,6 @@ bool Options::parseArg(int key, const char *arg) case 4006: /* --cc-port */ case 4012: /* --cc-update-interval-c */ return parseArg(key, strtol(arg, nullptr, 10)); - case 4018: /* --cc-remote-logging-max-rows */ - return parseArg(key, strtol(arg, nullptr, 25)); case 'B': /* --background */ case 'k': /* --keepalive */ @@ -762,15 +756,6 @@ bool Options::parseArg(int key, uint64_t arg) } break; - case 4018: /* --cc-remote-logging-max-rows */ - if (arg < 1) { - m_ccUseRemoteLogging = false; - } - else { - m_ccRemoteLoggingMaxRows = (int) arg; - } - break; - default: break; } diff --git a/src/Options.h b/src/Options.h index 197092a3..911b508a 100644 --- a/src/Options.h +++ b/src/Options.h @@ -101,7 +101,6 @@ public: inline size_t threads() const { return m_threads; } inline int ccUpdateInterval() const { return m_ccUpdateInterval; } inline int ccPort() const { return m_ccPort; } - inline size_t ccRemoteLoggingMaxRows() const { return m_ccRemoteLoggingMaxRows; } inline int64_t affinity() const { return m_affinity; } inline int64_t multiHashThreadMask() const { return m_multiHashThreadMask; } inline void setColors(bool colors) { m_colors = colors; } @@ -176,7 +175,6 @@ private: size_t m_threads; int m_ccUpdateInterval; int m_ccPort; - size_t m_ccRemoteLoggingMaxRows; int64_t m_affinity; int64_t m_multiHashThreadMask; std::vector m_pools; diff --git a/src/cc/Service.cpp b/src/cc/Service.cpp index b649cc7f..72719e13 100644 --- a/src/cc/Service.cpp +++ b/src/cc/Service.cpp @@ -40,6 +40,8 @@ uv_mutex_t Service::m_mutex; std::map Service::m_clientCommand; std::map Service::m_clientStatus; +std::map> Service::m_remoteLog; + int Service::m_currentServerTime = 0; bool Service::start() @@ -55,6 +57,7 @@ void Service::release() m_clientCommand.clear(); m_clientStatus.clear(); + m_remoteLog.clear(); uv_mutex_unlock(&m_mutex); } @@ -77,6 +80,8 @@ unsigned Service::handleGET(const Options* options, const std::string& url, cons resultCode = getClientConfig(options, clientId, resp); } else if (url.rfind("/admin/getClientCommand", 0) == 0) { resultCode = getClientCommand(clientId, resp); + } else if (url.rfind("/admin/getClientLog", 0) == 0) { + //resultCode = getClientCommand(clientId, resp); } } else { @@ -228,10 +233,6 @@ unsigned Service::setClientStatus(const std::string& clientIp, const std::string clientStatus.parseFromJson(document); clientStatus.setExternalIp(clientIp); - if (m_clientStatus.find(clientId) == m_clientStatus.end()) { - m_clientCommand[clientId] = ControlCommand(ControlCommand::RESTART); - } - m_clientStatus[clientId] = clientStatus; resultCode = getClientCommand(clientId, resp); @@ -293,6 +294,29 @@ unsigned Service::resetClientStatusList(const std::string& data, std::string& re return MHD_HTTP_OK; } +unsigned Service::getClientLog(const std::string& clientId, std::string& resp) +{ + if (m_remoteLog.find(clientId) != m_remoteLog.end()) { + + rapidjson::Document respDocument; + respDocument.SetObject(); + + auto& allocator = respDocument.GetAllocator(); + + rapidjson::Value controlCommand = m_clientCommand[clientId].toJson(allocator); + respDocument.AddMember("client_log", controlCommand, allocator); + + rapidjson::StringBuffer buffer(0, 4096); + rapidjson::Writer writer(buffer); + writer.SetMaxDecimalPlaces(10); + respDocument.Accept(writer); + + resp = buffer.GetString(); + } + + return MHD_HTTP_OK; +} + unsigned Service::getAdminPage(const Options* options, std::string& resp) { std::stringstream data; diff --git a/src/cc/Service.h b/src/cc/Service.h index 4993e978..98d0ee03 100644 --- a/src/cc/Service.h +++ b/src/cc/Service.h @@ -48,6 +48,7 @@ public: private: static unsigned getClientConfig(const Options* options, const std::string& clientId, std::string& resp); static unsigned getClientCommand(const std::string& clientId, std::string& resp); + static unsigned getClientLog(const std::string& clientId, std::string& resp); static unsigned getClientStatusList(std::string& resp); static unsigned getAdminPage(const Options* options, std::string& resp); @@ -63,6 +64,7 @@ private: static std::map m_clientStatus; static std::map m_clientCommand; + static std::map> m_remoteLog; static uv_mutex_t m_mutex; diff --git a/src/log/RemoteLog.cpp b/src/log/RemoteLog.cpp index 7a314790..d0d93caa 100644 --- a/src/log/RemoteLog.cpp +++ b/src/log/RemoteLog.cpp @@ -22,15 +22,19 @@ RemoteLog* RemoteLog::m_self = nullptr; -RemoteLog::RemoteLog(size_t maxRows) - : maxRows_(maxRows) +RemoteLog::RemoteLog() + : m_maxRows(100) { + uv_mutex_init(&m_mutex); + m_self = this; } RemoteLog::~RemoteLog() { m_self = nullptr; + + uv_mutex_destroy(&m_mutex); } void RemoteLog::message(int level, const char* fmt, va_list args) @@ -56,13 +60,17 @@ void RemoteLog::message(int level, const char* fmt, va_list args) size = vsnprintf(buf + size, 512 - size - 1, fmt, args) + size; buf[size] = '\n'; - if (rows_.size() == maxRows_) { - rows_.pop_front(); + uv_mutex_lock(&m_mutex); + + if (m_rows.size() == m_maxRows) { + m_rows.pop_front(); } std::string row = std::regex_replace(std::string(buf, size+1), std::regex("\x1B\\[[0-9;]*[a-zA-Z]"), ""); - rows_.push_back(row); + m_rows.push_back(row); + + uv_mutex_unlock(&m_mutex); delete[](buf); } @@ -73,23 +81,21 @@ void RemoteLog::text(const char* fmt, va_list args) message(0, fmt, args); } -void RemoteLog::flushRows() -{ - if (m_self) { - m_self->rows_.clear(); - } -} - - std::string RemoteLog::getRows() { std::stringstream data; + uv_mutex_lock(&m_self->m_mutex); + if (m_self) { - for (std::list::iterator it = m_self->rows_.begin(); it != m_self->rows_.end(); it++) { - data << it->c_str(); + for (auto& m_row : m_self->m_rows) { + data << m_row.c_str(); } } + m_self->m_rows.clear(); + + uv_mutex_unlock(&m_self->m_mutex); + return data.str(); } diff --git a/src/log/RemoteLog.h b/src/log/RemoteLog.h index 793e01ba..07e8a768 100644 --- a/src/log/RemoteLog.h +++ b/src/log/RemoteLog.h @@ -21,6 +21,7 @@ #include #include +#include #include "interfaces/ILogBackend.h" @@ -28,21 +29,21 @@ class RemoteLog : public ILogBackend { public: - RemoteLog(size_t maxRows); + RemoteLog(); ~RemoteLog(); void message(int level, const char* fmt, va_list args) override; void text(const char* fmt, va_list args) override; - static void flushRows(); static std::string getRows(); private: static RemoteLog* m_self; - size_t maxRows_; - std::list rows_; + uv_mutex_t m_mutex; + size_t m_maxRows; + std::list m_rows; }; #endif /* __REMOTELOG_H__ */