Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions gamemode/core/libs/sh_log.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-- @module nut.log
-- @moduleCommentStart
-- Library functions for nutscript logs
-- @moduleCommentEnd

FLAG_NORMAL = 0
FLAG_SUCCESS = 1
FLAG_WARNING = 2
Expand All @@ -21,19 +26,61 @@ if (SERVER) then
include("sv_database.lua")
end

-- @type function nut.log.loadTables()
-- @typeCommentStart
-- Used to load tables into the database
-- @typeCommentEnd
-- @realm server
-- @internal
function nut.log.loadTables()
file.CreateDir("nutscript/logs")
end

-- @type function nut.log.resetTables()
-- @typeCommentStart
-- Used to reset tables into database
-- @typeCommentEnd
-- @realm server
-- @internal
function nut.log.resetTables()
end

-- @type table nut.log.types()
-- @typeCommentStart
-- Stores log types and their formatting functions
-- @typeCommentEnd
-- @realm server
nut.log.types = nut.log.types or {}

-- @type function nut.log.addType(logType, func)
-- @typeCommentStart
-- Used to reset tables into database
-- @typeCommentEnd
-- @realm server
-- @string logType
-- @function (client, ...) log format callback
-- @usageStart
-- nut.log.addType("playerConnected", function(client, ...)
-- local data = {...}
-- local steamID = data[2]
--
-- return string.format("%s[%s] has connected to the server.", client:Name(), steamID or client:SteamID())
-- end)
-- @usageEnd
function nut.log.addType(logType, func)
nut.log.types[logType] = func
end

-- @type function nut.log.getString(client, logType, ...)
-- @typeCommentStart
-- Formats a string that is in log.type
-- @typeCommentEnd
-- @player client Default argument for format string
-- @string logType
-- @vararg ... Other arguments on log format
-- @realm server
-- @treturn string Formatted string
-- @internal
function nut.log.getString(client, logType, ...)
local text = nut.log.types[logType]
if (isfunction(text)) then
Expand All @@ -44,6 +91,14 @@ if (SERVER) then
end
end

-- @type function nut.log.addRaw(logString, shouldNotify, flag)
-- @typeCommentStart
-- Adds a raw that does not require formatting
-- @typeCommentEnd
-- @string logString Log string data
-- @bool sholdNotify Display log notification in the administration console
-- @int flag Log color flag
-- @realm server
function nut.log.addRaw(logString, shouldNotify, flag)
if (shouldNotify) then
nut.log.send(nut.util.getAdmins(), logString, flag)
Expand All @@ -55,6 +110,19 @@ if (SERVER) then
end
end

-- @type function nut.log.add(client, logType, ...)
-- @typeCommentStart
-- Displays a line of the log according to the match described in the log type
-- @typeCommentEnd
-- @player client player name on displayed log
-- @string logType type of log
-- @vararg ... other arguments for log
-- @realm server
-- @usageStart
-- function GM:PlayerAuthed(client, steamID, uniqueID)
-- nut.log.add(client, "playerConnected", client, steamID)
-- end
-- @usageEnd
function nut.log.add(client, logType, ...)
local logString = nut.log.getString(client, logType, ...)
if (not isstring(logString)) then return end
Expand All @@ -71,6 +139,15 @@ if (SERVER) then
netstream.Hook(client, "nutLogView", logData)
end

-- @type function nut.log.add(client, logString, flag)
-- @typeCommentStart
-- Display log raw on client console
-- @typeCommentEnd
-- @player client player name on displayed log
-- @string logString log string
-- @int flag Color flag on log string
-- @realm server
-- @internal
function nut.log.send(client, logString, flag)
netstream.Start(client, "nutLogStream", logString, flag)
end
Expand Down