UniSet  2.6.0
LogAgregator.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 LogAgregator_H_
00018 #define LogAgregator_H_
00019 // -------------------------------------------------------------------------
00020 #include <string>
00021 #include <memory>
00022 #include <regex>
00023 #include <list>
00024 #include <vector>
00025 #include <unordered_map>
00026 #include "DebugStream.h"
00027 #include "LogServerTypes.h"
00028 // -------------------------------------------------------------------------
00029 namespace uniset
00030 {
00127 // -------------------------------------------------------------------------
00128 /* Т.к. в других агрегаторах может тоже встречаться такие же логи, приходится отдельно вести
00129  * учёт подключений (conmap) и подключаться к потокам напрямую, а не к агрегатору
00130  * иначе будет происходить дублирование информации на экране (логи смешиваются от разных агрегаторов)
00131 */
00132 class LogAgregator:
00133     public DebugStream
00134 {
00135     public:
00136 
00137         const std::string sep = {"/"}; /*< раздедитель для имён подчинённых агрегаторов */
00138 
00139         explicit LogAgregator( const std::string& name, Debug::type t );
00140         explicit LogAgregator( const std::string& name = "" );
00141 
00142         virtual ~LogAgregator();
00143 
00144         virtual void logFile( const std::string& f, bool truncate = false ) override;
00145 
00146         void add( std::shared_ptr<LogAgregator> log, const std::string& lname = "" );
00147         void add( std::shared_ptr<DebugStream> log, const std::string& lname = "" );
00148 
00149         std::shared_ptr<DebugStream> create( const std::string& logname );
00150 
00151         // Управление "подчинёнными" логами
00152         void addLevel( const std::string& logname, Debug::type t );
00153         void delLevel( const std::string& logname, Debug::type t );
00154         void level( const std::string& logname, Debug::type t );
00155         void offLogFile( const std::string& logname );
00156         void onLogFile( const std::string& logname );
00157 
00158         // найти лог..
00159         std::shared_ptr<DebugStream> getLog( const std::string& logname );
00160         bool logExist( std::shared_ptr<DebugStream>& l ) const;
00161 
00162         struct iLog
00163         {
00164             iLog( const std::shared_ptr<DebugStream>& l, const std::string& nm ): log(l), name(nm) {}
00165             std::shared_ptr<DebugStream> log;
00166             std::string name;
00167 
00168             // для сортировки "по алфавиту"
00169             inline bool operator < ( const iLog& r ) const
00170             {
00171                 return name < r.name;
00172             }
00173         };
00174 
00175         std::list<iLog> getLogList() const;
00176         std::list<iLog> getLogList( const std::string& regexp_str ) const;
00177 
00178         friend std::ostream& operator<<(std::ostream& os, LogAgregator& la );
00179         friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<LogAgregator> la );
00180 
00181         static std::vector<std::string> splitFirst( const std::string& lname, const std::string s = "/" );
00182 
00183         std::ostream& printLogList( std::ostream& os, const std::string& regexp_str = "" );
00184         static std::ostream& printLogList( std::ostream& os, std::list<iLog>& lst );
00185 
00186     protected:
00187         void logOnEvent( const std::string& s );
00188         void addLog( std::shared_ptr<DebugStream> l, const std::string& lname, bool connect );
00189         void addLogAgregator( std::shared_ptr<LogAgregator> la, const std::string& lname );
00190 
00191         // поиск лога по составному логу.."agregator/agregator2/.../logname"
00192         std::shared_ptr<DebugStream> findLog( const std::string& lname ) const;
00193 
00194         // вывод в виде "дерева"
00195         std::ostream& printTree(std::ostream& os, const std::string& g_tab = "");
00196 
00197         // получить список с именами (длинными) и с указателями на логи
00198         std::list<iLog> makeLogNameList( const std::string& prefix ) const;
00199 
00200     private:
00201         typedef std::unordered_map<std::string, std::shared_ptr<DebugStream>> LogMap;
00202         LogMap lmap;
00203 
00204         typedef std::unordered_map<std::shared_ptr<DebugStream>, sigc::connection> ConnectionMap;
00205         ConnectionMap conmap;
00206 };
00207 // -------------------------------------------------------------------------
00208 } // end of uniset namespace
00209 // -------------------------------------------------------------------------
00210 #endif // LogAgregator_H_
00211 // -------------------------------------------------------------------------