diff --git a/libs/common/include/launchdarkly/bindings/c/config/logging_builder.h b/libs/common/include/launchdarkly/bindings/c/config/logging_builder.h index caaa00033..6a392b1f5 100644 --- a/libs/common/include/launchdarkly/bindings/c/config/logging_builder.h +++ b/libs/common/include/launchdarkly/bindings/c/config/logging_builder.h @@ -3,6 +3,8 @@ #pragma once +#include + #include #include @@ -17,17 +19,6 @@ extern "C" { // only need to export C interface if typedef struct _LDLoggingBasicBuilder* LDLoggingBasicBuilder; typedef struct _LDLoggingCustomBuilder* LDLoggingCustomBuilder; -/** - * Defines the log levels used with the SDK's default logger, or a user-provided - * custom logger. - */ -enum LDLogLevel { - LD_LOG_DEBUG = 0, - LD_LOG_INFO = 1, - LD_LOG_WARN = 2, - LD_LOG_ERROR = 3, -}; - typedef bool (*EnabledFn)(enum LDLogLevel level, void* user_data); typedef void (*WriteFn)(enum LDLogLevel level, char const* msg, diff --git a/libs/common/include/launchdarkly/bindings/c/logging/log_level.h b/libs/common/include/launchdarkly/bindings/c/logging/log_level.h new file mode 100644 index 000000000..41ba38a6a --- /dev/null +++ b/libs/common/include/launchdarkly/bindings/c/logging/log_level.h @@ -0,0 +1,52 @@ +/** @file */ +// NOLINTBEGIN modernize-use-using + +#pragma once + +#include +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { // only need to export C interface if +// used by C++ source code +#endif + +/** + * Defines the log levels used with the SDK's default logger, or a user-provided + * custom logger. + */ +enum LDLogLevel { + LD_LOG_DEBUG = 0, + LD_LOG_INFO = 1, + LD_LOG_WARN = 2, + LD_LOG_ERROR = 3, +}; + +/** + * Lookup the name of a LDLogLevel. + * @param level Target level. + * @param level_if_unknown Default name to return if the level wasn't + * recognized. + * @return Name of the level as a string, or level_if_unknown if not recognized. + */ +LD_EXPORT(char const*) +LDLogLevel_Name(enum LDLogLevel level, char const* level_if_unknown); + +/** + * Lookup a LDLogLevel by name. + * @param level Name of level. + * @param level_if_unknown Default level to return if the level wasn't + * recognized. + * @return LDLogLevel matching the name, or level_if_unknown if not recognized. + */ +LD_EXPORT(enum LDLogLevel) +LDLogLevel_Enum(char const* level, enum LDLogLevel level_if_unknown); + +#ifdef __cplusplus +} +#endif + +// NOLINTEND modernize-use-using diff --git a/libs/common/src/CMakeLists.txt b/libs/common/src/CMakeLists.txt index 8ba969cb4..4a1c9f9dd 100644 --- a/libs/common/src/CMakeLists.txt +++ b/libs/common/src/CMakeLists.txt @@ -52,6 +52,7 @@ add_library(${LIBNAME} OBJECT bindings/c/flag_listener.cpp bindings/c/memory_routines.cpp bindings/c/data_source/error_info.cpp + bindings/c/logging/log_level.cpp log_level.cpp config/persistence_builder.cpp config/logging_builder.cpp diff --git a/libs/common/src/bindings/c/logging/log_level.cpp b/libs/common/src/bindings/c/logging/log_level.cpp new file mode 100644 index 000000000..e73e9c267 --- /dev/null +++ b/libs/common/src/bindings/c/logging/log_level.cpp @@ -0,0 +1,15 @@ +#include + +#include + +LD_EXPORT(char const*) +LDLogLevel_Name(LDLogLevel level, char const* name_if_unknown) { + return GetLogLevelName(static_cast(level), + name_if_unknown); +} + +LD_EXPORT(LDLogLevel) +LDLogLevel_Enum(char const* level, LDLogLevel level_if_unknown) { + return static_cast(GetLogLevelEnum( + level, static_cast(level_if_unknown))); +} diff --git a/libs/common/tests/logging_c_binding_test.cpp b/libs/common/tests/logging_c_binding_test.cpp new file mode 100644 index 000000000..429b6d012 --- /dev/null +++ b/libs/common/tests/logging_c_binding_test.cpp @@ -0,0 +1,21 @@ +#include + +#include + +TEST(LogLevelTests, LogLevelToString) { + ASSERT_STREQ("debug", LDLogLevel_Name(LD_LOG_DEBUG, "unknown")); + ASSERT_STREQ("info", LDLogLevel_Name(LD_LOG_INFO, "unknown")); + ASSERT_STREQ("warn", LDLogLevel_Name(LD_LOG_WARN, "unknown")); + ASSERT_STREQ("error", LDLogLevel_Name(LD_LOG_ERROR, "unknown")); + ASSERT_STREQ("unknown", + LDLogLevel_Name(static_cast(4141), "unknown")); +} + +TEST(LogLevelTests, LogLevelToEnum) { + constexpr auto unknown = static_cast(4141); + ASSERT_EQ(LD_LOG_DEBUG, LDLogLevel_Enum("debug", unknown)); + ASSERT_EQ(LD_LOG_INFO, LDLogLevel_Enum("info", unknown)); + ASSERT_EQ(LD_LOG_WARN, LDLogLevel_Enum("warn", unknown)); + ASSERT_EQ(LD_LOG_ERROR, LDLogLevel_Enum("error", unknown)); + ASSERT_EQ(unknown, LDLogLevel_Enum("potato", unknown)); +}