Skip to content

Commit

Permalink
Feat: Support strings for loglevel and severities
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Sep 9, 2024
1 parent 18613e7 commit 2678131
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
56 changes: 42 additions & 14 deletions contrib/lualibs/mympd/50-util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,65 @@
--- myMPD utility functions
---

-- Jsonrpc severities
local jsonrpc_severities = {
SEVERITY_INFO = 0,
SEVERITY_WARNING = 1,
SEVERITY_ERR = 2
}

--- Sends a notification to the client that started this script.
-- @param severity Severity
-- 0 = Info
-- 1 = Warning
-- 2 = Error
-- 0 = SEVERITY_INFO = Info
-- 1 = SEVERITY_WARNING = Warning
-- 2 = SEVERITY_ERR = Error
-- @param message Jsonrpc message to send
function mympd.notify_client(severity, message)
if type(severity) == "string" then
severity = jsonrpc_severities[severity]
end
mympd_util_notify(mympd_env.partition, mympd_env.requestid, severity, message)
end

--- Sends a notification to all clients in the current partition.
-- @param severity Severity
-- 0 = Info
-- 1 = Warning
-- 2 = Error
-- 0 = SEVERITY_INFO = Info
-- 1 = SEVERITY_WARNING = Warning
-- 2 = SEVERITY_ERR = Error
-- @param message Jsonrpc message to send
function mympd.notify_partition(severity, message)
if type(severity) == "string" then
severity = jsonrpc_severities[severity]
end
mympd_util_notify(mympd_env.partition, 0, severity, message)
end

-- Posix loglevel names
local loglevel_names = {
LOG_EMERG = 0,
LOG_ALERT = 1,
LOG_CRIT = 2,
LOG_ERR = 3,
LOG_WARNING = 4,
LOG_NOTICE = 5,
LOG_INFO = 6,
LOG_DEBUG = 7
}

--- Logs to the myMPD log.
-- @param loglevel Syslog loglevel
-- 0 = Emergency
-- 1 = Alert
-- 2 = Critical
-- 3 = Error
-- 4 = Warning
-- 5 = Notice
-- 6 = Info
-- 7 = Debug
-- 0 = LOG_EMERG = Emergency
-- 1 = LOG_ALERT = Alert
-- 2 = LOG_CRIT = Critical
-- 3 = LOG_ERR = Error
-- 4 = LOG_WARNING = Warning
-- 5 = LOG_NOTICE = Notice
-- 6 = LOG_INFO = Info
-- 7 = LOG_DEBUG = Debug
function mympd.log(loglevel, message)
if type(loglevel) == "string" then
loglevel = loglevel_names[loglevel]
end
mympd_util_log(mympd_env.partition, mympd_env.scriptname, loglevel, message)
end

Expand Down
37 changes: 24 additions & 13 deletions docs/scripting/functions/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,34 @@ local decoded = mympd.urldecode(string, form_url_decode)

## Logging

Logs messages to the myMPD log.
Logs messages to the myMPD log. You can use the number or the loglevel name.

```lua
mympd.log(loglevel, message)

-- This is the same
mympd.log(5, message)
mympd.log("LOG_NOTICE", message)
```

**Parameters:**

| PARAMETER | TYPE | DESCRIPTION |
| --------- | ---- | ----------- |
| message | string | Message to log |
| loglevel | number | Syslog log level |
| loglevel | number or string | Syslog log level |
{: .table .table-sm }

| LOGLEVEL | DESCRIPTION |
| -------- | ----------- |
| 0 | Emergency |
| 1 | Alert |
| 2 | Critical |
| 3 | Error |
| 4 | Warning |
| 5 | Notice |
| 6 | Info |
| 7 | Debug |
| LOGLEVEL | NUMBER | DESCRIPTION |
| -------- | ------ | ----------- |
| LOG_EMERG | 0 | Emergency |
| LOG_ALERT | 1 | Alert |
| LOG_CRIT | 2 | Critical |
| LOG_ERR | 3 | Error |
| LOG_WARNING | 4 | Warning |
| LOG_NOTICE | 5 | Notice |
| LOG_INFO | 6 | Informational |
| LOG_DEBUG | 7 | Debug |
{: .table .table-sm }

## Notifications
Expand All @@ -91,6 +95,13 @@ mympd.notify_partition(severity, message)

| PARAMETER | TYPE | DESCRIPTION |
| --------- | ---- | ----------- |
| severity | number | 0 = Info, 1 = Warn, 2 = Error |
| severity | number or string | Severity |
| message | string | Message to send. |
{: .table .table-sm }

| SEVERITY | NUMBER | DESCRIPTION |
| -------- | ------ | ----------- |
| SEVERITY_INFO | 0 | Informational |
| SEVERITY_WARNING | 1 | Warning |
| SEVERITY_ERR | 2 | Error |
{: .table .table-sm }

0 comments on commit 2678131

Please sign in to comment.