Skip to content

Commit

Permalink
feat!: default to null logger (#187)
Browse files Browse the repository at this point in the history
The library now uses a NullLogger by default if no logger is specified.
This means that no logs will be printed unless a customer logger is
provided.
  • Loading branch information
dmehala authored Feb 18, 2025
1 parent dc5781d commit 9f7d0aa
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ cc_library(
"src/datadog/version.cpp",
"src/datadog/w3c_propagation.cpp",
"src/datadog/base64.h",
"src/datadog/cerr_logger.h",
"src/datadog/config_manager.h",
"src/datadog/collector_response.h",
"src/datadog/datadog_agent.h",
Expand All @@ -62,6 +61,7 @@ cc_library(
"src/datadog/json_serializer.h",
"src/datadog/limiter.h",
"src/datadog/msgpack.h",
"src/datadog/null_logger.h",
"src/datadog/parse_util.h",
"src/datadog/platform_util.h",
"src/datadog/random.h",
Expand All @@ -78,6 +78,7 @@ cc_library(
"src/datadog/w3c_propagation.h",
],
hdrs = [
"include/datadog/cerr_logger.h",
"include/datadog/clock.h",
"include/datadog/collector.h",
"include/datadog/config.h",
Expand Down
2 changes: 2 additions & 0 deletions examples/hasher/hasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// canonical format. Produce a trace whose structure reflects the directory
// structure.

#include <datadog/cerr_logger.h>
#include <datadog/span_config.h>
#include <datadog/tracer.h>
#include <datadog/tracer_config.h>
Expand Down Expand Up @@ -133,6 +134,7 @@ int main() {
dd::TracerConfig config;
config.service = "dd-trace-cpp-example";
config.environment = "dev";
config.logger = std::make_shared<datadog::tracing::CerrLogger>();

auto validated = dd::finalize_config(config);
if (auto *error = validated.if_error()) {
Expand Down
2 changes: 2 additions & 0 deletions examples/http-server/proxy/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <optional>
#include <string_view>

#include "datadog/cerr_logger.h"
#include "datadog/dict_reader.h"
#include "datadog/dict_writer.h"
#include "datadog/span.h"
Expand All @@ -27,6 +28,7 @@ int main() {
dd::TracerConfig config;
config.service = "dd-trace-cpp-http-server-example-proxy";
config.service_type = "proxy";
config.logger = std::make_shared<datadog::tracing::CerrLogger>();

// `finalize_config` validates `config` and applies any settings from
// environment variables, such as `DD_AGENT_HOST`.
Expand Down
2 changes: 2 additions & 0 deletions examples/http-server/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//
// will deliver a response after approximately 23 milliseconds.

#include <datadog/cerr_logger.h>
#include <datadog/clock.h>
#include <datadog/dict_reader.h>
#include <datadog/dict_writer.h>
Expand Down Expand Up @@ -110,6 +111,7 @@ int main() {
dd::TracerConfig config;
config.service = "dd-trace-cpp-http-server-example-server";
config.service_type = "server";
config.logger = std::make_shared<datadog::tracing::CerrLogger>();

// `finalize_config` validates `config` and applies any settings from
// environment variables, such as `DD_AGENT_HOST`.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/datadog/cerr_logger.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cerr_logger.h"
#include <datadog/cerr_logger.h>

#include <iostream>

Expand Down
25 changes: 25 additions & 0 deletions src/datadog/null_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

// This component provides a class, `NullLogger`, that implements the `Logger`
// interface from `logger.h`.
// `NullLogger` is a no-op logger, meaning it doesn't log anything.
//
// `NullLogger` is the default logger used by `Tracer` unless otherwise
// configured in `TracerConfig`.

#include <datadog/logger.h>

namespace datadog {
namespace tracing {

class NullLogger : public Logger {
public:
void log_error(const LogFunc&) override{};
void log_startup(const LogFunc&) override{};

void log_error(const Error&) override{};
void log_error(StringView) override{};
};

} // namespace tracing
} // namespace datadog
4 changes: 2 additions & 2 deletions src/datadog/tracer_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <unordered_map>
#include <vector>

#include "cerr_logger.h"
#include "datadog_agent.h"
#include "json.hpp"
#include "null_logger.h"
#include "parse_util.h"
#include "platform_util.h"
#include "string_util.h"
Expand Down Expand Up @@ -240,7 +240,7 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &config) {
Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &user_config,
const Clock &clock) {
auto logger =
user_config.logger ? user_config.logger : std::make_shared<CerrLogger>();
user_config.logger ? user_config.logger : std::make_shared<NullLogger>();

Expected<TracerConfig> env_config = load_tracer_env_config(*logger);
if (auto error = env_config.if_error()) {
Expand Down
7 changes: 0 additions & 7 deletions test/mocks/loggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

using namespace datadog::tracing;

struct NullLogger : public Logger {
void log_error(const LogFunc&) override {}
void log_startup(const LogFunc&) override {}
void log_error(const Error&) override {}
void log_error(StringView) override {}
};

struct MockLogger : public Logger {
struct Entry {
enum Kind { DD_ERROR, STARTUP } kind;
Expand Down
2 changes: 1 addition & 1 deletion test/remote_config/test_remote_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "catch.hpp"
#include "datadog/json.hpp"
#include "datadog/remote_config/remote_config.h"
#include "mocks/loggers.h"
#include "null_logger.h"

namespace rc = datadog::remote_config;
using namespace datadog::tracing;
Expand Down
1 change: 1 addition & 0 deletions test/test_curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "datadog/clock.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

using namespace datadog::tracing;
Expand Down
2 changes: 1 addition & 1 deletion test/test_smoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <datadog/tracer.h>
#include <datadog/tracer_config.h>

#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

using namespace datadog::tracing;
Expand Down
1 change: 1 addition & 0 deletions test/test_span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "mocks/dict_readers.h"
#include "mocks/dict_writers.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

using namespace datadog::tracing;
Expand Down
2 changes: 1 addition & 1 deletion test/test_span_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <utility>

#include "mocks/collectors.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

using namespace datadog::tracing;
Expand Down
2 changes: 1 addition & 1 deletion test/test_trace_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <ostream>

#include "mocks/collectors.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

namespace std {
Expand Down
1 change: 1 addition & 0 deletions test/test_trace_segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mocks/dict_readers.h"
#include "mocks/dict_writers.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

using namespace datadog::tracing;
Expand Down
1 change: 1 addition & 0 deletions test/test_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "mocks/dict_readers.h"
#include "mocks/dict_writers.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

namespace datadog {
Expand Down
1 change: 1 addition & 0 deletions test/test_tracer_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mocks/collectors.h"
#include "mocks/event_schedulers.h"
#include "mocks/loggers.h"
#include "null_logger.h"
#include "test.h"

namespace datadog {
Expand Down

0 comments on commit 9f7d0aa

Please sign in to comment.