NCBI C++ ToolKit
log_gbench.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: log_gbench.cpp 39928 2017-11-27 21:05:12Z katargir $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Roman Katargin
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
36 
38 
39 DEFINE_STATIC_MUTEX(s_LogGbenchMutex);
40 
42 static bool s_WasOpened = false;
43 
45 {
46  CMutexGuard LOCK( s_LogGbenchMutex );
47  if( !s_Instance ){
49  }
50 
51  return s_Instance;
52 }
53 
54 
56 {
57  if (s_WasOpened)
58  m_LogStream.flush();
59 
60  wxLog::Flush();
61 }
62 
63 
65 : m_OrigHandler(), m_Total()
66 {
68 
69  // set this hanlder as the default
70  SetDiagHandler(this, false);
71 
72  wxString path = CSysPath::ResolvePath( wxT("<home>/gblog.log") );
73  if( !s_WasOpened ){
74  m_LogStream.open( path.fn_str(), ofstream::out );
75 
76  s_WasOpened = true;
77  } else {
78  m_LogStream.open( path.fn_str(), ofstream::out | ofstream::app );
79  }
80 }
81 
83 {
84  CMutexGuard LOCK(s_LogGbenchMutex);
86 
87  if( m_LogStream.is_open() ){
88  m_LogStream.close();
89  }
90  s_Instance = NULL;
91 }
92 
93 void CwxLogDiagHandler::GetMsgCount(size_t& total, size_t& buffered)
94 {
95  CMutexGuard LOCK(s_LogGbenchMutex);
96  total = m_Total;
97  buffered = m_Buffer.size();
98 }
99 
100 bool CwxLogDiagHandler::GetMessage(size_t index, SMessage& msg, size_t& total) const
101 {
102  CMutexGuard LOCK(s_LogGbenchMutex);
103 
104  total = m_Total;
105 
106  if (index < m_Total - m_Buffer.size() || index >= m_Total)
107  return false;
108 
109  msg = m_Buffer[index - (m_Total - m_Buffer.size())];
110  return true;
111 
112 }
113 
115 {
116  static const char* auth_match = "Authorization";
117  static const size_t auth_match_len = strlen( auth_match );
118 
119  size_t match_ix = line.find( auth_match );
120  if( match_ix == string::npos ) return;
121 
122  size_t eoln_ix = line.find_first_of( "\r\n", match_ix );
123  if( eoln_ix == string::npos ){
124  eoln_ix = line.size();
125  }
126 
127  match_ix += auth_match_len + 2; // ": "
128  if( eoln_ix > match_ix ){
129  line.replace( match_ix, eoln_ix - match_ix, eoln_ix - match_ix, '*' );
130  }
131 }
132 
133 
135 {
136  CMutexGuard LOCK(s_LogGbenchMutex);
137 
138  {{
139  CNcbiOstrstream os;
140  os << msg;
141  string text = CNcbiOstrstreamToString(os);
143  m_LogStream << text;
144  }}
145 
146  string logline;
147  logline.assign(msg.m_Buffer, msg.m_Buffer + msg.m_BufferLen);
148  logline.erase(std::remove(logline.begin(), logline.end(), '\r'), logline.end());
149 
150  if( m_OrigHandler ){
151  SDiagMessage* patched_msg = const_cast<SDiagMessage*>(&msg);
152 
153  const char* saved_buffer = patched_msg->m_Buffer;
154  size_t saved_buffer_len = patched_msg->m_BufferLen;
155 
156  patched_msg->m_Buffer = logline.c_str();
157  patched_msg->m_BufferLen = logline.size();
158 
159  m_OrigHandler->Post( *patched_msg );
160 
161  patched_msg->m_Buffer = saved_buffer;
162  patched_msg->m_BufferLen = saved_buffer_len;
163  }
164 
165  SMessage message;
166  message.severity = msg.m_Severity;
167  message.time = msg.GetTime();
168 
169  // add infomation about source of the message to facilitate debugging
170  // maybe we make optional, based on _DEBUG build or NCBI_WXLOG_VERBOSE_DEBUGGING
171  //
172 
173  message.source = msg.m_File;
174  if (!message.source.empty()) {
175  // trim the base path
176  size_t pos = message.source.find("src");
177  if (pos!=string::npos) {
178  message.source = message.source.substr(pos);
179  }
180  else {
181  size_t pos = message.source.find("include");
182  if (pos!=string::npos) {
183  message.source = message.source.substr(pos);
184  }
185  }
186  message.source += ":";
187  message.source += NStr::NumericToString(msg.m_Line);
188  }
189 
190  message.message = logline;
191 
192  CRef<CEvent> ev(x_AddMessage(message));
193  LOCK.Release();
194  if (ev) CEventHandler::Post(ev);
195 }
196 
197 void CwxLogDiagHandler::DoLogRecord(wxLogLevel level,
198  const wxString& msg,
199  const wxLogRecordInfo& info)
200 {
201  CMutexGuard LOCK(s_LogGbenchMutex);
202 
203  SMessage message;
204  message.severity = eDiag_Info;
205  message.message = string(msg.ToUTF8());
206  CTime time(info.timestamp);
207  message.time = time.ToLocalTime();
208  message.wxMsg = true;
209 
210  switch (level) {
211  case wxLOG_Status:
212  case wxLOG_Progress:
213  return;
214 
215  case wxLOG_Debug:
216  message.severity = eDiag_Trace;
217  m_LogStream << "(Wx)" << message.message << endl;
218  break;
219  case wxLOG_Trace:
220  message.severity = eDiag_Trace;
221  m_LogStream << "(Wx)" << message.message << endl;
222  break;
223  case wxLOG_Message:
224  message.severity = eDiag_Info;
225  m_LogStream << "(Wx)" << message.message << endl;
226  break;
227  case wxLOG_Warning:
228  message.severity = eDiag_Warning;
229  m_LogStream << "(Wx)" << message.message << endl;
230  break;
231  case wxLOG_Error:
232  message.severity = eDiag_Error;
233  m_LogStream << message.time.AsString("M/D/y h:m:s ") << "Error(Wx): " << message.message << endl;
234  break;
235  case wxLOG_FatalError:
236  message.severity = eDiag_Fatal;
237  m_LogStream << message.time.AsString("M/D/y h:m:s ") << "Fatal(Wx): " << message.message << endl;
238  break;
239  }
240 
241  CRef<CEvent> ev(x_AddMessage(message));
242  LOCK.Release();
243  if (ev) CEventHandler::Post(ev);
244 }
245 
247 {
248  if (m_Buffer.size() == kMaxBuffered)
249  m_Buffer.pop_front();
250  m_Buffer.push_back(msg);
251  ++m_Total;
252 
253  if(GetListeners())
254  return new CDiagEvent(m_Total, m_Buffer.size());
255 
256  return 0;
257 }
258 
CEvent - generic event implementation TODO TODO - Attachments.
Definition: event.hpp:86
void Release()
Manually force the resource to be released.
Definition: guard.hpp:166
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
static wxString ResolvePath(const wxString &path, const wxString &rel_name)
Utility function to hide the platform specifics of locating our standard directories and files.
Definition: sys_path.cpp:106
CTime –.
Definition: ncbitime.hpp:296
an event for child notification
Definition: log_gbench.hpp:83
CwxLogDiagHandler - provides a centralized logging facility that integrates both with C++ Toolkit (CD...
Definition: log_gbench.hpp:59
bool GetMessage(size_t index, SMessage &msg, size_t &total) const
Definition: log_gbench.cpp:100
void GetMsgCount(size_t &total, size_t &buffered)
Definition: log_gbench.cpp:93
CEvent * x_AddMessage(const SMessage &msg)
Definition: log_gbench.cpp:246
virtual void Post(const SDiagMessage &msg)
post a message (CDiagHandler interface implementation)
Definition: log_gbench.cpp:134
void TweakContents(string &line)
Definition: log_gbench.cpp:114
CDiagHandler * m_OrigHandler
Definition: log_gbench.hpp:126
static CwxLogDiagHandler * GetInstance()
Definition: log_gbench.cpp:44
virtual void Flush()
Definition: log_gbench.cpp:55
TMessages m_Buffer
Definition: log_gbench.hpp:131
virtual void DoLogRecord(wxLogLevel level, const wxString &msg, const wxLogRecordInfo &info)
Definition: log_gbench.cpp:197
CNcbiOfstream m_LogStream
stream for logging all actions
Definition: log_gbench.hpp:129
static void DLIST_NAME() remove(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:90
std::ofstream out("events_result.xml")
main entry point for tests
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
const char * m_File
File name.
Definition: ncbidiag.hpp:1654
const char * m_Buffer
Not guaranteed to be '\0'-terminated!
Definition: ncbidiag.hpp:1652
size_t m_Line
Line number in file.
Definition: ncbidiag.hpp:1658
EDiagSev m_Severity
Severity level.
Definition: ncbidiag.hpp:1651
virtual void Post(const SDiagMessage &mess)=0
Post message to handler.
CDiagHandler * GetDiagHandler(bool take_ownership=false, bool *current_ownership=0)
Get the currently set diagnostic handler class.
Definition: ncbidiag.cpp:6329
size_t m_BufferLen
Length of m_Buffer.
Definition: ncbidiag.hpp:1653
void SetDiagHandler(CDiagHandler *handler, bool can_delete=true)
Set the diagnostic handler using the specified diagnostic handler class.
Definition: ncbidiag.cpp:6288
CTime GetTime(void) const
Get time and date - current or parsed.
Definition: ncbidiag.cpp:5341
@ eDiag_Trace
Trace message.
Definition: ncbidiag.hpp:657
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
@ eDiag_Warning
Warning message.
Definition: ncbidiag.hpp:652
@ eDiag_Fatal
Fatal error – guarantees exit(or abort)
Definition: ncbidiag.hpp:655
virtual const TListeners * GetListeners(int pool_name=ePool_Default) const
returns a set of listeners fro the specified pool
void Post(CRef< CEvent > evt, EDispatch disp_how=eDispatch_Default, int pool_name=ePool_Default)
Handles an event asynchronously (process and/or dispatch).
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
CTime & ToLocalTime(void)
Convert the time into local time.
Definition: ncbitime.hpp:2465
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
DEFINE_STATIC_MUTEX(s_LogGbenchMutex)
static CwxLogDiagHandler * s_Instance
Definition: log_gbench.cpp:41
static bool s_WasOpened
Definition: log_gbench.cpp:42
static void text(MDB_val *v)
Definition: mdb_dump.c:62
static MDB_envinfo info
Definition: mdb_load.c:37
#define wxT(x)
Definition: muParser.cpp:41
SDiagMessage –.
Definition: ncbidiag.hpp:1599
Modified on Sat Dec 09 04:44:13 2023 by modify_doxy.py rev. 669887