UniSet  2.24.2
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  static 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  std::shared_ptr<DebugStream> findByLogName( const std::string& logname ) const;
162  // -------------------------------------------------------------------------
163 
164  struct iLog
165  {
166  iLog( const std::shared_ptr<DebugStream>& l, const std::string& nm ): log(l), name(nm) {}
167  std::shared_ptr<DebugStream> log;
168  std::string name;
169 
170  // для сортировки "по алфавиту"
171  inline bool operator < ( const iLog& r ) const
172  {
173  return name < r.name;
174  }
175  };
176 
177  std::list<iLog> getLogList() const;
178  std::list<iLog> getLogList( const std::string& regexp_str ) const;
179 
180  friend std::ostream& operator<<(std::ostream& os, LogAgregator& la );
181  friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<LogAgregator> la );
182 
183  static std::vector<std::string> splitFirst( const std::string& lname, const std::string s = "/" );
184 
185  std::ostream& printLogList( std::ostream& os, const std::string& regexp_str = "" ) const;
186  static std::ostream& printLogList( std::ostream& os, std::list<iLog>& lst );
187 
188  protected:
189  void logOnEvent( const std::string& s );
190  void addLog( std::shared_ptr<DebugStream> l, const std::string& lname, bool connect );
191  void addLogAgregator( std::shared_ptr<LogAgregator> la, const std::string& lname );
192 
193  // поиск лога по составному логу.."agregator/agregator2/.../logname"
194  std::shared_ptr<DebugStream> findLog( const std::string& lname ) const;
195 
196  // вывод в виде "дерева"
197  std::ostream& printTree(std::ostream& os, const std::string& g_tab = "") const;
198 
199  // получить список с именами (длинными) и с указателями на логи
200  std::list<iLog> makeLogNameList( const std::string& prefix ) const;
201 
202  // получить список по именам удовлетворяющим регулярному выражению (рекурсивная функция)
203  void getListByLogNameWithRule( std::list<iLog>& lst, const std::regex& rule, const std::string& prefix ) const;
204 
205  private:
206  typedef std::unordered_map<std::string, std::shared_ptr<DebugStream>> LogMap;
207  LogMap lmap;
208 
209  typedef std::unordered_map<std::shared_ptr<DebugStream>, sigc::connection> ConnectionMap;
210  ConnectionMap conmap;
211  };
212  // -------------------------------------------------------------------------
213 } // end of uniset namespace
214 // -------------------------------------------------------------------------
215 #endif // LogAgregator_H_
216 // -------------------------------------------------------------------------
Definition: DebugStream.h:62
Debug::type level() const noexcept
Returns the current debug level.
Definition: DebugStream.h:84
Definition: LogAgregator.h:134
virtual void logFile(const std::string &f, bool truncate=false) override
Sets the debugstreams' logfile to f.
Definition: LogAgregator.cc:51
Definition: CommonEventLoop.h:15
Definition: LogAgregator.h:165