1//
2// NestedDiagnosticContext.cpp
3//
4// Library: Foundation
5// Package: Core
6// Module: NestedDiagnosticContext
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/NestedDiagnosticContext.h"
16#include "Poco/SingletonHolder.h"
17#include "Poco/ThreadLocal.h"
18
19
20namespace Poco {
21
22
23NestedDiagnosticContext::NestedDiagnosticContext()
24{
25}
26
27
28NestedDiagnosticContext::NestedDiagnosticContext(const NestedDiagnosticContext& ctx)
29{
30 _stack = ctx._stack;
31}
32
33
34NestedDiagnosticContext::~NestedDiagnosticContext()
35{
36}
37
38
39NestedDiagnosticContext& NestedDiagnosticContext::operator = (const NestedDiagnosticContext& ctx)
40{
41 if (&ctx != this)
42 _stack = ctx._stack;
43 return *this;
44}
45
46
47void NestedDiagnosticContext::push(const std::string& info)
48{
49 Context ctx;
50 ctx.info = info;
51 ctx.line = -1;
52 ctx.file = 0;
53 _stack.push_back(ctx);
54}
55
56
57void NestedDiagnosticContext::push(const std::string& info, int line, const char* filename)
58{
59 Context ctx;
60 ctx.info = info;
61 ctx.line = line;
62 ctx.file = filename;
63 _stack.push_back(ctx);
64}
65
66
67void NestedDiagnosticContext::pop()
68{
69 if (!_stack.empty())
70 _stack.pop_back();
71}
72
73
74int NestedDiagnosticContext::depth() const
75{
76 return int(_stack.size());
77}
78
79
80std::string NestedDiagnosticContext::toString() const
81{
82 std::string result;
83 for (Stack::const_iterator it = _stack.begin(); it != _stack.end(); ++it)
84 {
85 if (!result.empty())
86 result.append(":");
87 result.append(it->info);
88 }
89 return result;
90}
91
92
93void NestedDiagnosticContext::dump(std::ostream& ostr) const
94{
95 dump(ostr, "\n");
96}
97
98
99void NestedDiagnosticContext::dump(std::ostream& ostr, const std::string& delimiter) const
100{
101 for (Stack::const_iterator it = _stack.begin(); it != _stack.end(); ++it)
102 {
103 ostr << it->info;
104 if (it->file)
105 ostr << " (in \"" << it->file << "\", line " << it->line << ")";
106 ostr << delimiter;
107 }
108}
109
110
111void NestedDiagnosticContext::clear()
112{
113 _stack.clear();
114}
115
116
117namespace
118{
119 static ThreadLocal<NestedDiagnosticContext> ndc;
120}
121
122
123NestedDiagnosticContext& NestedDiagnosticContext::current()
124{
125 return ndc.get();
126}
127
128
129} // namespace Poco
130