2021-09-27 08:36:28 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-10-11 09:46:40 +00:00
|
|
|
"strings"
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2021-09-27 08:36:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogDestinations is the logDestionations parameter.
|
2023-05-04 18:16:41 +00:00
|
|
|
type LogDestinations []logger.Destination
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d LogDestinations) MarshalJSON() ([]byte, error) {
|
|
|
|
out := make([]string, len(d))
|
|
|
|
i := 0
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
for _, p := range d {
|
2021-09-27 08:36:28 +00:00
|
|
|
var v string
|
|
|
|
|
|
|
|
switch p {
|
|
|
|
case logger.DestinationStdout:
|
|
|
|
v = "stdout"
|
|
|
|
|
|
|
|
case logger.DestinationFile:
|
|
|
|
v = "file"
|
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
default:
|
2024-03-04 13:20:34 +00:00
|
|
|
v = "syslog"
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out[i] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(out)
|
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
func (d *LogDestinations) contains(v logger.Destination) bool {
|
|
|
|
for _, item := range *d {
|
|
|
|
if item == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d *LogDestinations) UnmarshalJSON(b []byte) error {
|
2021-10-11 09:46:40 +00:00
|
|
|
var in []string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
2021-09-27 08:36:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
*d = nil
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
for _, dest := range in {
|
|
|
|
var v logger.Destination
|
|
|
|
switch dest {
|
2021-09-27 08:36:28 +00:00
|
|
|
case "stdout":
|
2023-05-04 18:16:41 +00:00
|
|
|
v = logger.DestinationStdout
|
2021-09-27 08:36:28 +00:00
|
|
|
|
|
|
|
case "file":
|
2023-05-04 18:16:41 +00:00
|
|
|
v = logger.DestinationFile
|
2021-09-27 08:36:28 +00:00
|
|
|
|
|
|
|
case "syslog":
|
2023-05-04 18:16:41 +00:00
|
|
|
v = logger.DestinationSyslog
|
2021-09-27 08:36:28 +00:00
|
|
|
|
|
|
|
default:
|
2023-05-04 18:16:41 +00:00
|
|
|
return fmt.Errorf("invalid log destination: %s", dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.contains(v) {
|
|
|
|
return fmt.Errorf("log destination set twice")
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
2023-05-04 18:16:41 +00:00
|
|
|
|
|
|
|
*d = append(*d, v)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-11 09:46:40 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
|
|
func (d *LogDestinations) UnmarshalEnv(_ string, v string) error {
|
|
|
|
byts, _ := json.Marshal(strings.Split(v, ","))
|
2021-10-11 09:46:40 +00:00
|
|
|
return d.UnmarshalJSON(byts)
|
|
|
|
}
|