Commit d7df5e1c authored by Max Kellermann's avatar Max Kellermann

LogBackend: pass std::string_view to Log()

parent e4e4576a
...@@ -24,11 +24,12 @@ ...@@ -24,11 +24,12 @@
#include "util/Compiler.h" #include "util/Compiler.h"
#include <exception> #include <exception>
#include <string_view>
class Domain; class Domain;
void void
Log(LogLevel level, const Domain &domain, const char *msg) noexcept; Log(LogLevel level, const Domain &domain, std::string_view msg) noexcept;
gcc_printf(3,4) gcc_printf(3,4)
void void
......
...@@ -103,10 +103,9 @@ log_date() noexcept ...@@ -103,10 +103,9 @@ log_date() noexcept
* characters. * characters.
*/ */
static int static int
chomp_length(const char *p) noexcept chomp_length(std::string_view p) noexcept
{ {
size_t length = strlen(p); return StripRight(p.data(), p.size());
return StripRight(p, length);
} }
#ifdef HAVE_SYSLOG #ifdef HAVE_SYSLOG
...@@ -137,11 +136,11 @@ ToSysLogLevel(LogLevel log_level) noexcept ...@@ -137,11 +136,11 @@ ToSysLogLevel(LogLevel log_level) noexcept
} }
static void static void
SysLog(const Domain &domain, LogLevel log_level, const char *message) noexcept SysLog(const Domain &domain, LogLevel log_level, std::string_view message) noexcept
{ {
syslog(ToSysLogLevel(log_level), "%s: %.*s", syslog(ToSysLogLevel(log_level), "%s: %.*s",
domain.GetName(), domain.GetName(),
chomp_length(message), message); chomp_length(message), message.data());
} }
void void
...@@ -161,12 +160,12 @@ LogFinishSysLog() noexcept ...@@ -161,12 +160,12 @@ LogFinishSysLog() noexcept
#endif #endif
static void static void
FileLog(const Domain &domain, const char *message) noexcept FileLog(const Domain &domain, std::string_view message) noexcept
{ {
fprintf(stderr, "%s%s: %.*s\n", fprintf(stderr, "%s%s: %.*s\n",
enable_timestamp ? log_date() : "", enable_timestamp ? log_date() : "",
domain.GetName(), domain.GetName(),
chomp_length(message), message); chomp_length(message), message.data());
#ifdef _WIN32 #ifdef _WIN32
/* force-flush the log file, because setvbuf() does not seem /* force-flush the log file, because setvbuf() does not seem
...@@ -178,15 +177,16 @@ FileLog(const Domain &domain, const char *message) noexcept ...@@ -178,15 +177,16 @@ FileLog(const Domain &domain, const char *message) noexcept
#endif /* !ANDROID */ #endif /* !ANDROID */
void void
Log(LogLevel level, const Domain &domain, const char *msg) noexcept Log(LogLevel level, const Domain &domain, std::string_view msg) noexcept
{ {
#ifdef ANDROID #ifdef ANDROID
__android_log_print(ToAndroidLogLevel(level), "MPD", __android_log_print(ToAndroidLogLevel(level), "MPD",
"%s: %s", domain.GetName(), msg); "%s: %.*s", domain.GetName(),
(int)msg.size(), msg.data());
if (logListener != nullptr) { if (logListener != nullptr) {
char buffer[1024]; char buffer[1024];
snprintf(buffer, sizeof(buffer), "%s: %s", snprintf(buffer, sizeof(buffer), "%s: %.*s",
domain.GetName(), msg); domain.GetName(), (int)msg.size(), msg.data());
logListener->OnLog(Java::GetEnv(), ToAndroidLogLevel(level), logListener->OnLog(Java::GetEnv(), ToAndroidLogLevel(level),
buffer); buffer);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment