UniSet  2.6.0
LogAgregator.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 LogAgregator_H_
18 #define LogAgregator_H_
19 // -------------------------------------------------------------------------
20 #include <string>
21 #include <memory>
22 #include <regex>
23 #include <list>
24 #include <vector>
25 #include <unordered_map>
26 #include "DebugStream.h"
27 #include "LogServerTypes.h"
28 // -------------------------------------------------------------------------
29 namespace uniset
30 {
127  // -------------------------------------------------------------------------
128  /* Т.к. в других агрегаторах может тоже встречаться такие же логи, приходится отдельно вести
129  * учёт подключений (conmap) и подключаться к потокам напрямую, а не к агрегатору
130  * иначе будет происходить дублирование информации на экране (логи смешиваются от разных агрегаторов)
131  */
133  public DebugStream
134  {
135  public:
136 
137  const std::string sep = {"/"}; /*< раздедитель для имён подчинённых агрегаторов */
138 
139  explicit LogAgregator( const std::string& name, Debug::type t );
140  explicit LogAgregator( const std::string& name = "" );
141 
142  virtual ~LogAgregator();
143 
144  virtual void logFile( const std::string& f, bool truncate = false ) override;
145 
146  void add( std::shared_ptr<LogAgregator> log, const std::string& lname = "" );
147  void add( std::shared_ptr<DebugStream> log, const std::string& lname = "" );
148 
149  std::shared_ptr<DebugStream> create( const std::string& logname );
150 
151  // Управление "подчинёнными" логами
152  void addLevel( const std::string& logname, Debug::type t );
153  void delLevel( const std::string& logname, Debug::type t );
154  void level( const std::string& logname, Debug::type t );
155  void offLogFile( const std::string& logname );
156  void onLogFile( const std::string& logname );
157 
158  // найти лог..
159  std::shared_ptr<DebugStream> getLog( const std::string& logname );
160  bool logExist( std::shared_ptr<DebugStream>& l ) const;
161 
162  struct iLog
163  {
164  iLog( const std::shared_ptr<DebugStream>& l, const std::string& nm ): log(l), name(nm) {}
165  std::shared_ptr<DebugStream> log;
166  std::string name;
167 
168  // для сортировки "по алфавиту"
169  inline bool operator < ( const iLog& r ) const
170  {
171  return name < r.name;
172  }
173  };
174 
175  std::list<iLog> getLogList() const;
176  std::list<iLog> getLogList( const std::string& regexp_str ) const;
177 
178  friend std::ostream& operator<<(std::ostream& os, LogAgregator& la );
179  friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<LogAgregator> la );
180 
181  static std::vector<std::string> splitFirst( const std::string& lname, const std::string s = "/" );
182 
183  std::ostream& printLogList( std::ostream& os, const std::string& regexp_str = "" );
184  static std::ostream& printLogList( std::ostream& os, std::list<iLog>& lst );
185 
186  protected:
187  void logOnEvent( const std::string& s );
188  void addLog( std::shared_ptr<DebugStream> l, const std::string& lname, bool connect );
189  void addLogAgregator( std::shared_ptr<LogAgregator> la, const std::string& lname );
190 
191  // поиск лога по составному логу.."agregator/agregator2/.../logname"
192  std::shared_ptr<DebugStream> findLog( const std::string& lname ) const;
193 
194  // вывод в виде "дерева"
195  std::ostream& printTree(std::ostream& os, const std::string& g_tab = "");
196 
197  // получить список с именами (длинными) и с указателями на логи
198  std::list<iLog> makeLogNameList( const std::string& prefix ) const;
199 
200  private:
201  typedef std::unordered_map<std::string, std::shared_ptr<DebugStream>> LogMap;
202  LogMap lmap;
203 
204  typedef std::unordered_map<std::shared_ptr<DebugStream>, sigc::connection> ConnectionMap;
205  ConnectionMap conmap;
206  };
207  // -------------------------------------------------------------------------
208 } // end of uniset namespace
209 // -------------------------------------------------------------------------
210 #endif // LogAgregator_H_
211 // -------------------------------------------------------------------------