Use std::string_view instead of const char* in the public API

This commit is contained in:
Oliver Hamlet
2025-04-04 19:11:07 +01:00
parent fac057490d
commit 78ef049093
8 changed files with 22 additions and 19 deletions
+2 -1
View File
@@ -29,6 +29,7 @@
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include "loot/api_decorator.h"
#include "loot/enum/game_type.h"
@@ -55,7 +56,7 @@ namespace loot {
* level of the message being logged, and the second is the message.
*/
LOOT_API void SetLoggingCallback(
std::function<void(LogLevel, const char*)> callback);
std::function<void(LogLevel, std::string_view)> callback);
/**
* @brief Set the log severity level.
+1 -1
View File
@@ -39,7 +39,7 @@ public:
/**
* The name of the group to which all plugins belong by default.
*/
static constexpr const char* DEFAULT_NAME = "default";
static constexpr std::string_view DEFAULT_NAME = "default";
/**
* Construct a Group with the name "default" and an empty set of groups to
+1 -1
View File
@@ -41,7 +41,7 @@ public:
* The code for the default language assumed for message content, which is
* "en" (English).
*/
static constexpr const char* DEFAULT_LANGUAGE = "en";
static constexpr std::string_view DEFAULT_LANGUAGE = "en";
/**
* Construct a MessageContent object with an empty English message string.
+1 -1
View File
@@ -83,7 +83,7 @@ std::filesystem::path ResolvePath(const std::filesystem::path& path) {
}
LOOT_API void SetLoggingCallback(
std::function<void(LogLevel, const char*)> callback) {
std::function<void(LogLevel, std::string_view)> callback) {
const auto logger = createLogger(callback);
spdlog::drop(logger->name());
+3 -3
View File
@@ -76,7 +76,7 @@ spdlog::level::level_enum mapToSpdlog(LogLevel severity) {
class SpdLoggingSink : public spdlog::sinks::base_sink<std::mutex> {
public:
explicit SpdLoggingSink(std::function<void(LogLevel, const char*)> callback) {
explicit SpdLoggingSink(std::function<void(LogLevel, std::string_view)> callback) {
this->callback = callback;
}
@@ -91,7 +91,7 @@ protected:
void flush_() override {}
private:
std::function<void(LogLevel, const char*)> callback;
std::function<void(LogLevel, std::string_view)> callback;
};
}
@@ -99,7 +99,7 @@ namespace loot {
std::shared_ptr<spdlog::logger> getLogger() { return spdlog::get(std::string(LOGGER_NAME)); }
std::shared_ptr<spdlog::logger> createLogger(
std::function<void(LogLevel, const char*)> callback) {
std::function<void(LogLevel, std::string_view)> callback) {
auto sink = std::make_shared<SpdLoggingSink>(callback);
auto logger = std::make_shared<spdlog::logger>(std::string(LOGGER_NAME), sink);
logger->set_level(spdlog::level::level_enum::trace);
+3 -1
View File
@@ -24,6 +24,8 @@
#ifndef LOOT_API_HELPERS_LOGGING
#define LOOT_API_HELPERS_LOGGING
#include <string_view>
#include <spdlog/spdlog.h>
#include "loot/enum/log_level.h"
@@ -32,7 +34,7 @@ namespace loot {
std::shared_ptr<spdlog::logger> getLogger();
std::shared_ptr<spdlog::logger> createLogger(
std::function<void(LogLevel, const char*)> callback);
std::function<void(LogLevel, std::string_view)> callback);
void setLoggerLevel(LogLevel level);
}
+1 -1
View File
@@ -39,7 +39,7 @@ PluginSortingData::PluginSortingData(const PluginSortingInterface* plugin,
name_(plugin == nullptr ? std::string() : plugin->GetName()),
isMaster_(plugin != nullptr && plugin->IsMaster()),
group_(userMetadata.GetGroup().value_or(
masterlistMetadata.GetGroup().value_or(Group::DEFAULT_NAME))),
masterlistMetadata.GetGroup().value_or(std::string(Group::DEFAULT_NAME)))),
masterlistLoadAfter_(masterlistMetadata.GetLoadAfterFiles()),
userLoadAfter_(userMetadata.GetLoadAfterFiles()),
masterlistReq_(masterlistMetadata.GetRequirements()),
+10 -10
View File
@@ -37,12 +37,12 @@ int main(int argc, char **argv) {
namespace loot {
namespace test {
void testLoggingCallback(LogLevel, const char *) {
void testLoggingCallback(LogLevel, std::string_view) {
// Do nothing.
}
struct TestLogger {
void callback(LogLevel, const char *message) {
void callback(LogLevel, std::string_view message) {
loggedMessages += std::string(message);
}
@@ -56,7 +56,7 @@ TEST(SetLoggingCallback, shouldAcceptAFreeFunction) {
CreateGameHandle(GameType::tes4, "dummy");
FAIL();
} catch (...) {
SetLoggingCallback([](LogLevel, const char *) {});
SetLoggingCallback([](LogLevel, std::string_view) {});
}
}
@@ -77,13 +77,13 @@ TEST(SetLoggingCallback, shouldAcceptAMemberFunction) {
"IV: Oblivion\" with game path \"dummy\" and game local path \"\"",
testLogger.loggedMessages);
SetLoggingCallback([](LogLevel, const char *) {});
SetLoggingCallback([](LogLevel, std::string_view) {});
}
}
TEST(SetLoggingCallback, shouldAcceptALambdaFunction) {
std::string loggedMessages;
auto callback = [&](LogLevel, const char *string) {
auto callback = [&](LogLevel, std::string_view string) {
loggedMessages += std::string(string);
};
SetLoggingCallback(callback);
@@ -97,7 +97,7 @@ TEST(SetLoggingCallback, shouldAcceptALambdaFunction) {
"IV: Oblivion\" with game path \"dummy\" and game local path \"\"",
loggedMessages);
SetLoggingCallback([](LogLevel, const char *) {});
SetLoggingCallback([](LogLevel, std::string_view) {});
}
}
@@ -105,7 +105,7 @@ TEST(SetLoggingCallback,
shouldNotBreakLoggingIfPassedLambdaFunctionGoesOutOfScope) {
std::string loggedMessages;
{
SetLoggingCallback([&](LogLevel, const char *string) {
SetLoggingCallback([&](LogLevel, std::string_view string) {
loggedMessages += std::string(string);
});
}
@@ -119,13 +119,13 @@ TEST(SetLoggingCallback,
"IV: Oblivion\" with game path \"dummy\" and game local path \"\"",
loggedMessages);
SetLoggingCallback([](LogLevel, const char *) {});
SetLoggingCallback([](LogLevel, std::string_view) {});
}
}
TEST(SetLogLevel, shouldOnlyRunTheCallbackForMessagesAtOrAboveTheGivenLevel) {
std::vector<std::pair<LogLevel, std::string>> loggedMessages;
auto callback = [&](LogLevel level, const char *string) {
auto callback = [&](LogLevel level, std::string_view string) {
loggedMessages.push_back(std::make_pair(level, std::string(string)));
};
SetLoggingCallback(callback);
@@ -151,7 +151,7 @@ TEST(SetLogLevel, shouldOnlyRunTheCallbackForMessagesAtOrAboveTheGivenLevel) {
"IV: Oblivion\" with game path \"dummy\" and game local path \"\"",
loggedMessages[0].second);
SetLoggingCallback([](LogLevel, const char *) {});
SetLoggingCallback([](LogLevel, std::string_view) {});
}
}
}