/
Nebelung
/
QuikAssistantGiga
Обзор
Документация
Войти
/
Nebelung
/
QuikAssistantGiga
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
log.lua
108 строк
2 KB
NebelungCat
first_commit
09 дек 2025, 22:27
09 дек 2025, 22:27
04bf7ed
Код
Авторство
О чём код?
-- -- log.lua -- -- Copyright (c) 2016 rxi -- -- This library is free software; you can redistribute it and/or modify it -- under the terms of the MIT license. See LICENSE for details. -- local log = {_version = "0.1.0"} log.usecolor = true log.outfile = nil log.level = "trace" local modes = { {name = "trace", color = "\27[34m"}, {name = "debug", color = "\27[36m"}, {name = "info", color = "\27[32m"}, {name = "warn", color = "\27[33m"}, {name = "error", color = "\27[31m"}, {name = "fatal", color = "\27[35m"} } local userId = getInfoParam("USERID") local problem = "" if (userId == nil or userId == "") then problem = "ID пользователя не получено" end if (userId == "171783") then Broker = "Finam" elseif (userId == "49653") then Broker = "VTB" elseif (userId == "34146") then Broker = "PSB" elseif (userId == "48640") then Broker = "RSHB" else Broker = "" end local datetime = os.date("*t", os.time()) local fileLog = string.format("/Log/%s/%04d-%02d-%02d.log", Broker, datetime.year, datetime.month, datetime.day) log.outfile = fileLog local levels = {} for i, v in ipairs(modes) do levels[v.name] = i end local round = function(x, increment) increment = increment or 1 x = x / increment return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment end local _tostring = tostring local tostring = function(...) local t = {} for i = 1, select("#", ...) do local x = select(i, ...) if type(x) == "number" then x = round(x, .01) end t[#t + 1] = _tostring(x) end return table.concat(t, " ") end for i, x in ipairs(modes) do local nameupper = x.name:upper() log[x.name] = function(...) -- Return early if we're below the log level if i < levels[log.level] then return end local msg = tostring(...) local info = debug.getinfo(2, "Sl") local lineinfo = info.short_src .. ":" .. info.currentline -- Output to console print( string.format( "%s[%-6s%s]%s %s: %s", log.usecolor and x.color or "", nameupper, os.date("%H:%M:%S"), log.usecolor and "\27[0m" or "", lineinfo, msg ) ) -- Output to log file if log.outfile then local fp = io.open(getScriptPath() .. log.outfile, "a") local str = string.format("%-6s\t\t%s\t\t%s\t\t%s:\t\t%s\n", nameupper, os.date("%x %X", os.time()), Broker, lineinfo, msg) fp:write(str) fp:close() end end end return log