-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add C bindings for getting log levels (#334)
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
1 parent
8a11435
commit 2b7f7d6
Showing
5 changed files
with
91 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
libs/common/include/launchdarkly/bindings/c/logging/log_level.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |