Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/hello-cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
namespace net = boost::asio; // from <boost/asio.hpp>

using launchdarkly::ContextBuilder;
using launchdarkly::LogLevel;
using launchdarkly::client_side::Client;
using launchdarkly::client_side::ConfigBuilder;
using launchdarkly::client_side::DataSourceBuilder;
using launchdarkly::config::shared::builders::LoggingBuilder;

int main() {
net::io_context ioc;
Expand All @@ -36,6 +38,7 @@ int main() {
std::chrono::seconds{30}))
.WithReasons(true)
.UseReport(true))
.Logging(LoggingBuilder::BasicLogging().Level(LogLevel::kDebug))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some implicit constructors to make setting the logging method simpler. Here there is a basic logging builder getting converted into a LoggingBuilder.

.Events(launchdarkly::client_side::EventsBuilder().FlushInterval(
std::chrono::seconds(5)))
.Build()
Expand Down
11 changes: 9 additions & 2 deletions libs/client-sdk/src/client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <launchdarkly/config/shared/built/logging.hpp>
#include <launchdarkly/logging/console_backend.hpp>
#include <launchdarkly/logging/null_logger.hpp>

namespace launchdarkly::client_side {

Expand All @@ -35,8 +36,14 @@ static std::unique_ptr<IDataSource> MakeDataSource(
}

static Logger MakeLogger(config::shared::built::Logging const& config) {
// TODO: Use settings for logger.
return Logger(std::make_unique<logging::ConsoleBackend>("LaunchDarkly"));
if (config.disable_logging) {
return {std::make_shared<logging::NullLoggerBackend>()};
}
if (config.backend) {
return {config.backend};
}
return {
std::make_shared<logging::ConsoleBackend>(config.level, config.tag)};
}

ClientImpl::ClientImpl(Config config, Context context)
Expand Down
22 changes: 11 additions & 11 deletions libs/client-sdk/tests/data_source_event_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestHandler : public IDataSourceUpdateSink {
};

TEST(StreamingDataHandlerTests, HandlesPutMessage) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -49,7 +49,7 @@ TEST(StreamingDataHandlerTests, HandlesPutMessage) {
}

TEST(StreamingDataHandlerTests, HandlesEmptyPutMessage) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -64,7 +64,7 @@ TEST(StreamingDataHandlerTests, HandlesEmptyPutMessage) {
}

TEST(StreamingDataHandlerTests, BadJsonPut) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -77,7 +77,7 @@ TEST(StreamingDataHandlerTests, BadJsonPut) {
}

TEST(StreamingDataHandlerTests, BadSchemaPut) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -90,7 +90,7 @@ TEST(StreamingDataHandlerTests, BadSchemaPut) {
}

TEST(StreamingDataHandlerTests, HandlesPatchMessage) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -110,7 +110,7 @@ TEST(StreamingDataHandlerTests, HandlesPatchMessage) {
}

TEST(StreamingDataHandlerTests, BadJsonPatch) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -123,7 +123,7 @@ TEST(StreamingDataHandlerTests, BadJsonPatch) {
}

TEST(StreamingDataHandlerTests, BadSchemaPatch) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -136,7 +136,7 @@ TEST(StreamingDataHandlerTests, BadSchemaPatch) {
}

TEST(StreamingDataHandlerTests, HandlesDeleteMessage) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -153,7 +153,7 @@ TEST(StreamingDataHandlerTests, HandlesDeleteMessage) {
}

TEST(StreamingDataHandlerTests, BadJsonDelete) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -166,7 +166,7 @@ TEST(StreamingDataHandlerTests, BadJsonDelete) {
}

TEST(StreamingDataHandlerTests, BadSchemaDelete) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand All @@ -179,7 +179,7 @@ TEST(StreamingDataHandlerTests, BadSchemaDelete) {
}

TEST(StreamingDataHandlerTests, UnrecognizedVerb) {
auto logger = Logger(std::make_unique<logging::ConsoleBackend>("test"));
auto logger = Logger(std::make_shared<logging::ConsoleBackend>("test"));
auto test_handler = std::make_unique<TestHandler>();
DataSourceStatusManager status_manager;
DataSourceEventHandler stream_handler(test_handler.get(), logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <launchdarkly/config/shared/builders/endpoints_builder.hpp>
#include <launchdarkly/config/shared/builders/events_builder.hpp>
#include <launchdarkly/config/shared/builders/http_properties_builder.hpp>
#include <launchdarkly/config/shared/builders/logging_bulder.hpp>
#include <launchdarkly/config/shared/builders/logging_builder.hpp>
#include <launchdarkly/config/shared/config.hpp>

#include <optional>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#pragma once

#include <launchdarkly/config/shared/built/logging.hpp>
#include <launchdarkly/logging/log_backend.hpp>

#include <variant>

namespace launchdarkly::config::shared::builders {

/**
* Used to configure logging for the SDK.
*/
class LoggingBuilder {
public:
/**
* Class for configuring built in logging using the SDKs console logger.
*/
class BasicLogging {
public:
BasicLogging();

/**
* Set the enabled log level.
*
* @return A reference to this builder.
*/
BasicLogging& Level(LogLevel level);

/**
* Set a tag for this logger. This tag will be included at the start
* of log entries in square brackets.
*
* If the name was "LaunchDarkly", then log entries will be prefixed
* with "[LaunchDarkly]".
*
* @param name
* @return
*/
BasicLogging& Tag(std::string name);

private:
LogLevel level_;
std::string tag_;
friend class LoggingBuilder;
};

class CustomLogging {
public:
/**
* Set the backend to use for logging. The provided back-end should
* be thread-safe.
* @param backend The implementation of the backend.
* @return A reference to this builder.
*/
CustomLogging& Backend(std::shared_ptr<ILogBackend> backend);

private:
std::shared_ptr<ILogBackend> backend_;
friend class LoggingBuilder;
};

class NoLogging {};

using LoggingType = std::variant<BasicLogging, CustomLogging, NoLogging>;

/**
* Construct a logging builder.
*/
LoggingBuilder() = default;

/**
* Construct a logging builder from a custom logging builder.
* @param custom The custom logging builder to construct a builder from.
*/
LoggingBuilder(CustomLogging custom);

/**
* Construct a logging builder from a basic logging builder.
* @param basic The basic logging builder to construct a builder from.
*/
LoggingBuilder(BasicLogging basic);

/**
* Construct a logging builder from a no logging builder.
* @param no The no logging builder to construct a builder from.
*/
LoggingBuilder(NoLogging no);

/**
* Set the type of logging to use.
*
* Disable logging:
* ```
* builder.Logging(LoggingBuilder::NoLogging())
* ```
*
* Custom logging level:
* ```
* builder.Logging(LoggingBuilder::BasicLogging().Level(LogLevel::kDebug))
* ```
*
* @param logging
* @return
*/
LoggingBuilder& Logging(LoggingType logging);

/**
* Build a logger configuration. Intended for use by the SDK implementation.
*
* @return A built logging configuration.
*/
built::Logging Build() const;

private:
LoggingType logging_;
};

} // namespace launchdarkly::config::shared::builders

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
#pragma once

#include <launchdarkly/logging/log_backend.hpp>

#include <optional>

namespace launchdarkly::config::shared::built {

class Logging {
// TODO: Implement
/**
* Logging configuration.
*/
struct Logging {
/*
* True to disable logging.
*/
bool disable_logging;

/**
* Set to use a custom back-end.
*
* If set then name and level will not be used.
*/
std::shared_ptr<ILogBackend> backend;

/**
* When logging is enabled, and a custom backend is not
* in use, this will be the tag used for the logger.
*/
std::string tag;

/**
* When logging is enabled, and a custom backend is not
* in use, this will be the minimum log level.
*/
LogLevel level;
};

} // namespace launchdarkly::config::shared::built
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <launchdarkly/config/shared/built/events.hpp>
#include <launchdarkly/config/shared/built/http_properties.hpp>
#include <launchdarkly/config/shared/built/service_endpoints.hpp>
#include "sdks.hpp"
#include <launchdarkly/config/shared/sdks.hpp>
#include <launchdarkly/logging/log_level.hpp>

namespace launchdarkly::config::shared {

Expand All @@ -20,6 +21,9 @@ struct Defaults {
* @return
*/
static bool Offline() { return false; }

static std::string LogTag() { return "LaunchDarkly"; }
static launchdarkly::LogLevel LogLevel() { return LogLevel::kInfo; }
};

template <>
Expand Down
4 changes: 2 additions & 2 deletions libs/common/include/launchdarkly/logging/log_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ILogBackend {
* @param level The log level to check.
* @return Returns true if the level is enabled.
*/
virtual bool Enabled(LogLevel level) = 0;
virtual bool Enabled(LogLevel level) noexcept = 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed like a good idea to make these noexcept.


/**
* Write a message to the specified level.
* @param level The level to write the message for.
* @param message The message to Write.
*/
virtual void Write(LogLevel level, std::string message) = 0;
virtual void Write(LogLevel level, std::string message) noexcept = 0;

virtual ~ILogBackend(){};
};
Expand Down
Loading