NCBI C++ ToolKit
srv_diag.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef NETCACHE__SRV_DIAG__HPP
2 #define NETCACHE__SRV_DIAG__HPP
3 /* $Id: srv_diag.hpp 79772 2017-10-12 13:53:21Z gouriano $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Pavel Ivanov
29  *
30  * File Description:
31  */
32 
33 
34 
36 
37 
38 class CTempString;
39 class CRequestContext;
40 struct SSrvThread;
41 struct SLogData;
42 
43 
44 #define _VERIFY(x) if (x) {} else SRV_FATAL("assertion failed: " << #x)
45 
46 #ifdef _ASSERT
47 # undef _ASSERT
48 #endif
49 #ifdef _DEBUG
50 # define _ASSERT(x) _VERIFY(x)
51 #else
52 # define _ASSERT(x) do {} while (0)
53 #endif
54 
55 #ifdef assert
56 # undef assert
57 #endif
58 #define assert(x) _ASSERT(x)
59 
60 
61 /// Class used in all diagnostic logging.
62 /// This class effectively replaces things like CNcbiDiag and CDiagContext.
63 /// Any started messages should finish by call to Flush() or by destruction
64 /// of the object. One shouldn't start any new messages before he finishes
65 /// started one. After call to Flush() the same object can be reused to start
66 /// a new message. All messages will have request number from request context
67 /// assigned to currently executing task, except those methods that get
68 /// pointer to CRequestContext as input parameter (these methods should be
69 /// used very rarely).
70 /// CSrvDiagMsg objects cannot persist upon return to TaskServer code and
71 /// cannot be used in several threads simultaneously.
73 {
74 public:
75  CSrvDiagMsg(void);
76  ~CSrvDiagMsg(void);
77 
78  /// Severity levels for logging
79  enum ESeverity {
86  Fatal
87  };
88 
89  /// Checks if given severity level is visible, i.e. will make its way to
90  /// log file if someone will print anything on this level.
91  static bool IsSeverityVisible(ESeverity sev);
92 
93  /// Starts log message which will include severity, filename, line number
94  /// and function name.
96  const char* file,
97  int line,
98  const char* func) const;
99  /// Starts informational message which doesn't need to have filename, line
100  /// number or function name.
101  const CSrvDiagMsg& StartInfo(void) const;
102  const CSrvDiagMsg& StartInfo(CRequestContext* ctx) const;
103  /// Starts "request-start" message. Use PrintParam() methods to include
104  /// parameters into the message.
105  CSrvDiagMsg& StartRequest(void);
107  /// Starts "extra" message. Use PrintParam() methods to include
108  /// parameters into the message.
109  CSrvDiagMsg& PrintExtra(void);
111  /// Prints "request-stop" message. This is the only method that doesn't
112  /// start message, it prints the whole message which doesn't need to be
113  /// manually finished.
114  void StopRequest(void);
116  /// Finishes current message and prepare to start new one.
117  void Flush(void);
118  /// Starts the "old style" log message. Method shouldn't be used by
119  /// anybody except ERR_POST() macro used in legacy code.
120  const CSrvDiagMsg& StartOldStyle(const char* file, int line, const char* func);
121 
122  /// Adds parameter to "request-start" or "extra" message.
128  CSrvDiagMsg& PrintParam(CTempString name, double value);
129 
130  /// Converts input value to string and adds to started log message.
131  const CSrvDiagMsg& operator<< (CTempString str) const;
132  const CSrvDiagMsg& operator<< (const string& str) const;
133  const CSrvDiagMsg& operator<< (const char* str) const;
134  const CSrvDiagMsg& operator<< (Int4 num) const;
135  const CSrvDiagMsg& operator<< (Uint4 num) const;
136  const CSrvDiagMsg& operator<< (Int8 num) const;
137  const CSrvDiagMsg& operator<< (Uint8 num) const;
138 #ifdef NCBI_OS_MSWIN
139  const CSrvDiagMsg& operator<< (DWORD num) const {
140  return operator<< ((Uint8)num);
141  }
142 #endif
143  const CSrvDiagMsg& operator<< (double num) const;
144  const CSrvDiagMsg& operator<< (const void* ptr) const;
145  const CSrvDiagMsg& operator<< (const exception& ex) const;
146 
147 // Consider this section private as it's public for internal use only
148 // to minimize implementation-specific clutter in headers.
149 public:
150  /// Current thread created this object.
152  /// Log data from current thread.
154  /// Flag showing if "old style" message was started.
156 };
157 
158 
159 /// Macro to be used for printing log messages.
160 /// Macro usage is as follows:
161 /// SRV_LOG(Error, message << some_param << " more message");
162 #define SRV_LOG(sev, msg) \
163  do { \
164  if (CSrvDiagMsg::IsSeverityVisible(CSrvDiagMsg::sev)) { \
165  CSrvDiagMsg().StartSrvLog(CSrvDiagMsg::sev, \
166  __FILE__, __LINE__, \
167  NCBI_CURRENT_FUNCTION) \
168  << msg; \
169  } \
170  } \
171  while (0) \
172 /**/
173 #define SRV_FATAL(msg) \
174  do { \
175  SRV_LOG(Fatal, "Fatal error: " << msg); \
176  abort(); \
177  } while (0) \
178 
179 /// Macro to be used for printing informational messages.
180 /// Macro usage is as follows:
181 /// INFO(message << some_param << " more message");
182 #define INFO(msg) CSrvDiagMsg().StartInfo() << msg
183 
184 
186 
187 #endif /* NETCACHE__SRV_DIAG__HPP */
Class used in all diagnostic logging.
Definition: srv_diag.hpp:73
~CSrvDiagMsg(void)
Definition: logging.cpp:896
const CSrvDiagMsg & StartSrvLog(ESeverity sev, const char *file, int line, const char *func) const
Starts log message which will include severity, filename, line number and function name.
Definition: logging.cpp:909
CSrvDiagMsg & PrintExtra(void)
Starts "extra" message.
Definition: logging.cpp:1023
const CSrvDiagMsg & operator<<(CTempString str) const
Converts input value to string and adds to started log message.
Definition: logging.cpp:1138
CSrvDiagMsg & StartRequest(void)
Starts "request-start" message.
Definition: logging.cpp:998
CSrvDiagMsg & PrintParam(CTempString name, CTempString value)
Adds parameter to "request-start" or "extra" message.
Definition: logging.cpp:1040
SLogData * m_Data
Log data from current thread.
Definition: srv_diag.hpp:153
CSrvDiagMsg(void)
Definition: logging.cpp:880
SSrvThread * m_Thr
Current thread created this object.
Definition: srv_diag.hpp:151
bool m_OldStyle
Flag showing if "old style" message was started.
Definition: srv_diag.hpp:155
const CSrvDiagMsg & StartInfo(void) const
Starts informational message which doesn't need to have filename, line number or function name.
Definition: logging.cpp:953
void StopRequest(void)
Prints "request-stop" message.
Definition: logging.cpp:1084
void Flush(void)
Finishes current message and prepare to start new one.
Definition: logging.cpp:1116
static bool IsSeverityVisible(ESeverity sev)
Checks if given severity level is visible, i.e.
Definition: logging.cpp:903
const CSrvDiagMsg & StartOldStyle(const char *file, int line, const char *func)
Starts the "old style" log message.
Definition: logging.cpp:971
ESeverity
Severity levels for logging.
Definition: srv_diag.hpp:79
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CS_CONTEXT * ctx
Definition: t0006.c:12
static const char * str(char *buf, int n)
Definition: stats.c:84
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
FILE * file
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
unsigned int DWORD
Definition: sqltypes.h:98
Modified on Tue May 14 16:21:03 2024 by modify_doxy.py rev. 669887