NestedDiagnosticContext.cpp 2.2 KB

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