2021-09-27 08:36:28 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-10-17 13:40:17 +00:00
|
|
|
"sort"
|
2021-10-11 09:46:40 +00:00
|
|
|
"strings"
|
2021-09-27 08:36:28 +00:00
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogDestinations is the logDestionations parameter.
|
|
|
|
type LogDestinations map[logger.Destination]struct{}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
for p := range d {
|
|
|
|
var v string
|
|
|
|
|
|
|
|
switch p {
|
|
|
|
case logger.DestinationStdout:
|
|
|
|
v = "stdout"
|
|
|
|
|
|
|
|
case logger.DestinationFile:
|
|
|
|
v = "file"
|
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
case logger.DestinationSyslog:
|
2021-09-27 08:36:28 +00:00
|
|
|
v = "syslog"
|
2023-03-31 14:22:08 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid log destination: %v", p)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out[i] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2021-10-17 13:40:17 +00:00
|
|
|
sort.Strings(out)
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
return json.Marshal(out)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = make(LogDestinations)
|
|
|
|
|
2021-10-11 09:46:40 +00:00
|
|
|
for _, proto := range in {
|
2021-09-27 08:36:28 +00:00
|
|
|
switch proto {
|
|
|
|
case "stdout":
|
|
|
|
(*d)[logger.DestinationStdout] = struct{}{}
|
|
|
|
|
|
|
|
case "file":
|
|
|
|
(*d)[logger.DestinationFile] = struct{}{}
|
|
|
|
|
|
|
|
case "syslog":
|
|
|
|
(*d)[logger.DestinationSyslog] = struct{}{}
|
|
|
|
|
|
|
|
default:
|
2021-09-27 08:43:48 +00:00
|
|
|
return fmt.Errorf("invalid log destination: %s", proto)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-11 09:46:40 +00:00
|
|
|
|
2022-12-15 23:50:47 +00:00
|
|
|
// unmarshalEnv implements envUnmarshaler.
|
2021-10-11 09:46:40 +00:00
|
|
|
func (d *LogDestinations) unmarshalEnv(s string) error {
|
|
|
|
byts, _ := json.Marshal(strings.Split(s, ","))
|
|
|
|
return d.UnmarshalJSON(byts)
|
|
|
|
}
|