Skip to content

Commit

Permalink
feat: add C bindings for getting log levels (#334)
Browse files Browse the repository at this point in the history
This adds bindings for retrieving the log level name or enum value from
a string. It was already present in C++ but missing in C.
  • Loading branch information
cwaldren-ld authored Dec 13, 2023
1 parent 8a11435 commit 2b7f7d6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#pragma once

#include <launchdarkly/bindings/c/logging/log_level.h>

#include <launchdarkly/bindings/c/export.h>
#include <launchdarkly/bindings/c/status.h>

Expand All @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions libs/common/include/launchdarkly/bindings/c/logging/log_level.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/** @file */
// NOLINTBEGIN modernize-use-using

#pragma once

#include <launchdarkly/bindings/c/export.h>
#include <launchdarkly/bindings/c/status.h>

#include <stdbool.h>
#include <stddef.h>

#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
1 change: 1 addition & 0 deletions libs/common/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions libs/common/src/bindings/c/logging/log_level.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <launchdarkly/bindings/c/logging/log_level.h>

#include <launchdarkly/logging/log_level.hpp>

LD_EXPORT(char const*)
LDLogLevel_Name(LDLogLevel level, char const* name_if_unknown) {
return GetLogLevelName(static_cast<launchdarkly::LogLevel>(level),
name_if_unknown);
}

LD_EXPORT(LDLogLevel)
LDLogLevel_Enum(char const* level, LDLogLevel level_if_unknown) {
return static_cast<LDLogLevel>(GetLogLevelEnum(
level, static_cast<launchdarkly::LogLevel>(level_if_unknown)));
}
21 changes: 21 additions & 0 deletions libs/common/tests/logging_c_binding_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <launchdarkly/bindings/c/logging/log_level.h>

#include <gtest/gtest.h>

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<LDLogLevel>(4141), "unknown"));
}

TEST(LogLevelTests, LogLevelToEnum) {
constexpr auto unknown = static_cast<LDLogLevel>(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));
}

0 comments on commit 2b7f7d6

Please sign in to comment.