XRootD
XrdClLog.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #ifndef __XRD_CL_LOG_HH__
26 #define __XRD_CL_LOG_HH__
27 
28 #include <cstdarg>
29 #include <string>
30 #include <map>
31 #include <cstdint>
32 #include "XrdSys/XrdSysPthread.hh"
33 
34 //------------------------------------------------------------------------------
35 // C++11 atomics are used to avoid illegal behavior when setting/getting the
36 // log level. To minimize costs across all platforms, we use
37 // std::memory_order_relaxed; this means threads may reorder SetLogLevel writes
38 // and the visibility is relatively undefined. However, we know the stores are
39 // at least atomic.
40 //------------------------------------------------------------------------------
41 #include <atomic>
42 
43 namespace XrdCl
44 {
45  //----------------------------------------------------------------------------
47  //----------------------------------------------------------------------------
48  class LogOut
49  {
50  public:
51  virtual ~LogOut() {}
52 
53  //------------------------------------------------------------------------
57  //------------------------------------------------------------------------
58  virtual void Write( const std::string &message ) = 0;
59  };
60 
61  //----------------------------------------------------------------------------
63  //----------------------------------------------------------------------------
64  class LogOutFile: public LogOut
65  {
66  public:
67  LogOutFile(): pFileDes(-1) {};
68  virtual ~LogOutFile() { Close(); };
69 
70  //------------------------------------------------------------------------
72  //------------------------------------------------------------------------
73  bool Open( const std::string &fileName );
74 
75  //------------------------------------------------------------------------
77  //------------------------------------------------------------------------
78  void Close();
79  virtual void Write( const std::string &message );
80 
81  private:
82  int pFileDes;
83  };
84 
85  //----------------------------------------------------------------------------
87  //----------------------------------------------------------------------------
88  class LogOutCerr: public LogOut
89  {
90  public:
91  virtual void Write( const std::string &message );
92  virtual ~LogOutCerr() {}
93  private:
94  XrdSysMutex pMutex;
95  };
96 
97  //----------------------------------------------------------------------------
99  //----------------------------------------------------------------------------
100  class Log
101  {
102  public:
103  //------------------------------------------------------------------------
105  //------------------------------------------------------------------------
106  enum LogLevel
107  {
108  NoMsg = 0,
109  ErrorMsg = 1,
111  InfoMsg = 3,
112  DebugMsg = 4,
113  DumpMsg = 5
114  };
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  Log(): pLevel( NoMsg ), pTopicMaxLength( 18 ), pPid(0)
120  {
121  pOutput = new LogOutCerr();
122  int maxMask = (int)DumpMsg+1;
123  for( int i = 0; i < maxMask; ++i )
124  pMask[i] = 0xffffffffffffffffULL;
125  }
126 
127  //------------------------------------------------------------------------
128  // Destructor
129  //------------------------------------------------------------------------
131  {
132  delete pOutput;
133  }
134 
135  //------------------------------------------------------------------------
137  //------------------------------------------------------------------------
138  void Error( uint64_t topic, const char *format, ... )
139 #if defined(__GNUC__)
140  __attribute__ ((__format__ (__printf__, 3, 4)))
141 #endif
142  ;
143 
144  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  void Warning( uint64_t topic, const char *format, ... )
148 #if defined(__GNUC__)
149  __attribute__ ((__format__ (__printf__, 3, 4)))
150 #endif
151  ;
152 
153  //------------------------------------------------------------------------
155  //------------------------------------------------------------------------
156  void Info( uint64_t topic, const char *format, ... )
157 #if defined(__GNUC__)
158  __attribute__ ((__format__ (__printf__, 3, 4)))
159 #endif
160  ;
161 
162  //------------------------------------------------------------------------
164  //------------------------------------------------------------------------
165  void Debug( uint64_t topic, const char *format, ... )
166 #if defined(__GNUC__)
167  __attribute__ ((__format__ (__printf__, 3, 4)))
168 #endif
169  ;
170 
171  //------------------------------------------------------------------------
173  //------------------------------------------------------------------------
174  void Dump( uint64_t topic, const char *format, ... )
175 #if defined(__GNUC__)
176  __attribute__ ((__format__ (__printf__, 3, 4)))
177 #endif
178  ;
179 
180  //------------------------------------------------------------------------
187  //------------------------------------------------------------------------
188  void Say( LogLevel level, uint64_t topic, const char *format, va_list list );
189 
190  //------------------------------------------------------------------------
192  //------------------------------------------------------------------------
193  void SetLevel( LogLevel level )
194  {
195 #if __cplusplus >= 201103L
196  pLevel.store(level, std::memory_order_relaxed);
197 #else
198  pLevel = level;
199 #endif
200  }
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  void SetLevel( const std::string &level )
206  {
207  LogLevel lvl;
208  if( StringToLogLevel( level, lvl ) )
209  SetLevel( lvl );
210  }
211 
212  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
215  void SetOutput( LogOut *output )
216  {
217  delete pOutput;
218  pOutput = output;
219  }
220 
221  //------------------------------------------------------------------------
223  //------------------------------------------------------------------------
224  void SetMask( LogLevel level, uint64_t mask )
225  {
226  pMask[level] = mask;
227  }
228 
229  //------------------------------------------------------------------------
231  //------------------------------------------------------------------------
232  void SetMask( const std::string &level, uint64_t mask )
233  {
234  LogLevel lvl;
235  if( StringToLogLevel( level, lvl ) )
236  pMask[lvl] = mask;
237  }
238 
239  //------------------------------------------------------------------------
241  //------------------------------------------------------------------------
242  void SetTopicName( uint64_t topic, std::string name );
243 
244 
245  //------------------------------------------------------------------------
247  //------------------------------------------------------------------------
248  inline uint64_t RegisterTopic( const std::string &topic )
249  {
250  uint64_t tpcnb = pTopicMap.rbegin()->first << 1;
251  SetTopicName( tpcnb, topic );
252  return tpcnb;
253  }
254 
255  //------------------------------------------------------------------------
257  //------------------------------------------------------------------------
259  {
260  LogLevel lvl = pLevel.load(std::memory_order_relaxed);
261  return lvl;
262  }
263 
264  //------------------------------------------------------------------------
266  //------------------------------------------------------------------------
267  void SetPid(pid_t pid)
268  {
269  pPid = pid;
270  }
271 
272  private:
273  typedef std::map<uint64_t, std::string> TopicMap;
274  std::string LogLevelToString( LogLevel level );
275  bool StringToLogLevel( const std::string &strLevel, LogLevel &level );
276  std::string TopicToString( uint64_t topic );
277 
278  std::atomic<LogLevel> pLevel;
279 
280  uint64_t pMask[DumpMsg+1];
281  LogOut *pOutput;
282  TopicMap pTopicMap;
283  uint32_t pTopicMaxLength;
284  pid_t pPid;
285  };
286 }
287 
288 #endif // __XRD_CL_LOG_HH__
#define __attribute__(x)
Write log messages to stderr.
Definition: XrdClLog.hh:89
virtual void Write(const std::string &message)
Definition: XrdClLog.cc:86
virtual ~LogOutCerr()
Definition: XrdClLog.hh:92
Write log messages to a file.
Definition: XrdClLog.hh:65
virtual void Write(const std::string &message)
Definition: XrdClLog.cc:67
void Close()
Close the log file.
Definition: XrdClLog.cc:55
bool Open(const std::string &fileName)
Open the log file.
Definition: XrdClLog.cc:39
virtual ~LogOutFile()
Definition: XrdClLog.hh:68
Interface for logger outputs.
Definition: XrdClLog.hh:49
virtual void Write(const std::string &message)=0
virtual ~LogOut()
Definition: XrdClLog.hh:51
Handle diagnostics.
Definition: XrdClLog.hh:101
void SetLevel(const std::string &level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:205
LogLevel
Log levels.
Definition: XrdClLog.hh:107
@ InfoMsg
print info
Definition: XrdClLog.hh:111
@ NoMsg
report nothing
Definition: XrdClLog.hh:108
@ WarningMsg
report warnings
Definition: XrdClLog.hh:110
@ DebugMsg
print debug info
Definition: XrdClLog.hh:112
@ ErrorMsg
report errors
Definition: XrdClLog.hh:109
@ DumpMsg
print details of the request and responses
Definition: XrdClLog.hh:113
uint64_t RegisterTopic(const std::string &topic)
Register new topic.
Definition: XrdClLog.hh:248
void SetMask(LogLevel level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:224
Log()
Constructor.
Definition: XrdClLog.hh:119
void SetTopicName(uint64_t topic, std::string name)
Map a topic number to a string.
Definition: XrdClLog.cc:163
void SetMask(const std::string &level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:232
void SetLevel(LogLevel level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:193
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition: XrdClLog.cc:231
LogLevel GetLevel() const
Get the log level.
Definition: XrdClLog.hh:258
void Warning(uint64_t topic, const char *format,...)
Report a warning.
Definition: XrdClLog.cc:248
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
Definition: XrdClLog.cc:299
void Info(uint64_t topic, const char *format,...)
Print an info.
Definition: XrdClLog.cc:265
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition: XrdClLog.cc:282
void Say(LogLevel level, uint64_t topic, const char *format, va_list list)
Definition: XrdClLog.cc:96
void SetOutput(LogOut *output)
Set the output that should be used.
Definition: XrdClLog.hh:215
void SetPid(pid_t pid)
Set pid.
Definition: XrdClLog.hh:267