1
0

Avoid a copy when logging lua strings

This commit is contained in:
Peter Bell 2020-05-15 12:54:08 +01:00 committed by Tiger Wang
parent 3189a3cbee
commit 6dee68cb63

View File

@ -33,7 +33,6 @@
#include "../HTTP/UrlParser.h" #include "../HTTP/UrlParser.h"
#include "../Item.h" #include "../Item.h"
#include "../LineBlockTracer.h" #include "../LineBlockTracer.h"
#include "../Logger.h"
#include "../Server.h" #include "../Server.h"
#include "../Root.h" #include "../Root.h"
#include "../StringCompression.h" #include "../StringCompression.h"
@ -368,23 +367,19 @@ static int tolua_StringSplitAndTrim(lua_State * tolua_S)
/** Retrieves the log message from the first param on the Lua stack. /** Retrieves the log message from the first param on the Lua stack.
Can take either a string or a cCompositeChat. Can take either a string or a cCompositeChat.
*/ */
static AString GetLogMessage(lua_State * tolua_S) static void LogFromLuaStack(lua_State * tolua_S, eLogLevel a_LogLevel)
{ {
tolua_Error err; tolua_Error err;
if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err)) if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err))
{ {
return static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->ExtractText(); auto Msg = static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->ExtractText();
Logger::LogSimple(Msg, a_LogLevel);
return;
} }
else
{
size_t len = 0; size_t len = 0;
const char * str = lua_tolstring(tolua_S, 1, &len); const char * str = lua_tolstring(tolua_S, 1, &len);
if (str != nullptr) Logger::LogSimple(std::string_view(str, len), a_LogLevel);
{
return AString(str, len);
}
}
return "";
} }
@ -406,11 +401,13 @@ static int tolua_LOG(lua_State * tolua_S)
tolua_Error err; tolua_Error err;
if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err)) if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err))
{ {
LogLevel = cCompositeChat::MessageTypeToLogLevel(static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->GetMessageType()); LogLevel = cCompositeChat::MessageTypeToLogLevel(
static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->GetMessageType()
);
} }
// Log the message: // Log the message:
cLogger::GetInstance().LogSimple(GetLogMessage(tolua_S), LogLevel); LogFromLuaStack(tolua_S, LogLevel);
return 0; return 0;
} }
@ -428,7 +425,7 @@ static int tolua_LOGINFO(lua_State * tolua_S)
return 0; return 0;
} }
cLogger::GetInstance().LogSimple(GetLogMessage(tolua_S), eLogLevel::Info); LogFromLuaStack(tolua_S, eLogLevel::Info);
return 0; return 0;
} }
@ -446,7 +443,7 @@ static int tolua_LOGWARN(lua_State * tolua_S)
return 0; return 0;
} }
cLogger::GetInstance().LogSimple(GetLogMessage(tolua_S), eLogLevel::Warning); LogFromLuaStack(tolua_S, eLogLevel::Warning);
return 0; return 0;
} }
@ -464,7 +461,7 @@ static int tolua_LOGERROR(lua_State * tolua_S)
return 0; return 0;
} }
cLogger::GetInstance().LogSimple(GetLogMessage(tolua_S), eLogLevel::Error); LogFromLuaStack(tolua_S, eLogLevel::Error);
return 0; return 0;
} }