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

Go to the SVN repository for this file.

1 /* $Id: perf_log.cpp 67675 2015-06-03 18:05:11Z grichenk $
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: Denis Vakatov, Vladimir Ivanov
27  *
28  * File Description:
29  * NCBI C++ API for timing-and-logging
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/perf_log.hpp>
35 #include <corelib/ncbi_param.hpp>
36 
37 
39 
40 
41 /// Turn on/off performance logging (globally)
42 // Registry file:
43 // [Log]
44 // PerfLogging = true/false
45 // Environment variable:
46 // LOG_PerfLogging
47 //
48 NCBI_PARAM_DECL(bool, Log, PerfLogging);
49 NCBI_PARAM_DEF_EX(bool, Log, PerfLogging, false, eParam_NoThread, LOG_PerfLogging);
50 typedef NCBI_PARAM_TYPE(Log, PerfLogging) TPerfLogging;
51 
52 
53 //////////////////////////////////////////////////////////////////////////////
54 //
55 // CPerfLogger
56 //
57 
58 bool CPerfLogger::IsON(void)
59 {
60  return TPerfLogging::GetDefault();
61 }
62 
63 
64 void CPerfLogger::SetON(bool enable) {
65  TPerfLogging::SetDefault(enable);
66 }
67 
68 
70  CTempString resource,
71  CTempString status_msg)
72 {
73  Suspend();
74  if ( !x_CheckValidity("Post") || !CPerfLogger::IsON() ) {
75  Discard();
76  return GetDiagContext().Extra();
77  }
79  if ( resource.empty() ) {
80  NCBI_THROW(CCoreException, eInvalidArg,
81  "CPerfLogger::Log: resource name is not specified");
82  }
83  args.push_back(SDiagMessage::TExtraArg("resource", resource));
84  if ( !status_msg.empty() ) {
85  args.push_back(SDiagMessage::TExtraArg("status_msg", status_msg));
86  }
87  double elapsed = m_StopWatch->Elapsed() + m_Adjustment;
88  CDiagContext_Extra extra = g_PostPerf((int)status,
89  elapsed < 0.0 ? 0.0 : elapsed, args);
90  Discard();
91  return extra;
92 }
93 
94 
95 
96 //////////////////////////////////////////////////////////////////////////////
97 //
98 // CPerfLogGuard
99 //
100 
101 void CPerfLogGuard::Post(int status, CTempString status_msg)
102 {
103  // Check a validity
104  if ( m_Logger.m_IsDiscarded ) {
105  ERR_POST_ONCE(Error << "Post() cannot be done, " \
106  "CPerfLogGuard is already discarded");
107  return;
108  }
109  // Check that logging is enabled to avoid extra 'extra' to be printed
110  if ( CPerfLogger::IsON() ) {
111  CDiagContext_Extra extra = m_Logger.Post(status, m_Resource, status_msg);
112  extra.Print(m_Parameters);
113  }
114  Discard();
115 }
116 
117 
void Post(CRequestStatus::ECode status, CTempString status_msg=CTempString())
Write the collected resource info and timing to the log.
Definition: perf_log.hpp:491
#define ERR_POST_ONCE(message)
Error posting only once during program execution.
Definition: ncbidiag.hpp:590
bool m_IsDiscarded
Definition: perf_log.hpp:165
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:62
bool empty(void) const
Return true if the represented string is empty (i.e., the length is zero)
Definition: tempstr.hpp:322
void Discard(void)
Discard the timing results; stop and deactivate the timer.
Definition: perf_log.hpp:386
double m_Adjustment
Definition: perf_log.hpp:166
SDiagMessage::TExtraArgs m_Parameters
Definition: perf_log.hpp:302
CDiagContext & GetDiagContext(void)
Get diag context instance.
Definition: logging.cpp:746
bool x_CheckValidity(const CTempString &err_msg) const
Definition: perf_log.hpp:411
CCoreException –.
Definition: ncbiexpt.hpp:1282
string m_Resource
Definition: perf_log.hpp:301
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
NCBI_PARAM_DEF_EX(bool, Log, PerfLogging, false, eParam_NoThread, LOG_PerfLogging)
static void SetON(bool enable=true)
Turn performance logging on/off globally.
Definition: perf_log.cpp:64
CDiagContext_Extra Extra(void) const
Create a temporary CDiagContext_Extra object.
Definition: ncbidiag.hpp:2091
CDiagContext_Extra g_PostPerf(int status, double timespan, SDiagMessage::TExtraArgs &args)
Definition: ncbidiag.cpp:7405
Temporary object for holding extra message arguments.
Definition: ncbidiag.hpp:1856
CDiagContext_Extra & Print(const string &name, const string &value)
The method does not print the argument, but adds it to the string.
Definition: ncbidiag.cpp:2125
void Suspend(void)
Suspend the timer.
Definition: perf_log.hpp:364
CPerfLogger –.
Definition: perf_log.hpp:79
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1014
CDiagContext_Extra Post(CRequestStatus::ECode status, CTempString resource, CTempString status_msg=CTempString())
Log the timing; stop and deactivate the timer.
Definition: perf_log.hpp:377
NCBI_PARAM_DECL(bool, Log, PerfLogging)
Turn on/off performance logging (globally)
double Elapsed(void) const
Return time elapsed since first Start() or last Restart() call (in seconds).
Definition: ncbitime.hpp:2727
Do not use per-thread values.
Definition: ncbi_param.hpp:465
Defines NCBI C++ API for timing-and-logging, classes, and macros.
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string...
Definition: ncbiexpt.hpp:546
void Discard(void)
Discard the results.
Definition: perf_log.hpp:499
CPerfLogger m_Logger
Definition: perf_log.hpp:300
list< TExtraArg > TExtraArgs
Definition: ncbidiag.hpp:1627
pair< string, string > TExtraArg
Definition: ncbidiag.hpp:1626
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
static bool IsON(void)
Is performance logging on, globally? Controlled by CParam(section="Log", entry="PerfLogging", default=false)
Definition: perf_log.cpp:58
CStopWatch * m_StopWatch
Definition: perf_log.hpp:163
typedef NCBI_PARAM_TYPE(Log, PerfLogging) TPerfLogging
Modified on Sat Aug 29 13:39:09 2015 by modify_doxy.py rev. 426318