198 public EventLoopServer
199#ifndef DISABLE_REST_API
200 ,
public Poco::Net::HTTPRequestHandler
204 LogDB(
const std::string& name,
int argc,
const char*
const* argv,
const std::string& prefix );
208 static std::shared_ptr<LogDB>
init_logdb(
int argc,
const char*
const* argv,
const std::string& prefix =
"logdb-" );
213 inline std::shared_ptr<DebugStream> log()
218 void run(
bool async );
219#ifndef DISABLE_REST_API
220 virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
override;
221 void onWebSocketSession( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
229 virtual void evfinish()
override;
230 virtual void evprepare()
override;
231 void onCheckBuffer( ev::timer& t,
int revents );
232 void onActivate( ev::async& watcher,
int revents ) ;
233 void addLog(
Log* log,
const std::string& txt );
234 void log2File(
Log* log,
const std::string& txt );
236 size_t getCountOfRecords(
const std::string& logname =
"" );
237 size_t getFirstOfOldRecord(
size_t maxnum );
240 static std::string qEscapeString(
const std::string& s );
242#ifndef DISABLE_REST_API
243 Poco::JSON::Object::Ptr respError( Poco::Net::HTTPServerResponse& resp, Poco::Net::HTTPResponse::HTTPStatus s,
const std::string& message );
244 Poco::JSON::Object::Ptr httpGetRequest( Poco::Net::HTTPServerResponse& resp,
const std::string& cmd,
const Poco::URI::QueryParameters& p );
245 Poco::JSON::Object::Ptr
httpGetList( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
246 Poco::JSON::Object::Ptr httpGetLogs( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
247 Poco::JSON::Object::Ptr httpGetCount( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
248 Poco::JSON::Object::Ptr httpDownload( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
249 Poco::JSON::Object::Ptr httpLogControl(std::ostream& out, Poco::Net::HTTPServerRequest& req,
250 Poco::Net::HTTPServerResponse& resp,
const std::string& logname,
const Poco::URI::QueryParameters& params );
252 void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req,
253 Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
254 void httpWebSocketConnectPage( Poco::Net::HTTPServerRequest& req,
255 Poco::Net::HTTPServerResponse& resp,
const std::string& logname,
const Poco::URI::QueryParameters& p );
257 bool supportsGzip( Poco::Net::HTTPServerRequest& request );
261 static std::string qLast(
const std::string& p );
264 static std::string qDate(
const std::string& p,
const char sep =
'-');
266 std::shared_ptr<LogWebSocket> newWebSocket(Poco::Net::HTTPServerRequest* req, Poco::Net::HTTPServerResponse* resp,
267 const std::string& logname,
const Poco::URI::QueryParameters& p );
268 void delWebSocket( std::shared_ptr<LogWebSocket>& ws );
272 std::unique_ptr<SQLiteInterface> db;
277 bool activate = {
false };
279 typedef std::queue<std::string> QueryBuffer;
281 size_t qbufSize = { 1000 };
283 ev::timer flushBufferTimer;
284 double tmFlushBuffer_sec = { 1.0 };
288 size_t maxdbRecords = { 200 * 1000 };
289 size_t numOverflow = { 0 };
294 void onTerminate( ev::sig& evsig,
int revents );
296 ev::async wsactivate;
307 std::string peername;
308 std::string description;
310 std::shared_ptr<DebugStream> dblog;
311 std::shared_ptr<DebugStream> logfile;
313 bool isConnected()
const;
315 void set( ev::dynamic_loop& loop );
316 void check( ev::timer& t,
int revents );
317 void event( ev::io& watcher,
int revents );
318 void read( ev::io& watcher );
319 void oncommand( ev::async& watcher,
int revents );
320 void write( ev::io& io );
323 typedef sigc::signal<void, Log*, const std::string&> ReadSignal;
324 ReadSignal signal_on_read();
326 void setCheckConnectionTime(
double sec );
327 void setReadBufSize(
size_t sz );
332 bool connect()
noexcept;
340 double checkConnection_sec = { 5.0 };
342 std::shared_ptr<UTCPStream> tcp;
343 std::vector<char> buf;
345 static const size_t reservsize = { 1000 };
349 std::queue<UTCPCore::Buffer*> wbuf;
352 std::vector<UTCPCore::Buffer*> cmdbuf;
356 std::vector< std::shared_ptr<Log> > logservers;
357 std::shared_ptr<DebugStream> dblog;
360#ifndef DISABLE_REST_API
361 std::shared_ptr<Poco::Net::HTTPServer> httpserv;
362 std::string httpHost = {
"" };
363 int httpPort = { 0 };
364 std::string httpCORS_allow = {
"*" };
365 std::string httpReplyAddr = {
"" };
366 std::string httpJsonContentType = {
"text/json; charset=UTF-8" };
367 std::string httpHtmlContentType = {
"text/html; charset=UTF-8" };
368 std::string utf8Code =
"UTF-8";
370 double wsHeartbeatTime_sec = { 3.0 };
371 double wsSendTime_sec = { 0.5 };
372 size_t wsMaxSend = { 200 };
373 bool httpEnabledLogControl = {
false };
374 bool httpEnabledDownload = {
false };
376 std::string wsPageTemplate =
"";
386 public Poco::Net::WebSocket
389 LogWebSocket(Poco::Net::HTTPServerRequest* req,
390 Poco::Net::HTTPServerResponse* resp,
391 std::shared_ptr<Log>& log );
393 virtual ~LogWebSocket();
396 std::shared_ptr<DebugStream> dblog;
399 void set( ev::dynamic_loop& loop );
401 void send( ev::timer& t,
int revents );
402 void ping( ev::timer& t,
int revents );
403 void add(
Log* log,
const std::string& txt );
406 void waitCompletion();
409 void setHearbeatTime(
const double& sec );
410 void setSendPeriod(
const double& sec );
411 void setMaxSendCount(
size_t val );
418 double send_sec = { 0.5 };
419 size_t maxsend = { 200 };
422 double ping_sec = { 3.0 };
424 std::mutex finishmut;
425 std::condition_variable finish;
427 std::atomic_bool cancelled = {
false };
429 sigc::connection con;
431 Poco::Net::HTTPServerRequest* req;
432 Poco::Net::HTTPServerResponse* resp;
435 std::queue<UTCPCore::Buffer*> wbuf;
438 std::shared_ptr<Log> log;
441 class LogWebSocketGuard
445 LogWebSocketGuard( std::shared_ptr<LogWebSocket>& s, LogDB* l ):
451 logdb->delWebSocket(ws);
456 std::shared_ptr<LogWebSocket> ws;
462 std::list<std::shared_ptr<LogWebSocket>> wsocks;
464 size_t maxwsocks = { 50 };
466 class LogDBRequestHandlerFactory:
467 public Poco::Net::HTTPRequestHandlerFactory
470 LogDBRequestHandlerFactory( LogDB* l ): logdb(l) {}
471 virtual ~LogDBRequestHandlerFactory() {}
473 virtual Poco::Net::HTTPRequestHandler* createRequestHandler(
const Poco::Net::HTTPServerRequest& req )
override;