OnDebugMessage: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Remove new item highlight.)
 
(4 intermediate revisions by 3 users not shown)
Line 2: Line 2:
{{Server event}}
{{Server event}}
This event is triggered when debug messages (for instance errors or warnings) appear in the server console.
This event is triggered when debug messages (for instance errors or warnings) appear in the server console.
'''Note:''' To prevent infinite loops, debug messages that occur inside the function that handles this event won't trigger this event, so you won't be able to rely on debug info to fix faulty code that is inside this function. Since build [https://buildinfo.mtasa.com/index.php?Revision=14683 r14683] debug messages from [[outputDebugString]] and [[iprint]] will show up.


==Parameters==
==Parameters==
<syntaxhighlight lang="lua">
string message, int level, string file, int line
</syntaxhighlight>
{{New items|5.0154|1.5.4-9.11412|
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string message, int level, string file, int line, int r, int g, int b
string message, int level, string file, int line, int r, int g, int b
</syntaxhighlight>
</syntaxhighlight>
}}


*'''message''': the message which was outputted in the server console, without details like file, line etc.
*'''message''': the message which was outputted in the server console, without details like file, line etc.
Line 24: Line 20:
*'''line''': the line in file '''file''' where the debug message was outputted.
*'''line''': the line in file '''file''' where the debug message was outputted.
**'''Note:''' may return [[nil]] when the source could not be found.
**'''Note:''' may return [[nil]] when the source could not be found.
{{New items|5.0154|1.5.4-9.11412|
*'''r''': an [[int]] representing the amount of red color (0-255).
*'''r''': an [[int]] representing the amount of red color (0-255).
*'''g''': an [[int]] representing the amount of green color (0-255).
*'''g''': an [[int]] representing the amount of green color (0-255).
*'''b''': an [[int]] representing the amount of blue color (0-255).
*'''b''': an [[int]] representing the amount of blue color (0-255).
}}


==Source==
==Source==
The [[event system#Event source|source]] of this event is the [[root element]].
The [[event system#Event source|source]] of this event is the [[root element]].
==Canceling==
If this event is [[Event system #Canceling|canceled]], the debug message won't appear.


==Example==  
==Example==  
This example outputs all debug messages to the chat box with the same colouring.
This example outputs error debug messages to chat.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler("onDebugMessage", getRootElement(),
function onDebugMessage(debugMessage, debugLevel, debugFile, debugLine, debugRed, debugGreen, debugBlue)
local debugError = (debugLevel == 1)
 
if (not debugError) then
return false
end


function(message, level, file, line)
local debugAtFile = (debugFile and debugFile or "NO_FILE")
local debugAtLine = (debugLine and debugLine or "NO_LINE")
local debugChatMessage = "ERROR: "..debugAtFile..":"..debugAtLine..": "..debugMessage


if level == 1 then
outputChatBox(debugChatMessage, root, debugRed, debugGreen, debugBlue)
outputChatBox("ERROR: " .. file .. ":" .. tostring(line) .. ", " .. message, getRootElement(), 255,0,0)
elseif level == 2 then
outputChatBox("WARNING: " .. file .. ":" .. tostring(line) .. ", " .. message, getRootElement(), 255,165,0)
else
outputChatBox("INFO: " .. file .. ":" .. tostring(line) .. ", " .. message, getRootElement(), 0,0,255)
end
end
 
addEventHandler("onDebugMessage", root, onDebugMessage)
 
end)
</syntaxhighlight>
</syntaxhighlight>


{{See also/Server event|Server events}}
{{See also/Server event|Server events}}

Latest revision as of 07:06, 8 June 2024

This event is triggered when debug messages (for instance errors or warnings) appear in the server console.

Note: To prevent infinite loops, debug messages that occur inside the function that handles this event won't trigger this event, so you won't be able to rely on debug info to fix faulty code that is inside this function. Since build r14683 debug messages from outputDebugString and iprint will show up.

Parameters

string message, int level, string file, int line, int r, int g, int b
  • message: the message which was outputted in the server console, without details like file, line etc.
  • level: the type of debug message which was outputted.
    • 0: "Custom" message.
    • 1: Error message.
    • 2: Warning message.
    • 3: Information message.
  • file: the file from which the debug message was outputted.
    • Note: may return nil when the source could not be found.
  • line: the line in file file where the debug message was outputted.
    • Note: may return nil when the source could not be found.
  • r: an int representing the amount of red color (0-255).
  • g: an int representing the amount of green color (0-255).
  • b: an int representing the amount of blue color (0-255).

Source

The source of this event is the root element.

Canceling

If this event is canceled, the debug message won't appear.

Example

This example outputs error debug messages to chat.

function onDebugMessage(debugMessage, debugLevel, debugFile, debugLine, debugRed, debugGreen, debugBlue)
	local debugError = (debugLevel == 1)

	if (not debugError) then
		return false
	end

	local debugAtFile = (debugFile and debugFile or "NO_FILE")
	local debugAtLine = (debugLine and debugLine or "NO_LINE")
	local debugChatMessage = "ERROR: "..debugAtFile..":"..debugAtLine..": "..debugMessage

	outputChatBox(debugChatMessage, root, debugRed, debugGreen, debugBlue)
end
addEventHandler("onDebugMessage", root, onDebugMessage)

See Also

Server events


Event functions