context.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "context.h"
  2. #include "log.h"
  3. #include <util/thread/singleton.h>
  4. namespace NYql {
  5. namespace NLog {
  6. namespace {
  7. struct TThrowedLogContext {
  8. TString LocationWithLogContext; // separated with ': '
  9. };
  10. } // namspace
  11. void OutputLogCtx(IOutputStream* out, bool withBraces, bool skipSessionId) {
  12. const NImpl::TLogContextListItem* ctxList = NImpl::GetLogContextList();
  13. if (ctxList->HasNext()) {
  14. if (withBraces) {
  15. (*out) << '{';
  16. }
  17. // skip header stub element
  18. NImpl::TLogContextListItem* ctxItem = ctxList->Next;
  19. bool isFirst = true;
  20. while (ctxItem != ctxList) {
  21. for (const TString& name: *ctxItem) {
  22. if (!skipSessionId && !name.empty()) {
  23. if (!isFirst) {
  24. (*out) << '/';
  25. }
  26. (*out) << name;
  27. isFirst = false;
  28. }
  29. skipSessionId = false;
  30. }
  31. ctxItem = ctxItem->Next;
  32. }
  33. if (withBraces) {
  34. (*out) << TStringBuf("} ");
  35. }
  36. }
  37. }
  38. NImpl::TLogContextListItem* NImpl::GetLogContextList() {
  39. return FastTlsSingleton<NImpl::TLogContextListItem>();
  40. }
  41. std::pair<TString, TString> CurrentLogContextPath() {
  42. TString sessionId;
  43. const NImpl::TLogContextListItem* possibleRootLogCtx = NImpl::GetLogContextList()->Next;
  44. if (auto rootLogCtx = dynamic_cast<const NImpl::TLogContextSessionItem*>(possibleRootLogCtx)) {
  45. if (rootLogCtx->HasSessionId()) {
  46. sessionId = (*rootLogCtx->begin());
  47. }
  48. }
  49. TStringStream ss;
  50. OutputLogCtx(&ss, false, !sessionId.empty());
  51. return std::make_pair(sessionId, ss.Str());
  52. }
  53. TString ThrowedLogContextPath() {
  54. TThrowedLogContext* tlc = FastTlsSingleton<TThrowedLogContext>();
  55. return std::move(tlc->LocationWithLogContext);
  56. }
  57. TAutoPtr<TLogElement> TContextPreprocessor::Preprocess(
  58. TAutoPtr<TLogElement> element)
  59. {
  60. OutputLogCtx(element.Get(), true);
  61. return element;
  62. }
  63. void TYqlLogContextLocation::SetThrowedLogContextPath() const {
  64. TStringStream ss;
  65. ss << Location_ << TStringBuf(": ");
  66. OutputLogCtx(&ss, true);
  67. TThrowedLogContext* tlc = FastTlsSingleton<TThrowedLogContext>();
  68. tlc->LocationWithLogContext = ss.Str();
  69. }
  70. } // namespace NLog
  71. } // namespace NYql