📄 nesteddiagnosticcontext.h
字号:
//
// NestedDiagnosticContext.h
//
// $Id: //poco/Main/Foundation/include/Foundation/NestedDiagnosticContext.h#5 $
//
// Definition of the NestedDiagnosticContext class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_NestedDiagnosticContext_INCLUDED
#define Foundation_NestedDiagnosticContext_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef STD_VECTOR_INCLUDED
#include <vector>
#define STD_VECTOR_INCLUDED
#endif
#ifndef STD_OSTREAM_INCLUDED
#include <ostream>
#define STD_OSTREAM_INCLUDED
#endif
Foundation_BEGIN
class NDCScope;
class Foundation_API NestedDiagnosticContext
/// This class implements a Nested Diagnostic Context (NDC),
/// as described in Neil Harrison's article "Patterns for Logging
/// Diagnostic Messages" in "Pattern Languages of Program Design 3"
/// (Addison-Wesley).
///
/// A NDC maintains a stack of context information, consisting of
/// an informational string (e.g., a method name), as well as an
/// optional source code line number and file name.
/// NDCs are especially useful for tagging log messages with
/// context information which is very helpful in a multithreaded
/// server scenario.
/// Every thread has its own private NDC, which is automatically
/// created when needed and destroyed when the thread terminates.
///
/// The NDCScope (or NDC::Scope) class can be used to automatically
/// push information at the beginning of a scope, and to pop it
/// at the end.
/// The poco_ndc(info) macro augments the information with a
/// source code line number and file name.
{
public:
typedef NDCScope Scope;
NestedDiagnosticContext();
/// Creates the NestedDiagnosticContext.
NestedDiagnosticContext(const NestedDiagnosticContext& ctx);
/// Copy constructor.
~NestedDiagnosticContext();
/// Destroys the NestedDiagnosticContext.
NestedDiagnosticContext& operator = (const NestedDiagnosticContext& ctx);
/// Assignment operator.
void push(const std::string& info);
/// Pushes a context (without line number and filename) onto the stack.
void push(const std::string& info, int line, const char* filename);
/// Pushes a context (including line number and filename)
/// onto the stack. Filename must be a static string, such as the
/// one produced by the __FILE__ preprocessor macro.
void pop();
/// Pops the top-most context off the stack.
int depth() const;
/// Returns the depth (number of contexts) of the stack.
std::string toString() const;
/// Returns the stack as a string with entries
/// delimited by colons. The string does not contain
/// line numbers and filenames.
void dump(std::ostream& ostr) const;
/// Dumps the stack (including line number and filenames)
/// to the given stream. The entries are delimited by
/// a newline.
void dump(std::ostream& ostr, const std::string& delimiter) const;
/// Dumps the stack (including line number and filenames)
/// to the given stream.
void clear();
/// Clears the NDC stack.
static NestedDiagnosticContext& current();
/// Returns the current thread's NDC.
private:
struct Context
{
std::string info;
const char* file;
int line;
};
typedef std::vector<Context> Stack;
Stack _stack;
};
typedef NestedDiagnosticContext NDC;
class Foundation_API NDCScope
/// This class can be used to automatically push a context onto
/// the NDC stack at the beginning of a scope, and to pop
/// the context at the end of the scope.
{
public:
NDCScope(const std::string& info);
/// Pushes a context on the stack.
NDCScope(const std::string& info, int line, const char* filename);
/// Pushes a context on the stack.
~NDCScope();
/// Pops the top-most context off the stack.
};
//
// inlines
//
inline NDCScope::NDCScope(const std::string& info)
{
NestedDiagnosticContext::current().push(info);
}
inline NDCScope::NDCScope(const std::string& info, int line, const char* filename)
{
NestedDiagnosticContext::current().push(info, line, filename);
}
inline NDCScope::~NDCScope()
{
NestedDiagnosticContext::current().pop();
}
//
// helper macros
//
#define poco_ndc(msg) \
Foundation::NDCScope _theNdcScope(msg, __LINE__, __FILE__)
Foundation_END
#endif // Foundation_NestedDiagnosticContext_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -