xrootd
XrdClLog.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_LOG_HH__
20 #define __XRD_CL_LOG_HH__
21 
22 #include <stdarg.h>
23 #include <string>
24 #include <map>
25 #include <stdint.h>
26 
27 #include "XrdCl/XrdClOptimizers.hh"
28 
29 #include "XrdSys/XrdSysPthread.hh"
30 
31 namespace XrdCl
32 {
33  //----------------------------------------------------------------------------
35  //----------------------------------------------------------------------------
36  class LogOut
37  {
38  public:
39  virtual ~LogOut() {}
40 
41  //------------------------------------------------------------------------
45  //------------------------------------------------------------------------
46  virtual void Write( const std::string &message ) = 0;
47  };
48 
49  //----------------------------------------------------------------------------
51  //----------------------------------------------------------------------------
52  class LogOutFile: public LogOut
53  {
54  public:
55  LogOutFile(): pFileDes(-1) {};
56  virtual ~LogOutFile() { Close(); };
57 
58  //------------------------------------------------------------------------
60  //------------------------------------------------------------------------
61  bool Open( const std::string &fileName );
62 
63  //------------------------------------------------------------------------
65  //------------------------------------------------------------------------
66  void Close();
67  virtual void Write( const std::string &message );
68  private:
69  int pFileDes;
70  };
71 
72  //----------------------------------------------------------------------------
74  //----------------------------------------------------------------------------
75  class LogOutCerr: public LogOut
76  {
77  public:
78  virtual void Write( const std::string &message );
79  private:
81  };
82 
83  //----------------------------------------------------------------------------
85  //----------------------------------------------------------------------------
86  class Log
87  {
88  public:
89  //------------------------------------------------------------------------
91  //------------------------------------------------------------------------
92  enum LogLevel
93  {
94  NoMsg = 0,
95  ErrorMsg = 1,
96  WarningMsg = 2,
97  InfoMsg = 3,
98  DebugMsg = 4,
99  DumpMsg = 5
100  };
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
106  {
107  pOutput = new LogOutCerr();
108  int maxMask = (int)DumpMsg+1;
109  for( int i = 0; i < maxMask; ++i )
110  pMask[i] = 0xffffffffffffffffULL;
111  }
112 
113  //------------------------------------------------------------------------
114  // Destructor
115  //------------------------------------------------------------------------
117  {
118  delete pOutput;
119  }
120 
121  //------------------------------------------------------------------------
123  //------------------------------------------------------------------------
124  void Error( uint64_t topic, const char *format, ... )
125  {
126  if( unlikely( pLevel < ErrorMsg ) )
127  return;
128 
129  if( unlikely( (topic & pMask[ErrorMsg]) == 0 ) )
130  return;
131 
132  va_list argList;
133  va_start( argList, format );
134  Say( ErrorMsg, topic, format, argList );
135  va_end( argList );
136  }
137 
138  //------------------------------------------------------------------------
140  //------------------------------------------------------------------------
141  void Warning( uint64_t topic, const char *format, ... )
142  {
143  if( unlikely( pLevel < WarningMsg ) )
144  return;
145 
146  if( unlikely( (topic & pMask[WarningMsg]) == 0 ) )
147  return;
148 
149  va_list argList;
150  va_start( argList, format );
151  Say( WarningMsg, topic, format, argList );
152  va_end( argList );
153  }
154 
155  //------------------------------------------------------------------------
157  //------------------------------------------------------------------------
158  void Info( uint64_t topic, const char *format, ... )
159  {
160  if( likely( pLevel < InfoMsg ) )
161  return;
162 
163  if( unlikely( (topic & pMask[InfoMsg]) == 0 ) )
164  return;
165 
166  va_list argList;
167  va_start( argList, format );
168  Say( InfoMsg, topic, format, argList );
169  va_end( argList );
170  }
171 
172  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  void Debug( uint64_t topic, const char *format, ... )
176  {
177  if( likely( pLevel < DebugMsg ) )
178  return;
179 
180  if( unlikely( (topic & pMask[DebugMsg]) == 0 ) )
181  return;
182 
183  va_list argList;
184  va_start( argList, format );
185  Say( DebugMsg, topic, format, argList );
186  va_end( argList );
187  }
188 
189  //------------------------------------------------------------------------
191  //------------------------------------------------------------------------
192  void Dump( uint64_t topic, const char *format, ... )
193  {
194  if( likely( pLevel < DumpMsg ) )
195  return;
196 
197  if( unlikely( (topic & pMask[DumpMsg]) == 0 ) )
198  return;
199 
200  va_list argList;
201  va_start( argList, format );
202  Say( DumpMsg, topic, format, argList );
203  va_end( argList );
204  }
205 
206  //------------------------------------------------------------------------
213  //------------------------------------------------------------------------
214  void Say( LogLevel level, uint64_t topic, const char *format, va_list list );
215 
216  //------------------------------------------------------------------------
218  //------------------------------------------------------------------------
219  void SetLevel( LogLevel level )
220  {
221  pLevel = level;
222  }
223 
224  //------------------------------------------------------------------------
226  //------------------------------------------------------------------------
227  void SetLevel( const std::string &level )
228  {
229  LogLevel lvl;
230  if( StringToLogLevel( level, lvl ) )
231  pLevel = lvl;
232  }
233 
234  //------------------------------------------------------------------------
236  //------------------------------------------------------------------------
237  void SetOutput( LogOut *output )
238  {
239  delete pOutput;
240  pOutput = output;
241  }
242 
243  //------------------------------------------------------------------------
245  //------------------------------------------------------------------------
246  void SetMask( LogLevel level, uint64_t mask )
247  {
248  pMask[level] = mask;
249  }
250 
251  //------------------------------------------------------------------------
253  //------------------------------------------------------------------------
254  void SetMask( const std::string &level, uint64_t mask )
255  {
256  LogLevel lvl;
257  if( StringToLogLevel( level, lvl ) )
258  pMask[lvl] = mask;
259  }
260 
261  //------------------------------------------------------------------------
263  //------------------------------------------------------------------------
264  void SetTopicName( uint64_t topic, std::string name );
265 
266  //------------------------------------------------------------------------
268  //------------------------------------------------------------------------
270  {
271  return pLevel;
272  }
273 
274  private:
275  typedef std::map<uint64_t, std::string> TopicMap;
276  std::string LogLevelToString( LogLevel level );
277  bool StringToLogLevel( const std::string &strLevel, LogLevel &level );
278  std::string TopicToString( uint64_t topic );
279 
281  uint64_t pMask[DumpMsg+1];
284  uint32_t pTopicMaxLength;
285  };
286 }
287 
288 #endif // __XRD_CL_LOG_HH__