summaryrefslogtreecommitdiffstats
path: root/contrib/libs/opentelemetry-cpp/sdk/src/common/global_log_handler.cc
blob: bd30262ba714ca756863da87345773a555065b78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/sdk/common/global_log_handler.h"

#include <iostream>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace common
{
namespace internal_log
{

namespace
{
struct GlobalLogHandlerData
{
  nostd::shared_ptr<LogHandler> handler;
  LogLevel log_level{LogLevel::Warning};

  GlobalLogHandlerData() : handler(nostd::shared_ptr<LogHandler>(new DefaultLogHandler)) {}
  ~GlobalLogHandlerData() { is_singleton_destroyed = true; }

  GlobalLogHandlerData(const GlobalLogHandlerData &) = delete;
  GlobalLogHandlerData(GlobalLogHandlerData &&)      = delete;

  GlobalLogHandlerData &operator=(const GlobalLogHandlerData &) = delete;
  GlobalLogHandlerData &operator=(GlobalLogHandlerData &&)      = delete;

  static GlobalLogHandlerData &Instance() noexcept;
  static bool is_singleton_destroyed;
};

bool GlobalLogHandlerData::is_singleton_destroyed = false;

GlobalLogHandlerData &GlobalLogHandlerData::Instance() noexcept
{
  static GlobalLogHandlerData instance;
  return instance;
}

}  // namespace

LogHandler::~LogHandler() {}

void DefaultLogHandler::Handle(LogLevel level,
                               const char *file,
                               int line,
                               const char *msg,
                               const sdk::common::AttributeMap & /* attributes */) noexcept
{
  std::stringstream output_s;
  output_s << "[" << LevelToString(level) << "] ";
  if (file != nullptr)
  {
    output_s << "File: " << file << ":" << line << " ";
  }
  if (msg != nullptr)
  {
    output_s << msg;
  }
  output_s << '\n';
  // TBD - print attributes

  switch (level)
  {
    case LogLevel::Error:
    case LogLevel::Warning:
      std::cerr << output_s.str();  // thread safe.
      break;
    case LogLevel::Info:
    case LogLevel::Debug:
      std::cout << output_s.str();  // thread safe.
      break;
    case LogLevel::None:
    default:
      break;
  }
}

void NoopLogHandler::Handle(LogLevel,
                            const char *,
                            int,
                            const char *,
                            const sdk::common::AttributeMap &) noexcept
{}

nostd::shared_ptr<LogHandler> GlobalLogHandler::GetLogHandler() noexcept
{
  if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
  {
    return nostd::shared_ptr<LogHandler>();
  }

  return GlobalLogHandlerData::Instance().handler;
}

void GlobalLogHandler::SetLogHandler(const nostd::shared_ptr<LogHandler> &eh) noexcept
{
  if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
  {
    return;
  }

  GlobalLogHandlerData::Instance().handler = eh;
}

LogLevel GlobalLogHandler::GetLogLevel() noexcept
{
  if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
  {
    return LogLevel::None;
  }

  return GlobalLogHandlerData::Instance().log_level;
}

void GlobalLogHandler::SetLogLevel(LogLevel level) noexcept
{
  if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
  {
    return;
  }
  GlobalLogHandlerData::Instance().log_level = level;
}

}  // namespace internal_log
}  // namespace common
}  // namespace sdk
OPENTELEMETRY_END_NAMESPACE