Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
- Added `defaultImageSize`
- Added `defaultAllowedMentions`
- Added `logColors`
- Added `prettyNewlines`
- Added `status`
- Added `activity`
- Renamed `shardCount` to `totalShardCount`
Expand Down Expand Up @@ -745,12 +746,14 @@ Note: This is effectively a new class compared to the old one. It provides a sim

- Changed label colors from bold standard to non-bold bright
- Added "critical" log-level with highest priority
- Added `filePath` and `useColors` parameters to initializer
- Added `filePath`, `useColors` and `prettyNewlines` parameters to initializer
- Added `setLevel` method
- Added `setDateTime` method
- Added `setFile` method
- Added `enableColors` method
- Added `disableColors` method
- Added `enablePrettyNewlines` method
- Added `disablePrettyNewlines` method

#### Mutex

Expand Down
3 changes: 2 additions & 1 deletion libs/client/Client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ local defaultOptions = {
dateFormat = {'%F %T', function(o) return checkType('string', o) end},
logFile = {'discordia.log', function(o) return checkType('string', o) end},
logColors = {true, function(o) return checkType('boolean', o) end},
prettyNewlines = {true, function(o) return checkType('boolean', o) end},
status = {nil, function(o) return checkEnum(enums.status, o) end},
activity = {nil, checkActivity},
}
Expand All @@ -115,7 +116,7 @@ function Client:__init(options)
self._defaultImageSize = options.defaultImageSize
self._defaultAllowedMentions = options.defaultAllowedMentions
self._defaultBitrate = options.defaultBitrate
self._logger = Logger(options.logLevel, options.dateFormat, options.logFile, options.logColors)
self._logger = Logger(options.logLevel, options.dateFormat, options.logFile, options.logColors, options.prettyNewlines)
self._api = API(self)
self._cdn = CDN(self)
self._state = State(self)
Expand Down
21 changes: 19 additions & 2 deletions libs/utils/Logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local enums = require('../enums')
local typing = require('../typing')

local date = os.date
local format = string.format
local format, gsub, rep = string.format, string.gsub, string.rep
local stdout = pp.stdout
local openSync, writeSync, closeSync = fs.openSync, fs.writeSync, fs.closeSync
local checkEnum, checkType = typing.checkEnum, typing.checkType
Expand Down Expand Up @@ -35,11 +35,12 @@ end

local Logger = class('Logger')

function Logger:__init(level, dateFormat, filePath, useColors)
function Logger:__init(level, dateFormat, filePath, useColors, prettyNewlines)
self._level = checkEnum(enums.logLevel, level)
self._dateFormat = dateFormat and checkType('string', dateFormat)
self._file = filePath and openSync(filePath, 'a')
self._useColors = not not useColors
self._prettyNewlines = prettyNewlines
self._line = {nil, ' | ', nil, ' | ', nil, '\n'}
end

Expand Down Expand Up @@ -68,6 +69,14 @@ function Logger:disableColors()
self._useColors = false
end

function Logger:enablePrettyNewlines()
self._prettyNewlines = true
end

function Logger:disablePrettyNewlines()
self._prettyNewlines = false
end

function Logger:log(level, msg, ...)

level = checkEnum(enums.logLevel, level)
Expand All @@ -81,6 +90,14 @@ function Logger:log(level, msg, ...)
line[3] = label[1]
line[5] = format(msg, ...)

if self._prettyNewlines then
line[5] = gsub(
line[5],
'\r?\n',
'%0' .. rep(' ', #line[1] + 16 - 2) .. '| ' -- Timestamp + label (10) + separators (6)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show where you get 16 from? We probably shouldn't hardcode this if it can be dynamically generated (maybe as a module local value).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The label (line[3]) is always length 10. The two separators (|) (line[2] and line[4]) are both 3, so 6 total.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't hardcode this if it can be dynamically generated (maybe as a module local value).

Can you make this happen?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?
75018a4

)
end

if self._file then
writeSync(self._file, -1, line)
end
Expand Down