1
0

Add optional prefix parameter to LOG functions (#5229)

Co-authored-by: Tiger Wang <ziwei.tiger@outlook.com>
This commit is contained in:
x12xx12x 2021-06-23 00:09:06 +02:00 committed by GitHub
parent 3b35a00397
commit cc9f7c06b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 110 additions and 114 deletions

View File

@ -10931,14 +10931,26 @@ a_Player:OpenWindow(Window);
}, },
SendMessage = SendMessage =
{ {
Params =
{ {
Params =
{ {
Name = "Message", {
Type = "string", Name = "Message",
Type = "string",
},
}, },
Notes = "Sends the specified message to the player.",
},
{
Params =
{
{
Name = "Message",
Type = "cCompositeChat",
},
},
Notes = "Sends the {{cCompositeChat}} to the player, using a severity defined by the CompositeChat's MessageType.",
}, },
Notes = "Sends the specified message to the player.",
}, },
SendMessageRaw = SendMessageRaw =
{ {
@ -14590,8 +14602,13 @@ end
Name = "Message", Name = "Message",
Type = "string", Type = "string",
}, },
{
Name = "SuppressPluginNamePrefix",
Type = "boolean",
IsOptional = true,
},
}, },
Notes = "Logs a text into the server console and logfile using 'normal' severity (gray text)", Notes = "Logs a text into the server console and logfile using 'normal' severity (gray text).",
}, },
{ {
Params = Params =
@ -14600,101 +14617,78 @@ end
Name = "Message", Name = "Message",
Type = "cCompositeChat", Type = "cCompositeChat",
}, },
{
Name = "SuppressPluginNamePrefix",
Type = "boolean",
IsOptional = true,
},
}, },
Notes = "Logs the {{cCompositeChat}}'s human-readable text into the server console. The severity is converted from the CompositeChat's MessageType.", Notes = "Logs the {{cCompositeChat}}'s human-readable text into the server console, using a severity defined by the CompositeChat's MessageType.",
}, },
}, },
LOGERROR = LOGERROR =
{ {
Params =
{ {
Params =
{ {
{ Name = "Message",
Name = "Message", Type = "string",
Type = "string",
},
}, },
Notes = "Logs a text into the server console and logfile using 'error' severity (black text on red background)",
},
{
Params =
{ {
{ Name = "SuppressPluginNamePrefix",
Name = "Message", Type = "boolean",
Type = "cCompositeChat", IsOptional = true,
},
}, },
Notes = "Logs the {{cCompositeChat}}'s human-readable text into the server console and logfile using 'error' severity (black text on red background)",
}, },
Notes = "Logs a text into the server console and logfile using 'error' severity (black text on red background).",
}, },
LOGINFO = LOGINFO =
{ {
Params =
{ {
Params =
{ {
{ Name = "Message",
Name = "Message", Type = "string",
Type = "string",
},
}, },
Notes = "Logs a text into the server console and logfile using 'info' severity (yellow text)",
},
{
Params =
{ {
{ Name = "SuppressPluginNamePrefix",
Name = "Message", Type = "boolean",
Type = "cCompositeChat", IsOptional = true,
},
}, },
Notes = "Logs the {{cCompositeChat}}'s human-readable text into the server console and logfile using 'info' severity (yellow text)",
}, },
Notes = "Logs a text into the server console and logfile using 'info' severity (yellow text).",
}, },
LOGWARN = LOGWARN =
{ {
Params =
{ {
Params =
{ {
{ Name = "Message",
Name = "Message", Type = "string",
Type = "string",
},
}, },
Notes = "Logs a text into the server console and logfile using 'warning' severity (red text); OBSOLETE, use LOGWARNING() instead",
},
{
Params =
{ {
{ Name = "SuppressPluginNamePrefix",
Name = "Message", Type = "boolean",
Type = "cCompositeChat", IsOptional = true,
},
}, },
Notes = "Logs the {{cCompositeChat}}'s human-readable text into the server console and logfile using 'warning' severity (red text); OBSOLETE, use LOGWARNING() instead",
}, },
Notes = "Logs a text into the server console and logfile using 'warning' severity (red text); OBSOLETE, use LOGWARNING() instead.",
}, },
LOGWARNING = LOGWARNING =
{ {
Params =
{ {
Params =
{ {
{ Name = "Message",
Name = "Message", Type = "string",
Type = "string",
},
}, },
Notes = "Logs a text into the server console and logfile using 'warning' severity (red text)",
},
{
Params =
{ {
{ Name = "SuppressPluginNamePrefix",
Name = "Message", Type = "boolean",
Type = "cCompositeChat", IsOptional = true,
},
}, },
Notes = "Logs the {{cCompositeChat}}'s human-readable text into the server console and logfile using 'warning' severity (red text)",
}, },
Notes = "Logs a text into the server console and logfile using 'warning' severity (red text).",
}, },
md5 = md5 =
{ {

View File

@ -36,7 +36,7 @@ local function LoadAPIFiles(a_Folder, a_DstTable)
if (a_DstTable[k]) then if (a_DstTable[k]) then
-- The class is documented in two files, warn and store into a file (so that CIs can mark build as failure): -- The class is documented in two files, warn and store into a file (so that CIs can mark build as failure):
LOGWARNING(string.format( LOGWARNING(string.format(
"APIDump warning: class %s is documented at two places, the documentation in file %s will overwrite the previously loaded one!", "Warning: class %s is documented at two places, the documentation in file %s will overwrite the previously loaded one!",
k, FileName k, FileName
)) ))
local f = io.open("DuplicateDocs.txt", "a") local f = io.open("DuplicateDocs.txt", "a")
@ -2115,7 +2115,7 @@ function Initialize(Plugin)
g_Plugin = Plugin; g_Plugin = Plugin;
g_PluginFolder = Plugin:GetLocalFolder(); g_PluginFolder = Plugin:GetLocalFolder();
LOG("Initialising " .. Plugin:GetName() .. " v." .. Plugin:GetVersion()) LOG("Initialising v." .. Plugin:GetVersion())
-- Bind a console command to dump the API: -- Bind a console command to dump the API:
cPluginManager:BindConsoleCommand("api", HandleCmdApi, "Dumps the Lua API docs into the API/ subfolder") cPluginManager:BindConsoleCommand("api", HandleCmdApi, "Dumps the Lua API docs into the API/ subfolder")

View File

@ -4,7 +4,7 @@ function Initialize(a_Plugin)
-- Check if the infodump file exists. -- Check if the infodump file exists.
if (not cFile:IsFile("Plugins/InfoDump.lua")) then if (not cFile:IsFile("Plugins/InfoDump.lua")) then
LOGWARN("[DumpInfo] InfoDump.lua was not found.") LOGWARN("InfoDump.lua was not found.")
return false return false
end end

View File

@ -1489,6 +1489,22 @@ bool cLuaState::GetStackValue(int a_StackPos, cUUID & a_Value)
bool cLuaState::GetStackValue(int a_StackPos, std::string_view & a_Value)
{
size_t Length = 0;
const char * const Value = lua_tolstring(m_LuaState, a_StackPos, &Length);
if (Value != nullptr)
{
a_Value = { Value, Length };
return true;
}
return false;
}
template <typename T> template <typename T>
bool cLuaState::GetStackValue(int a_StackPos, Vector3<T> & a_ReturnedVal) bool cLuaState::GetStackValue(int a_StackPos, Vector3<T> & a_ReturnedVal)
{ {

View File

@ -664,6 +664,7 @@ public:
bool GetStackValue(int a_StackPos, eWeather & a_Value); bool GetStackValue(int a_StackPos, eWeather & a_Value);
bool GetStackValue(int a_StackPos, float & a_ReturnedVal); bool GetStackValue(int a_StackPos, float & a_ReturnedVal);
bool GetStackValue(int a_StackPos, cUUID & a_Value); bool GetStackValue(int a_StackPos, cUUID & a_Value);
bool GetStackValue(int a_StackPos, std::string_view & a_Value);
// template to catch all of the various c++ integral types without overload conflicts // template to catch all of the various c++ integral types without overload conflicts
template <class T, typename = std::enable_if_t<std::is_integral_v<T>>> template <class T, typename = std::enable_if_t<std::is_integral_v<T>>>

View File

@ -347,22 +347,37 @@ static int tolua_StringSplitAndTrim(lua_State * tolua_S)
/** Retrieves the log message from the first param on the Lua stack. /** Prints the message to the console, optionally formatting it with a plugin name prefix if the second param on the Lua stack is true. */
Can take either a string or a cCompositeChat. static void LogFromLuaStack(lua_State * tolua_S, const std::string_view a_Message, const eLogLevel a_LogLevel)
*/
static void LogFromLuaStack(lua_State * tolua_S, eLogLevel a_LogLevel)
{ {
tolua_Error err; if (lua_isboolean(tolua_S, 2) && (lua_toboolean(tolua_S, 2) == 1))
if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err)) {
Logger::LogSimple(a_Message, a_LogLevel);
}
else
{
Logger::LogSimple(fmt::format("[{}] {}", cManualBindings::GetLuaPlugin(tolua_S)->GetName(), a_Message), a_LogLevel);
}
}
/** Retrieves a string log message from the first param on the Lua stack, optionally prefixes it with plugin name, and prints it to the console. */
static void LogFromLuaStack(lua_State * tolua_S, const eLogLevel a_LogLevel)
{
cLuaState L(tolua_S);
// If there's no param, spit out an error message instead of crashing:
if (!L.CheckParamString(1))
{ {
auto Msg = static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->ExtractText();
Logger::LogSimple(Msg, a_LogLevel);
return; return;
} }
size_t len = 0; std::string_view Message;
const char * str = lua_tolstring(tolua_S, 1, &len); L.GetStackValue(1, Message);
Logger::LogSimple(fmt::format("[{}] {}", cManualBindings::GetLuaPlugin(tolua_S)->GetName(), std::string_view(str, len)), a_LogLevel); LogFromLuaStack(tolua_S, Message, a_LogLevel);
} }
@ -371,26 +386,20 @@ static void LogFromLuaStack(lua_State * tolua_S, eLogLevel a_LogLevel)
static int tolua_LOG(lua_State * tolua_S) static int tolua_LOG(lua_State * tolua_S)
{ {
// If there's no param, spit out an error message instead of crashing: // If the param is a cCompositeChat, read the data from it:
if (lua_isnil(tolua_S, 1))
{
LOGWARNING("Attempting to LOG a nil value!");
cLuaState::LogStackTrace(tolua_S);
return 0;
}
// If the param is a cCompositeChat, read the log level from it:
eLogLevel LogLevel = eLogLevel::Regular;
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( const auto CompositeChat = static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr));
static_cast<cCompositeChat *>(tolua_tousertype(tolua_S, 1, nullptr))->GetMessageType() if (CompositeChat != nullptr) // isusertype returns true for nil values
); {
LogFromLuaStack(tolua_S, CompositeChat->ExtractText(), cCompositeChat::MessageTypeToLogLevel(CompositeChat->GetMessageType()));
return 0;
}
} }
// Log the message: // Log the message:
LogFromLuaStack(tolua_S, LogLevel); LogFromLuaStack(tolua_S, eLogLevel::Regular);
return 0; return 0;
} }
@ -400,14 +409,6 @@ static int tolua_LOG(lua_State * tolua_S)
static int tolua_LOGINFO(lua_State * tolua_S) static int tolua_LOGINFO(lua_State * tolua_S)
{ {
// If there's no param, spit out an error message instead of crashing:
if (lua_isnil(tolua_S, 1))
{
LOGWARNING("Attempting to LOGINFO a nil value!");
cLuaState::LogStackTrace(tolua_S);
return 0;
}
LogFromLuaStack(tolua_S, eLogLevel::Info); LogFromLuaStack(tolua_S, eLogLevel::Info);
return 0; return 0;
} }
@ -418,14 +419,6 @@ static int tolua_LOGINFO(lua_State * tolua_S)
static int tolua_LOGWARN(lua_State * tolua_S) static int tolua_LOGWARN(lua_State * tolua_S)
{ {
// If there's no param, spit out an error message instead of crashing:
if (lua_isnil(tolua_S, 1))
{
LOGWARNING("Attempting to LOGWARN a nil value!");
cLuaState::LogStackTrace(tolua_S);
return 0;
}
LogFromLuaStack(tolua_S, eLogLevel::Warning); LogFromLuaStack(tolua_S, eLogLevel::Warning);
return 0; return 0;
} }
@ -436,14 +429,6 @@ static int tolua_LOGWARN(lua_State * tolua_S)
static int tolua_LOGERROR(lua_State * tolua_S) static int tolua_LOGERROR(lua_State * tolua_S)
{ {
// If there's no param, spit out an error message instead of crashing:
if (lua_isnil(tolua_S, 1))
{
LOGWARNING("Attempting to LOGERROR a nil value!");
cLuaState::LogStackTrace(tolua_S);
return 0;
}
LogFromLuaStack(tolua_S, eLogLevel::Error); LogFromLuaStack(tolua_S, eLogLevel::Error);
return 0; return 0;
} }