xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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:
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  //------------------------------------------------------------------------
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 
140  //------------------------------------------------------------------------
142  //------------------------------------------------------------------------
143  void Warning( uint64_t topic, const char *format, ... );
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  void Info( uint64_t topic, const char *format, ... );
149 
150  //------------------------------------------------------------------------
152  //------------------------------------------------------------------------
153  void Debug( uint64_t topic, const char *format, ... );
154 
155  //------------------------------------------------------------------------
157  //------------------------------------------------------------------------
158  void Dump( uint64_t topic, const char *format, ... );
159 
160  //------------------------------------------------------------------------
167  //------------------------------------------------------------------------
168  void Say( LogLevel level, uint64_t topic, const char *format, va_list list );
169 
170  //------------------------------------------------------------------------
172  //------------------------------------------------------------------------
173  void SetLevel( LogLevel level )
174  {
175 #if __cplusplus >= 201103L
176  pLevel.store(level, std::memory_order_relaxed);
177 #else
178  pLevel = level;
179 #endif
180  }
181 
182  //------------------------------------------------------------------------
184  //------------------------------------------------------------------------
185  void SetLevel( const std::string &level )
186  {
187  LogLevel lvl;
188  if( StringToLogLevel( level, lvl ) )
189  SetLevel( lvl );
190  }
191 
192  //------------------------------------------------------------------------
194  //------------------------------------------------------------------------
195  void SetOutput( LogOut *output )
196  {
197  delete pOutput;
198  pOutput = output;
199  }
200 
201  //------------------------------------------------------------------------
203  //------------------------------------------------------------------------
204  void SetMask( LogLevel level, uint64_t mask )
205  {
206  pMask[level] = mask;
207  }
208 
209  //------------------------------------------------------------------------
211  //------------------------------------------------------------------------
212  void SetMask( const std::string &level, uint64_t mask )
213  {
214  LogLevel lvl;
215  if( StringToLogLevel( level, lvl ) )
216  pMask[lvl] = mask;
217  }
218 
219  //------------------------------------------------------------------------
221  //------------------------------------------------------------------------
222  void SetTopicName( uint64_t topic, std::string name );
223 
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
228  inline uint64_t RegisterTopic( const std::string &topic )
229  {
230  uint64_t tpcnb = pTopicMap.rbegin()->first << 1;
231  SetTopicName( tpcnb, topic );
232  return tpcnb;
233  }
234 
235  //------------------------------------------------------------------------
237  //------------------------------------------------------------------------
239  {
240  LogLevel lvl = pLevel.load(std::memory_order_relaxed);
241  return lvl;
242  }
243 
244  //------------------------------------------------------------------------
246  //------------------------------------------------------------------------
247  void SetPid(pid_t pid)
248  {
249  pPid = pid;
250  }
251 
252  private:
253  typedef std::map<uint64_t, std::string> TopicMap;
254  std::string LogLevelToString( LogLevel level );
255  bool StringToLogLevel( const std::string &strLevel, LogLevel &level );
256  std::string TopicToString( uint64_t topic );
257 
258  std::atomic<LogLevel> pLevel;
259 
260  uint64_t pMask[DumpMsg+1];
263  uint32_t pTopicMaxLength;
264  pid_t pPid;
265  };
266 }
267 
268 #endif // __XRD_CL_LOG_HH__
virtual void Write(const std::string &message)
std::map< uint64_t, std::string > TopicMap
Definition: XrdClLog.hh:253
void SetMask(const std::string &level, uint64_t mask)
Sets the mask for the topics of messages that should be printed.
Definition: XrdClLog.hh:212
~Log()
Definition: XrdClLog.hh:130
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:111
LogLevel GetLevel() const
Get the log level.
Definition: XrdClLog.hh:238
report errors
Definition: XrdClLog.hh:109
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:82
void SetTopicName(uint64_t topic, std::string name)
Map a topic number to a string.
TopicMap pTopicMap
Definition: XrdClLog.hh:262
void SetPid(pid_t pid)
Set pid.
Definition: XrdClLog.hh:247
virtual ~LogOutCerr()
Definition: XrdClLog.hh:92
Write log messages to stderr.
Definition: XrdClLog.hh:88
Write log messages to a file.
Definition: XrdClLog.hh:64
report warnings
Definition: XrdClLog.hh:110
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
report nothing
Definition: XrdClLog.hh:108
std::string LogLevelToString(LogLevel level)
Definition: XrdSysPthread.hh:164
void SetLevel(const std::string &level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:185
LogLevel
Log levels.
Definition: XrdClLog.hh:106
print debug info
Definition: XrdClLog.hh:112
pid_t pPid
Definition: XrdClLog.hh:264
XrdSysMutex pMutex
Definition: XrdClLog.hh:94
void Close()
Close the log file.
bool StringToLogLevel(const std::string &strLevel, LogLevel &level)
virtual ~LogOutFile()
Definition: XrdClLog.hh:68
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:204
LogOut * pOutput
Definition: XrdClLog.hh:261
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:113
std::atomic< LogLevel > pLevel
Definition: XrdClLog.hh:258
void SetLevel(LogLevel level)
Set the level of the messages that should be sent to the destination.
Definition: XrdClLog.hh:173
LogOutFile()
Definition: XrdClLog.hh:67
uint64_t RegisterTopic(const std::string &topic)
Register new topic.
Definition: XrdClLog.hh:228
virtual ~LogOut()
Definition: XrdClLog.hh:51
void SetOutput(LogOut *output)
Set the output that should be used.
Definition: XrdClLog.hh:195
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
uint64_t pMask[DumpMsg+1]
Definition: XrdClLog.hh:260
uint32_t pTopicMaxLength
Definition: XrdClLog.hh:263
Interface for logger outputs.
Definition: XrdClLog.hh:48
Log()
Constructor.
Definition: XrdClLog.hh:119
Handle diagnostics.
Definition: XrdClLog.hh:100