UniSet  2.6.0
LogServer.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // -------------------------------------------------------------------------
00017 #ifndef LogServer_H_
00018 #define LogServer_H_
00019 // -------------------------------------------------------------------------
00020 #include <vector>
00021 #include <string>
00022 #include <memory>
00023 #include <unordered_map>
00024 #include <ev++.h>
00025 #include "Mutex.h"
00026 #include "UniXML.h"
00027 #include "DebugStream.h"
00028 #include "ThreadCreator.h"
00029 #include "UTCPSocket.h"
00030 #include "CommonEventLoop.h"
00031 #include "LogServerTypes.h"
00032 
00033 #ifndef DISABLE_REST_API
00034 #include <Poco/JSON/Object.h>
00035 #endif
00036 // -------------------------------------------------------------------------
00037 namespace uniset
00038 {
00039 // -------------------------------------------------------------------------
00040 class LogSession;
00041 class LogAgregator;
00042 class NullLogSession;
00043 // -------------------------------------------------------------------------
00091 // -------------------------------------------------------------------------
00092 class LogServer:
00093     protected EvWatcher
00094 {
00095     public:
00096 
00097         LogServer( std::shared_ptr<DebugStream> log );
00098         LogServer( std::shared_ptr<LogAgregator> log );
00099         virtual ~LogServer() noexcept;
00100 
00101         inline void setCmdTimeout( timeout_t msec ) noexcept
00102         {
00103             cmdTimeout = msec;
00104         }
00105 
00106         inline void setSessionLog( Debug::type t ) noexcept
00107         {
00108             sessLogLevel = t;
00109         }
00110         inline void setMaxSessionCount( int num ) noexcept
00111         {
00112             sessMaxCount = num;
00113         }
00114 
00115         void run( const std::string& addr, Poco::UInt16 port, bool thread = true );
00116         void terminate();
00117 
00118         inline bool isRunning() const noexcept
00119         {
00120             return isrunning;
00121         }
00122 
00123         bool check( bool restart_if_fail = true );
00124 
00125         void init( const std::string& prefix, xmlNode* cnode = 0 );
00126 
00127         static std::string help_print( const std::string& prefix );
00128 
00129         std::string getShortInfo();
00130 
00131 #ifndef DISABLE_REST_API
00132         Poco::JSON::Object::Ptr httpGetShortInfo();
00133 #endif
00134 
00135     protected:
00136         LogServer();
00137 
00138         virtual void evprepare( const ev::loop_ref& loop ) override;
00139         virtual void evfinish( const ev::loop_ref& loop ) override;
00140         virtual std::string wname() const noexcept override
00141         {
00142             return myname;
00143         }
00144 
00145         void ioAccept( ev::io& watcher, int revents );
00146         void sessionFinished( LogSession* s );
00147         void saveDefaultLogLevels( const std::string& logname );
00148         void restoreDefaultLogLevels( const std::string& logname );
00149         std::string onCommand( LogSession* s, LogServerTypes::Command cmd, const std::string& logname );
00150 
00151     private:
00152 
00153         timeout_t timeout = { UniSetTimer::WaitUpTime };
00154         timeout_t cmdTimeout = { 2000 };
00155         Debug::type sessLogLevel = { Debug::NONE };
00156         size_t sessMaxCount = { 10 };
00157 
00158         typedef std::vector< std::shared_ptr<LogSession> > SessionList;
00159         SessionList slist;
00160         uniset::uniset_rwmutex mutSList;
00161 
00162         DebugStream mylog;
00163         ev::io io;
00164 
00165         // делаем loop общим.. одним на всех!
00166         static CommonEventLoop loop;
00167 
00168         std::shared_ptr<UTCPSocket> sock;
00169         std::shared_ptr<DebugStream> elog; // eventlog..
00170 
00171         // map с уровнями логов по умолчанию (инициализируются при создании первой сессии),
00172         // (они необходимы для восстановления настроек после завершения всех (!) сессий)
00173         // т.к. shared_ptr-ов может быть много, то в качестве ключа используем указатель на "реальный объект"(внутри shared_ptr)
00174         // но только для этого(!), пользоваться этим указателем ни в коем случае нельзя (и нужно проверять shared_ptr на существование)
00175         std::unordered_map< DebugStream*, Debug::type > defaultLogLevels;
00176 
00177         std::string myname = { "LogServer" };
00178         std::string addr = { "" };
00179         Poco::UInt16 port = { 0 };
00180 
00181         std::atomic_bool isrunning = { false };
00182 };
00183 // -------------------------------------------------------------------------
00184 } // end of uniset namespace
00185 // -------------------------------------------------------------------------
00186 #endif // LogServer_H_
00187 // -------------------------------------------------------------------------