UniSet  2.2.1
LogAgregator.h
00001 #ifndef LogAgregator_H_
00002 #define LogAgregator_H_
00003 // -------------------------------------------------------------------------
00004 #include <string>
00005 #include <memory>
00006 #include <regex>
00007 #include <list>
00008 #include <vector>
00009 #include <unordered_map>
00010 #include "DebugStream.h"
00011 #include "LogServerTypes.h"
00012 // -------------------------------------------------------------------------
00109 // -------------------------------------------------------------------------
00110 /* Т.к. в других агрегаторах может тоже встречаться такие же логи, приходится отдельно вести
00111  * учёт подключений (conmap) и подключаться к потокам напрямую, а не к агрегатору
00112  * иначе будет происходить дублирование информации на экране (логи смешиваются от разных агрегаторов)
00113 */
00114 class LogAgregator:
00115     public DebugStream
00116 {
00117     public:
00118 
00119         const std::string sep = {"/"}; /*< раздедитель для имён подчинённых агрегаторов */
00120 
00121         explicit LogAgregator( const std::string& name, Debug::type t );
00122         explicit LogAgregator( const std::string& name = "" );
00123 
00124         virtual ~LogAgregator();
00125 
00126         virtual void logFile( const std::string& f, bool truncate = false ) override;
00127 
00128         void add( std::shared_ptr<LogAgregator> log, const std::string& lname = "" );
00129         void add( std::shared_ptr<DebugStream> log, const std::string& lname = "" );
00130 
00131         std::shared_ptr<DebugStream> create( const std::string& logname );
00132 
00133         // Управление "подчинёнными" логами
00134         void addLevel( const std::string& logname, Debug::type t );
00135         void delLevel( const std::string& logname, Debug::type t );
00136         void level( const std::string& logname, Debug::type t );
00137         void offLogFile( const std::string& logname );
00138         void onLogFile( const std::string& logname );
00139 
00140         // найти лог..
00141         std::shared_ptr<DebugStream> getLog( const std::string& logname );
00142         bool logExist( std::shared_ptr<DebugStream>& l );
00143 
00144         struct iLog
00145         {
00146             iLog( std::shared_ptr<DebugStream>& l, const std::string& nm ): log(l), name(nm) {}
00147             std::shared_ptr<DebugStream> log;
00148             std::string name;
00149 
00150             // для сортировки "по алфавиту"
00151             inline bool operator < ( const iLog& r ) const
00152             {
00153                 return name < r.name;
00154             }
00155         };
00156 
00157         std::list<iLog> getLogList();
00158         std::list<iLog> getLogList( const std::string& regexp_str );
00159 
00160         friend std::ostream& operator<<(std::ostream& os, LogAgregator& la );
00161         friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<LogAgregator> la );
00162 
00163         static std::vector<std::string> splitFirst( const std::string& lname, const std::string s = "/" );
00164 
00165         std::ostream& printLogList( std::ostream& os, const std::string& regexp_str = "" );
00166         static std::ostream& printLogList( std::ostream& os, std::list<iLog>& lst );
00167 
00168     protected:
00169         void logOnEvent( const std::string& s );
00170         void addLog( std::shared_ptr<DebugStream> l, const std::string& lname, bool connect );
00171         void addLogAgregator( std::shared_ptr<LogAgregator> la, const std::string& lname );
00172 
00173         // поиск лога по составному логу.."agregator/agregator2/.../logname"
00174         std::shared_ptr<DebugStream> findLog( const std::string& lname );
00175 
00176         // вывод в виде "дерева"
00177         std::ostream& printTree(std::ostream& os, const std::string& g_tab = "");
00178 
00179         // получить список с именами (длинными) и с указателями на логи
00180         std::list<iLog> makeLogNameList( const std::string& prefix );
00181 
00182     private:
00183         typedef std::unordered_map<std::string, std::shared_ptr<DebugStream>> LogMap;
00184         LogMap lmap;
00185 
00186         typedef std::unordered_map<std::shared_ptr<DebugStream>, sigc::connection> ConnectionMap;
00187         ConnectionMap conmap;
00188 };
00189 // -------------------------------------------------------------------------
00190 #endif // LogAgregator_H_
00191 // -------------------------------------------------------------------------