UniSet  2.8.0
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 "Mutex.h"
26 #include "UniXML.h"
27 #include "DebugStream.h"
28 #include "ThreadCreator.h"
29 #include "UTCPSocket.h"
30 #include "CommonEventLoop.h"
31 #include "LogServerTypes.h"
32 
33 #ifndef DISABLE_REST_API
34 #include <Poco/JSON/Object.h>
35 #endif
36 // -------------------------------------------------------------------------
37 namespace uniset
38 {
39  // -------------------------------------------------------------------------
40  class LogSession;
41  class LogAgregator;
42  class NullLogSession;
43  // -------------------------------------------------------------------------
91  // -------------------------------------------------------------------------
92  class LogServer:
93  protected EvWatcher
94  {
95  public:
96 
97  LogServer( std::shared_ptr<DebugStream> log );
98  LogServer( std::shared_ptr<LogAgregator> log );
99  virtual ~LogServer() noexcept;
100 
101  void setCmdTimeout( timeout_t msec ) noexcept;
102  void setSessionLog( Debug::type t ) noexcept;
103  void setMaxSessionCount( size_t num ) noexcept;
104 
105  bool async_run( const std::string& addr, Poco::UInt16 port );
106  bool run( const std::string& addr, Poco::UInt16 port );
107 
108  void terminate();
109 
110  bool isRunning() const noexcept;
111 
112  bool check( bool restart_if_fail = true );
113 
114  void init( const std::string& prefix, xmlNode* cnode = 0 );
115 
116  static std::string help_print( const std::string& prefix );
117 
118  std::string getShortInfo();
119 
120 #ifndef DISABLE_REST_API
121  Poco::JSON::Object::Ptr httpGetShortInfo();
122 #endif
123 
124  protected:
125  LogServer();
126 
127  virtual void evprepare( const ev::loop_ref& loop ) override;
128  virtual void evfinish( const ev::loop_ref& loop ) override;
129  virtual std::string wname() const noexcept override;
130 
131  void ioAccept( ev::io& watcher, int revents );
132  void sessionFinished( LogSession* s );
133  void saveDefaultLogLevels( const std::string& logname );
134  void restoreDefaultLogLevels( const std::string& logname );
135  std::string onCommand( LogSession* s, LogServerTypes::Command cmd, const std::string& logname );
136 
137  private:
138 
139  timeout_t cmdTimeout = { 2000 };
140  Debug::type sessLogLevel = { Debug::NONE };
141  size_t sessMaxCount = { 10 };
142 
143  typedef std::vector< std::shared_ptr<LogSession> > SessionList;
144  SessionList slist;
145  uniset::uniset_rwmutex mutSList;
146 
147  DebugStream mylog;
148  ev::io io;
149 
150  // делаем loop общим.. одним на всех!
151  static CommonEventLoop loop;
152 
153  std::shared_ptr<UTCPSocket> sock;
154  std::shared_ptr<DebugStream> elog; // eventlog..
155 
156  // map с уровнями логов по умолчанию (инициализируются при создании первой сессии),
157  // (они необходимы для восстановления настроек после завершения всех (!) сессий)
158  // т.к. shared_ptr-ов может быть много, то в качестве ключа используем указатель на "реальный объект"(внутри shared_ptr)
159  // но только для этого(!), пользоваться этим указателем ни в коем случае нельзя (и нужно проверять shared_ptr на существование)
160  std::unordered_map< DebugStream*, Debug::type > defaultLogLevels;
161 
162  std::string myname = { "LogServer" };
163  std::string addr = { "" };
164  Poco::UInt16 port = { 0 };
165 
166  std::atomic_bool isrunning = { false };
167  };
168  // -------------------------------------------------------------------------
169 } // end of uniset namespace
170 // -------------------------------------------------------------------------
171 #endif // LogServer_H_
172 // -------------------------------------------------------------------------
Definition: DebugStream.h:91
The CommonEventLoop class Реализация механизма "один eventloop, много подписчиков" (libev)....
Definition: CommonEventLoop.h:54
Definition: CommonEventLoop.h:14
Definition: CommonEventLoop.h:18
Definition: Mutex.h:31
Definition: LogServer.h:92
Definition: LogSession.h:38