diff --git a/src/log.cpp b/src/log.cpp index 98939c9bf..f7eb691ac 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -55,7 +55,7 @@ public: return m_logger.hasOutput(m_level); } - virtual void log(const std::string &buf) override { + virtual void log(std::string_view buf) override { if (!m_raw) { m_logger.log(m_level, buf); } else { @@ -106,7 +106,7 @@ thread_local LogStream dout_con(trace_target); // Android #ifdef __ANDROID__ -static unsigned int g_level_to_android[] = { +constexpr static unsigned int g_level_to_android[] = { ANDROID_LOG_INFO, // LL_NONE ANDROID_LOG_ERROR, // LL_ERROR ANDROID_LOG_WARN, // LL_WARNING @@ -116,11 +116,12 @@ static unsigned int g_level_to_android[] = { ANDROID_LOG_VERBOSE, // LL_TRACE }; -void AndroidLogOutput::logRaw(LogLevel lev, const std::string &line) +void AndroidLogOutput::logRaw(LogLevel lev, std::string_view line) { static_assert(ARRLEN(g_level_to_android) == LL_MAX, "mismatch between android and internal loglevels"); - __android_log_write(g_level_to_android[lev], PROJECT_NAME_C, line.c_str()); + __android_log_print(g_level_to_android[lev], PROJECT_NAME_C, "%.*s", + line.size(), line.data()); } #endif @@ -131,7 +132,7 @@ void AndroidLogOutput::logRaw(LogLevel lev, const std::string &line) //// Logger //// -LogLevel Logger::stringToLevel(const std::string &name) +LogLevel Logger::stringToLevel(std::string_view name) { if (name == "none") return LL_NONE; @@ -202,7 +203,7 @@ void Logger::setLevelSilenced(LogLevel lev, bool silenced) m_silenced_levels[lev] = silenced; } -void Logger::registerThread(const std::string &name) +void Logger::registerThread(std::string_view name) { std::thread::id id = std::this_thread::get_id(); MutexAutoLock lock(m_mutex); @@ -252,7 +253,7 @@ const std::string &Logger::getThreadName() return fallback_name; } -void Logger::log(LogLevel lev, const std::string &text) +void Logger::log(LogLevel lev, std::string_view text) { if (isLevelSilenced(lev)) return; @@ -268,7 +269,7 @@ void Logger::log(LogLevel lev, const std::string &text) logToOutputs(lev, line, timestamp, thread_name, text); } -void Logger::logRaw(LogLevel lev, const std::string &text) +void Logger::logRaw(LogLevel lev, std::string_view text) { if (isLevelSilenced(lev)) return; @@ -276,7 +277,7 @@ void Logger::logRaw(LogLevel lev, const std::string &text) logToOutputsRaw(lev, text); } -void Logger::logToOutputsRaw(LogLevel lev, const std::string &line) +void Logger::logToOutputsRaw(LogLevel lev, std::string_view line) { MutexAutoLock lock(m_mutex); for (size_t i = 0; i != m_outputs[lev].size(); i++) @@ -285,7 +286,7 @@ void Logger::logToOutputsRaw(LogLevel lev, const std::string &line) void Logger::logToOutputs(LogLevel lev, const std::string &combined, const std::string &time, const std::string &thread_name, - const std::string &payload_text) + std::string_view payload_text) { MutexAutoLock lock(m_mutex); for (size_t i = 0; i != m_outputs[lev].size(); i++) @@ -334,7 +335,7 @@ StreamLogOutput::StreamLogOutput(std::ostream &stream) : #endif } -void StreamLogOutput::logRaw(LogLevel lev, const std::string &line) +void StreamLogOutput::logRaw(LogLevel lev, std::string_view line) { bool colored_message = (Logger::color_mode == LOG_COLOR_ALWAYS) || (Logger::color_mode == LOG_COLOR_AUTO && is_tty); @@ -385,7 +386,7 @@ void LogOutputBuffer::updateLogLevel() m_logger.addOutputMaxLevel(this, log_level); } -void LogOutputBuffer::logRaw(LogLevel lev, const std::string &line) +void LogOutputBuffer::logRaw(LogLevel lev, std::string_view line) { std::string color; diff --git a/src/log.h b/src/log.h index 9ac4e5767..721ce58ed 100644 --- a/src/log.h +++ b/src/log.h @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include -#include +#include #include #include #include @@ -62,14 +62,14 @@ public: LogLevelMask removeOutput(ILogOutput *out); void setLevelSilenced(LogLevel lev, bool silenced); - void registerThread(const std::string &name); + void registerThread(std::string_view name); void deregisterThread(); - void log(LogLevel lev, const std::string &text); + void log(LogLevel lev, std::string_view text); // Logs without a prefix - void logRaw(LogLevel lev, const std::string &text); + void logRaw(LogLevel lev, std::string_view text); - static LogLevel stringToLevel(const std::string &name); + static LogLevel stringToLevel(std::string_view name); static const char *getLevelLabel(LogLevel lev); bool hasOutput(LogLevel level) { @@ -83,10 +83,10 @@ public: static LogColor color_mode; private: - void logToOutputsRaw(LogLevel, const std::string &line); + void logToOutputsRaw(LogLevel, std::string_view line); void logToOutputs(LogLevel, const std::string &combined, const std::string &time, const std::string &thread_name, - const std::string &payload_text); + std::string_view payload_text); const std::string &getThreadName(); @@ -99,17 +99,17 @@ private: class ILogOutput { public: - virtual void logRaw(LogLevel, const std::string &line) = 0; + virtual void logRaw(LogLevel, std::string_view line) = 0; virtual void log(LogLevel, const std::string &combined, const std::string &time, const std::string &thread_name, - const std::string &payload_text) = 0; + std::string_view payload_text) = 0; }; class ICombinedLogOutput : public ILogOutput { public: void log(LogLevel lev, const std::string &combined, const std::string &time, const std::string &thread_name, - const std::string &payload_text) + std::string_view payload_text) { logRaw(lev, combined); } @@ -119,7 +119,7 @@ class StreamLogOutput : public ICombinedLogOutput { public: StreamLogOutput(std::ostream &stream); - void logRaw(LogLevel lev, const std::string &line); + void logRaw(LogLevel lev, std::string_view line); private: std::ostream &m_stream; @@ -130,7 +130,7 @@ class FileLogOutput : public ICombinedLogOutput { public: void setFile(const std::string &filename, s64 file_size_max); - void logRaw(LogLevel lev, const std::string &line) + void logRaw(LogLevel lev, std::string_view line) { m_stream << line << std::endl; } @@ -154,7 +154,7 @@ public: void updateLogLevel(); - void logRaw(LogLevel lev, const std::string &line); + void logRaw(LogLevel lev, std::string_view line); void clear() { @@ -190,7 +190,7 @@ private: #ifdef __ANDROID__ class AndroidLogOutput : public ICombinedLogOutput { public: - void logRaw(LogLevel lev, const std::string &line); + void logRaw(LogLevel lev, std::string_view line); }; #endif @@ -206,7 +206,7 @@ class LogTarget { public: // Must be thread-safe. These can be called from any thread. virtual bool hasOutput() = 0; - virtual void log(const std::string &buf) = 0; + virtual void log(std::string_view buf) = 0; }; @@ -304,7 +304,7 @@ public: return m_target.hasOutput(); } - void internalFlush(const std::string &buf) { + void internalFlush(std::string_view buf) { m_target.log(buf); } diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp index ae83b8df0..e6bfafdcd 100644 --- a/src/script/common/c_internal.cpp +++ b/src/script/common/c_internal.cpp @@ -122,27 +122,30 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f throw LuaError(err_msg); } -static void script_log_add_source(lua_State *L, std::string &message, int stack_depth) +[[nodiscard]] static std::string script_log_add_source(lua_State *L, + std::string_view message, int stack_depth) { + std::string ret(message); if (stack_depth <= 0) - return; + return ret; lua_Debug ar; if (lua_getstack(L, stack_depth, &ar)) { FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed"); - message.append(" (at " + std::string(ar.short_src) + ":" + ret.append(" (at ").append(ar.short_src).append(":" + std::to_string(ar.currentline) + ")"); } else { - message.append(" (at ?:?)"); + ret.append(" (at ?:?)"); } + return ret; } -bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to, +bool script_log_unique(lua_State *L, std::string_view message_in, std::ostream &log_to, int stack_depth) { thread_local std::vector logged_messages; - script_log_add_source(L, message, stack_depth); + auto message = script_log_add_source(L, message_in, stack_depth); u64 hash = murmur_hash_64_ua(message.data(), message.length(), 0xBADBABE); if (std::find(logged_messages.begin(), logged_messages.end(), hash) @@ -174,7 +177,7 @@ DeprecatedHandlingMode get_deprecated_handling_mode() return ret; } -void log_deprecated(lua_State *L, std::string message, int stack_depth, bool once) +void log_deprecated(lua_State *L, std::string_view message, int stack_depth, bool once) { DeprecatedHandlingMode mode = get_deprecated_handling_mode(); if (mode == DeprecatedHandlingMode::Ignore) @@ -184,12 +187,12 @@ void log_deprecated(lua_State *L, std::string message, int stack_depth, bool onc if (once) { log = script_log_unique(L, message, warningstream, stack_depth); } else { - script_log_add_source(L, message, stack_depth); - warningstream << message << std::endl; + auto message2 = script_log_add_source(L, message, stack_depth); + warningstream << message2 << std::endl; } if (mode == DeprecatedHandlingMode::Error) - throw LuaError(message); + throw LuaError(std::string(message)); else if (log) infostream << script_get_backtrace(L) << std::endl; } diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h index a67b0dad9..a9f9fe226 100644 --- a/src/script/common/c_internal.h +++ b/src/script/common/c_internal.h @@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once +#include + extern "C" { #include #include @@ -127,7 +129,7 @@ int script_error_handler(lua_State *L); // Takes an error from lua_pcall and throws it as a LuaError void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn); -bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to, +bool script_log_unique(lua_State *L, std::string_view message, std::ostream &log_to, int stack_depth = 1); enum DeprecatedHandlingMode { @@ -152,7 +154,8 @@ DeprecatedHandlingMode get_deprecated_handling_mode(); * (ie: not builtin or core). -1 to disabled. * @param once Log the deprecation warning only once per callsite. */ -void log_deprecated(lua_State *L, std::string message, int stack_depth = 1, bool once = false); +void log_deprecated(lua_State *L, std::string_view message, + int stack_depth = 1, bool once = false); // Safely call string.dump on a function value // (does not pop, leaves one value on stack) diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index fac3e54d1..79eb38629 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -61,13 +61,13 @@ with this program; if not, write to the Free Software Foundation, Inc., int ModApiUtil::l_log(lua_State *L) { NO_MAP_LOCK_REQUIRED; - std::string text; + std::string_view text; LogLevel level = LL_NONE; - if (lua_isnone(L, 2)) { - text = luaL_checkstring(L, 1); + if (lua_isnoneornil(L, 2)) { + text = readParam(L, 1); } else { - std::string name = luaL_checkstring(L, 1); - text = luaL_checkstring(L, 2); + auto name = readParam(L, 1); + text = readParam(L, 2); if (name == "deprecated") { log_deprecated(L, text, 2); return 0; @@ -75,7 +75,7 @@ int ModApiUtil::l_log(lua_State *L) level = Logger::stringToLevel(name); if (level == LL_MAX) { warningstream << "Tried to log at unknown level '" << name - << "'. Defaulting to \"none\"." << std::endl; + << "'. Defaulting to \"none\"." << std::endl; level = LL_NONE; } } diff --git a/src/terminal_chat_console.h b/src/terminal_chat_console.h index 825c76ef4..1bd226609 100644 --- a/src/terminal_chat_console.h +++ b/src/terminal_chat_console.h @@ -32,14 +32,14 @@ struct ChatInterface; class TermLogOutput : public ILogOutput { public: - void logRaw(LogLevel lev, const std::string &line) + void logRaw(LogLevel lev, std::string_view line) { - queue.push_back(std::make_pair(lev, line)); + queue.push_back(std::make_pair(lev, std::string(line))); } virtual void log(LogLevel lev, const std::string &combined, const std::string &time, const std::string &thread_name, - const std::string &payload_text) + std::string_view payload_text) { std::ostringstream os(std::ios_base::binary); os << time << ": [" << thread_name << "] " << payload_text; diff --git a/src/util/stream.h b/src/util/stream.h index 2e61b46d2..620ad74ba 100644 --- a/src/util/stream.h +++ b/src/util/stream.h @@ -20,10 +20,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once #include -#include +#include #include -template > +template > class StringStreamBuffer : public std::streambuf { public: StringStreamBuffer(Emitter emitter) : m_emitter(emitter) { @@ -38,19 +38,19 @@ public: void push_back(char c) { if (c == '\n' || c == '\r') { if (buffer_index) - m_emitter(std::string(buffer, buffer_index)); + m_emitter(std::string_view(buffer, buffer_index)); buffer_index = 0; } else { buffer[buffer_index++] = c; if (buffer_index >= BufferLength) { - m_emitter(std::string(buffer, buffer_index)); + m_emitter(std::string_view(buffer, buffer_index)); buffer_index = 0; } } } std::streamsize xsputn(const char *s, std::streamsize n) { - for (int i = 0; i < n; ++i) + for (std::streamsize i = 0; i < n; ++i) push_back(s[i]); return n; }