xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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  private:
83  int pFileDes;
84  };
85 
86  //----------------------------------------------------------------------------
88  //----------------------------------------------------------------------------
89  class LogOutCerr: public LogOut
90  {
91  public:
92  virtual void Write( const std::string &message );
93  virtual ~LogOutCerr() {}
94  private:
96  };
97 
98  //----------------------------------------------------------------------------
100  //----------------------------------------------------------------------------
101  class Log
102  {
103  public:
104  //------------------------------------------------------------------------
106  //------------------------------------------------------------------------
107  enum LogLevel
108  {
109  NoMsg = 0,
110  ErrorMsg = 1,
112  InfoMsg = 3,
113  DebugMsg = 4,
114  DumpMsg = 5
115  };
116 
117  //------------------------------------------------------------------------
119  //------------------------------------------------------------------------
121  {
122  pOutput = new LogOutCerr();
123  int maxMask = (int)DumpMsg+1;
124  for( int i = 0; i < maxMask; ++i )
125  pMask[i] = 0xffffffffffffffffULL;
126  }
127 
128  //------------------------------------------------------------------------
129  // Destructor
130  //------------------------------------------------------------------------
132  {
133  delete pOutput;
134  }
135 
136  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  void Error( uint64_t topic, const char *format, ... );
140 
141  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  void Warning( uint64_t topic, const char *format, ... );
145 
146  //------------------------------------------------------------------------
148  //------------------------------------------------------------------------
149  void Info( uint64_t topic, const char *format, ... );
150 
151  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
154  void Debug( uint64_t topic, const char *format, ... );
155 
156  //------------------------------------------------------------------------
158  //------------------------------------------------------------------------
159  void Dump( uint64_t topic, const char *format, ... );
160 
161  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  void Say( LogLevel level, uint64_t topic, const char *format, va_list list );
170 
171  //------------------------------------------------------------------------
173  //------------------------------------------------------------------------
174  void SetLevel( LogLevel level )
175  {
176 #if __cplusplus >= 201103L
177  pLevel.store(level, std::memory_order_relaxed);
178 #else
179  pLevel = level;
180 #endif
181  }
182 
183  //------------------------------------------------------------------------
185  //------------------------------------------------------------------------
186  void SetLevel( const std::string &level )
187  {
188  LogLevel lvl;
189  if( StringToLogLevel( level, lvl ) )
190  SetLevel( lvl );
191  }
192 
193  //------------------------------------------------------------------------
195  //------------------------------------------------------------------------
196  void SetOutput( LogOut *output )
197  {
198  delete pOutput;
199  pOutput = output;
200  }
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  void SetMask( LogLevel level, uint64_t mask )
206  {
207  pMask[level] = mask;
208  }
209 
210  //------------------------------------------------------------------------
212  //------------------------------------------------------------------------
213  void SetMask( const std::string &level, uint64_t mask )
214  {
215  LogLevel lvl;
216  if( StringToLogLevel( level, lvl ) )
217  pMask[lvl] = mask;
218  }
219 
220  //------------------------------------------------------------------------
222  //------------------------------------------------------------------------
223  void SetTopicName( uint64_t topic, std::string name );
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
229  {
230 #if __cplusplus >= 201103L
231  LogLevel lvl = pLevel.load(std::memory_order_relaxed);
232  return lvl;
233 #else
234  return pLevel;
235 #endif
236  }
237 
238  //------------------------------------------------------------------------
240  //------------------------------------------------------------------------
241  void SetPid(pid_t pid)
242  {
243  pPid = pid;
244  }
245 
246  private:
247  typedef std::map<uint64_t, std::string> TopicMap;
248  std::string LogLevelToString( LogLevel level );
249  bool StringToLogLevel( const std::string &strLevel, LogLevel &level );
250  std::string TopicToString( uint64_t topic );
251 
252 #if __cplusplus >= 201103L
253  std::atomic<LogLevel> pLevel;
254 #else
256 #endif
257  uint64_t pMask[DumpMsg+1];
260  uint32_t pTopicMaxLength;
261  pid_t pPid;
262  };
263 }
264 
265 #endif // __XRD_CL_LOG_HH__
virtual void Write(const std::string &message)
std::map< uint64_t, std::string > TopicMap
Definition: XrdClLog.hh:247
void SetMask(const std::string &level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:213
~Log()
Definition: XrdClLog.hh:131
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:112
LogLevel GetLevel() const
Get the log level.
Definition: XrdClLog.hh:228
report errors
Definition: XrdClLog.hh:110
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:83
void SetTopicName(uint64_t topic, std::string name)
Map a topic number to a string.
TopicMap pTopicMap
Definition: XrdClLog.hh:259
void SetPid(pid_t pid)
Set pid.
Definition: XrdClLog.hh:241
virtual ~LogOutCerr()
Definition: XrdClLog.hh:93
Write log messages to stderr.
Definition: XrdClLog.hh:89
Write log messages to a file.
Definition: XrdClLog.hh:66
report warnings
Definition: XrdClLog.hh:111
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
report nothing
Definition: XrdClLog.hh:109
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:186
LogLevel
Log levels.
Definition: XrdClLog.hh:107
print debug info
Definition: XrdClLog.hh:113
pid_t pPid
Definition: XrdClLog.hh:261
XrdSysMutex pMutex
Definition: XrdClLog.hh:95
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:205
LogOut * pOutput
Definition: XrdClLog.hh:258
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:114
void SetLevel(LogLevel level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:174
LogOutFile()
Definition: XrdClLog.hh:69
virtual ~LogOut()
Definition: XrdClLog.hh:53
void SetOutput(LogOut *output)
Set the output that should be used.
Definition: XrdClLog.hh:196
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
LogLevel pLevel
Definition: XrdClLog.hh:255
uint64_t pMask[DumpMsg+1]
Definition: XrdClLog.hh:257
uint32_t pTopicMaxLength
Definition: XrdClLog.hh:260
Interface for logger outputs.
Definition: XrdClLog.hh:50
Log()
Constructor.
Definition: XrdClLog.hh:120
Handle diagnostics.
Definition: XrdClLog.hh:101