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 <stdarg.h>
29 #include <string>
30 #include <map>
31 #include <stdint.h>
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 #if __cplusplus >= 201103L
42 #include <atomic>
43 #endif
44 
45 namespace XrdCl
46 {
47  //----------------------------------------------------------------------------
49  //----------------------------------------------------------------------------
50  class LogOut
51  {
52  public:
53  virtual ~LogOut() {}
54 
55  //------------------------------------------------------------------------
59  //------------------------------------------------------------------------
60  virtual void Write( const std::string &message ) = 0;
61  };
62 
63  //----------------------------------------------------------------------------
65  //----------------------------------------------------------------------------
66  class LogOutFile: public LogOut
67  {
68  public:
69  LogOutFile(): pFileDes(-1) {};
70  virtual ~LogOutFile() { Close(); };
71 
72  //------------------------------------------------------------------------
74  //------------------------------------------------------------------------
75  bool Open( const std::string &fileName );
76 
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  void Close();
81  virtual void Write( const std::string &message );
82 
83  private:
84  int pFileDes;
85  };
86 
87  //----------------------------------------------------------------------------
89  //----------------------------------------------------------------------------
90  class LogOutCerr: public LogOut
91  {
92  public:
93  virtual void Write( const std::string &message );
94  virtual ~LogOutCerr() {}
95  private:
97  };
98 
99  //----------------------------------------------------------------------------
101  //----------------------------------------------------------------------------
102  class Log
103  {
104  public:
105  //------------------------------------------------------------------------
107  //------------------------------------------------------------------------
108  enum LogLevel
109  {
110  NoMsg = 0,
111  ErrorMsg = 1,
113  InfoMsg = 3,
114  DebugMsg = 4,
115  DumpMsg = 5
116  };
117 
118  //------------------------------------------------------------------------
120  //------------------------------------------------------------------------
122  {
123  pOutput = new LogOutCerr();
124  int maxMask = (int)DumpMsg+1;
125  for( int i = 0; i < maxMask; ++i )
126  pMask[i] = 0xffffffffffffffffULL;
127  }
128 
129  //------------------------------------------------------------------------
130  // Destructor
131  //------------------------------------------------------------------------
133  {
134  delete pOutput;
135  }
136 
137  //------------------------------------------------------------------------
139  //------------------------------------------------------------------------
140  void Error( uint64_t topic, const char *format, ... );
141 
142  //------------------------------------------------------------------------
144  //------------------------------------------------------------------------
145  void Warning( uint64_t topic, const char *format, ... );
146 
147  //------------------------------------------------------------------------
149  //------------------------------------------------------------------------
150  void Info( uint64_t topic, const char *format, ... );
151 
152  //------------------------------------------------------------------------
154  //------------------------------------------------------------------------
155  void Debug( uint64_t topic, const char *format, ... );
156 
157  //------------------------------------------------------------------------
159  //------------------------------------------------------------------------
160  void Dump( uint64_t topic, const char *format, ... );
161 
162  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  void Say( LogLevel level, uint64_t topic, const char *format, va_list list );
171 
172  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  void SetLevel( LogLevel level )
176  {
177 #if __cplusplus >= 201103L
178  pLevel.store(level, std::memory_order_relaxed);
179 #else
180  pLevel = level;
181 #endif
182  }
183 
184  //------------------------------------------------------------------------
186  //------------------------------------------------------------------------
187  void SetLevel( const std::string &level )
188  {
189  LogLevel lvl;
190  if( StringToLogLevel( level, lvl ) )
191  SetLevel( lvl );
192  }
193 
194  //------------------------------------------------------------------------
196  //------------------------------------------------------------------------
197  void SetOutput( LogOut *output )
198  {
199  delete pOutput;
200  pOutput = output;
201  }
202 
203  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
206  void SetMask( LogLevel level, uint64_t mask )
207  {
208  pMask[level] = mask;
209  }
210 
211  //------------------------------------------------------------------------
213  //------------------------------------------------------------------------
214  void SetMask( const std::string &level, uint64_t mask )
215  {
216  LogLevel lvl;
217  if( StringToLogLevel( level, lvl ) )
218  pMask[lvl] = mask;
219  }
220 
221  //------------------------------------------------------------------------
223  //------------------------------------------------------------------------
224  void SetTopicName( uint64_t topic, std::string name );
225 
226 
227  //------------------------------------------------------------------------
229  //------------------------------------------------------------------------
230  inline uint64_t RegisterTopic( const std::string &topic )
231  {
232  uint64_t tpcnb = pTopicMap.rbegin()->first << 1;
233  SetTopicName( tpcnb, topic );
234  return tpcnb;
235  }
236 
237  //------------------------------------------------------------------------
239  //------------------------------------------------------------------------
241  {
242 #if __cplusplus >= 201103L
243  LogLevel lvl = pLevel.load(std::memory_order_relaxed);
244  return lvl;
245 #else
246  return pLevel;
247 #endif
248  }
249 
250  //------------------------------------------------------------------------
252  //------------------------------------------------------------------------
253  void SetPid(pid_t pid)
254  {
255  pPid = pid;
256  }
257 
258  private:
259  typedef std::map<uint64_t, std::string> TopicMap;
260  std::string LogLevelToString( LogLevel level );
261  bool StringToLogLevel( const std::string &strLevel, LogLevel &level );
262  std::string TopicToString( uint64_t topic );
263 
264 #if __cplusplus >= 201103L
265  std::atomic<LogLevel> pLevel;
266 #else
268 #endif
269  uint64_t pMask[DumpMsg+1];
272  uint32_t pTopicMaxLength;
273  pid_t pPid;
274  };
275 }
276 
277 #endif // __XRD_CL_LOG_HH__
virtual void Write(const std::string &message)
std::map< uint64_t, std::string > TopicMap
Definition: XrdClLog.hh:259
void SetMask(const std::string &level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:214
~Log()
Definition: XrdClLog.hh:132
virtual void Write(const std::string &message)
void Say(LogLevel level, uint64_t topic, const char *format, va_list list)
print info
Definition: XrdClLog.hh:113
report errors
Definition: XrdClLog.hh:111
bool Open(const std::string &fileName)
Open the log file.
void Warning(uint64_t topic, const char *format,...)
Report a warning.
void Info(uint64_t topic, const char *format,...)
Print an info.
int pFileDes
Definition: XrdClLog.hh:84
void SetTopicName(uint64_t topic, std::string name)
Map a topic number to a string.
TopicMap pTopicMap
Definition: XrdClLog.hh:271
void SetPid(pid_t pid)
Set pid.
Definition: XrdClLog.hh:253
virtual ~LogOutCerr()
Definition: XrdClLog.hh:94
Write log messages to stderr.
Definition: XrdClLog.hh:90
Write log messages to a file.
Definition: XrdClLog.hh:66
report warnings
Definition: XrdClLog.hh:112
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
report nothing
Definition: XrdClLog.hh:110
std::string LogLevelToString(LogLevel level)
Definition: XrdSysPthread.hh:165
void SetLevel(const std::string &level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:187
LogLevel
Log levels.
Definition: XrdClLog.hh:108
print debug info
Definition: XrdClLog.hh:114
pid_t pPid
Definition: XrdClLog.hh:273
Definition: XrdClAnyObject.hh:25
XrdSysMutex pMutex
Definition: XrdClLog.hh:96
void Close()
Close the log file.
bool StringToLogLevel(const std::string &strLevel, LogLevel &level)
virtual ~LogOutFile()
Definition: XrdClLog.hh:70
virtual void Write(const std::string &message)=0
void SetMask(LogLevel level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:206
LogOut * pOutput
Definition: XrdClLog.hh:270
void Error(uint64_t topic, const char *format,...)
Report an error.
std::string TopicToString(uint64_t topic)
print details of the request and responses
Definition: XrdClLog.hh:115
void SetLevel(LogLevel level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:175
LogOutFile()
Definition: XrdClLog.hh:69
uint64_t RegisterTopic(const std::string &topic)
Register new topic.
Definition: XrdClLog.hh:230
virtual ~LogOut()
Definition: XrdClLog.hh:53
void SetOutput(LogOut *output)
Set the output that should be used.
Definition: XrdClLog.hh:197
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
LogLevel GetLevel() const
Get the log level.
Definition: XrdClLog.hh:240
LogLevel pLevel
Definition: XrdClLog.hh:267
uint64_t pMask[DumpMsg+1]
Definition: XrdClLog.hh:269
uint32_t pTopicMaxLength
Definition: XrdClLog.hh:272
Interface for logger outputs.
Definition: XrdClLog.hh:50
Log()
Constructor.
Definition: XrdClLog.hh:121
Handle diagnostics.
Definition: XrdClLog.hh:102