UniSet @VERSION@
LogServer.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// -------------------------------------------------------------------------
17#ifndef LogServer_H_
18#define LogServer_H_
19// -------------------------------------------------------------------------
20#include <vector>
21#include <string>
22#include <memory>
23#include <unordered_map>
24#include <ev++.h>
25#include <Poco/JSON/Object.h>
26#include "Mutex.h"
27#include "UniXML.h"
28#include "DebugStream.h"
29#include "ThreadCreator.h"
30#include "UTCPSocket.h"
31#include "CommonEventLoop.h"
32#include "LogServerTypes.h"
33
34#ifndef DISABLE_REST_API
35#include <Poco/JSON/Object.h>
36#endif
37// -------------------------------------------------------------------------
38namespace uniset
39{
40 // -------------------------------------------------------------------------
41 class LogSession;
42 class LogAgregator;
43 class NullLogSession;
44 // -------------------------------------------------------------------------
92 // -------------------------------------------------------------------------
93 class LogServer:
94 protected EvWatcher
95 {
96 public:
97
98 LogServer( std::shared_ptr<DebugStream> log );
99 LogServer( std::shared_ptr<LogAgregator> log );
100 virtual ~LogServer() noexcept;
101
102 void setCmdTimeout( timeout_t msec ) noexcept;
103 void setSessionLog( Debug::type t ) noexcept;
104 void setMaxSessionCount( size_t num ) noexcept;
105
106 // port <= 0: select an available port automatically
107 bool async_run( const std::string& addr, int port );
108 bool run( const std::string& addr, int port );
109
110 void terminate();
111
112 bool isRunning() const noexcept;
113
114 bool check( bool restart_if_fail = true );
115
116 void init( const std::string& prefix, xmlNode* cnode = nullptr, int argc = 0, const char* const argv[] = nullptr );
117
118 static std::string help_print( const std::string& prefix );
119
120 std::string getShortInfo();
121
122#ifndef DISABLE_REST_API
123 Poco::JSON::Object::Ptr httpGetShortInfo();
124 static Poco::JSON::Object::Ptr httpLogServerInfo( const std::shared_ptr<LogServer>& logserv,
125 const std::string& host,
126 int port );
127#endif
128
129 protected:
130 LogServer();
131
132 virtual void evprepare( const ev::loop_ref& loop ) override;
133 virtual void evfinish( const ev::loop_ref& loop ) override;
134 virtual std::string wname() const noexcept override;
135
136 void ioAccept( ev::io& watcher, int revents );
137 void sessionFinished( LogSession* s );
138 void saveDefaultLogLevels( const std::string& logname );
139 void restoreDefaultLogLevels( const std::string& logname );
140 std::string onCommand( LogSession* s, LogServerTypes::Command cmd, const std::string& logname );
141
142 private:
143
144 timeout_t cmdTimeout = { 2000 };
145 Debug::type sessLogLevel = { Debug::NONE };
146 size_t sessMaxCount = { 10 };
147
148 typedef std::vector< std::shared_ptr<LogSession> > SessionList;
149 SessionList slist;
150 uniset::uniset_rwmutex mutSList;
151
152 DebugStream mylog;
153 ev::io io;
154
155 // делаем loop общим.. одним на всех!
156 static CommonEventLoop loop;
157
158 std::shared_ptr<UTCPSocket> sock;
159 std::shared_ptr<DebugStream> elog; // eventlog..
160
161 // map с уровнями логов по умолчанию (инициализируются при создании первой сессии),
162 // (они необходимы для восстановления настроек после завершения всех (!) сессий)
163 // т.к. shared_ptr-ов может быть много, то в качестве ключа используем указатель на "реальный объект"(внутри shared_ptr)
164 // но только для этого(!), пользоваться этим указателем ни в коем случае нельзя (и нужно проверять shared_ptr на существование)
165 std::unordered_map< DebugStream*, Debug::type > defaultLogLevels;
166
167 std::string myname = { "LogServer" };
168 std::string addr = { "" };
169 Poco::UInt16 port = { 0 };
170
171 std::atomic_bool isrunning = { false };
172 };
173 // -------------------------------------------------------------------------
174} // end of uniset namespace
175// -------------------------------------------------------------------------
176#endif // LogServer_H_
177// -------------------------------------------------------------------------
Definition DebugStream.h:62
The CommonEventLoop class Реализация механизма "один eventloop, много подписчиков" (libev)....
Definition CommonEventLoop.h:55
Definition CommonEventLoop.h:19
Definition LogServer.h:95
Definition LogSession.h:39
Definition Mutex.h:32
Definition AccessConfig.h:30