UniSet  2.6.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  inline void setCmdTimeout( timeout_t msec ) noexcept
102  {
103  cmdTimeout = msec;
104  }
105 
106  inline void setSessionLog( Debug::type t ) noexcept
107  {
108  sessLogLevel = t;
109  }
110  inline void setMaxSessionCount( int num ) noexcept
111  {
112  sessMaxCount = num;
113  }
114 
115  void run( const std::string& addr, Poco::UInt16 port, bool thread = true );
116  void terminate();
117 
118  inline bool isRunning() const noexcept
119  {
120  return isrunning;
121  }
122 
123  bool check( bool restart_if_fail = true );
124 
125  void init( const std::string& prefix, xmlNode* cnode = 0 );
126 
127  static std::string help_print( const std::string& prefix );
128 
129  std::string getShortInfo();
130 
131 #ifndef DISABLE_REST_API
132  Poco::JSON::Object::Ptr httpGetShortInfo();
133 #endif
134 
135  protected:
136  LogServer();
137 
138  virtual void evprepare( const ev::loop_ref& loop ) override;
139  virtual void evfinish( const ev::loop_ref& loop ) override;
140  virtual std::string wname() const noexcept override
141  {
142  return myname;
143  }
144 
145  void ioAccept( ev::io& watcher, int revents );
146  void sessionFinished( LogSession* s );
147  void saveDefaultLogLevels( const std::string& logname );
148  void restoreDefaultLogLevels( const std::string& logname );
149  std::string onCommand( LogSession* s, LogServerTypes::Command cmd, const std::string& logname );
150 
151  private:
152 
153  timeout_t timeout = { UniSetTimer::WaitUpTime };
154  timeout_t cmdTimeout = { 2000 };
155  Debug::type sessLogLevel = { Debug::NONE };
156  size_t sessMaxCount = { 10 };
157 
158  typedef std::vector< std::shared_ptr<LogSession> > SessionList;
159  SessionList slist;
160  uniset::uniset_rwmutex mutSList;
161 
162  DebugStream mylog;
163  ev::io io;
164 
165  // делаем loop общим.. одним на всех!
166  static CommonEventLoop loop;
167 
168  std::shared_ptr<UTCPSocket> sock;
169  std::shared_ptr<DebugStream> elog; // eventlog..
170 
171  // map с уровнями логов по умолчанию (инициализируются при создании первой сессии),
172  // (они необходимы для восстановления настроек после завершения всех (!) сессий)
173  // т.к. shared_ptr-ов может быть много, то в качестве ключа используем указатель на "реальный объект"(внутри shared_ptr)
174  // но только для этого(!), пользоваться этим указателем ни в коем случае нельзя (и нужно проверять shared_ptr на существование)
175  std::unordered_map< DebugStream*, Debug::type > defaultLogLevels;
176 
177  std::string myname = { "LogServer" };
178  std::string addr = { "" };
179  Poco::UInt16 port = { 0 };
180 
181  std::atomic_bool isrunning = { false };
182  };
183  // -------------------------------------------------------------------------
184 } // end of uniset namespace
185 // -------------------------------------------------------------------------
186 #endif // LogServer_H_
187 // -------------------------------------------------------------------------