2021-09-27 08:36:28 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2021-09-27 08:36:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogLevel is the logLevel parameter.
|
|
|
|
type LogLevel logger.Level
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d LogLevel) MarshalJSON() ([]byte, error) {
|
|
|
|
var out string
|
|
|
|
|
|
|
|
switch d {
|
2021-10-28 15:29:27 +00:00
|
|
|
case LogLevel(logger.Error):
|
|
|
|
out = "error"
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
case LogLevel(logger.Warn):
|
|
|
|
out = "warn"
|
|
|
|
|
|
|
|
case LogLevel(logger.Info):
|
|
|
|
out = "info"
|
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
default:
|
2024-03-04 13:20:34 +00:00
|
|
|
out = "debug"
|
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 *LogLevel) UnmarshalJSON(b []byte) error {
|
|
|
|
var in string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch in {
|
2021-10-28 15:29:27 +00:00
|
|
|
case "error":
|
|
|
|
*d = LogLevel(logger.Error)
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
case "warn":
|
|
|
|
*d = LogLevel(logger.Warn)
|
|
|
|
|
|
|
|
case "info":
|
|
|
|
*d = LogLevel(logger.Info)
|
|
|
|
|
|
|
|
case "debug":
|
|
|
|
*d = LogLevel(logger.Debug)
|
|
|
|
|
|
|
|
default:
|
2023-03-31 14:22:08 +00:00
|
|
|
return fmt.Errorf("invalid log level: '%s'", in)
|
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 *LogLevel) UnmarshalEnv(_ string, v string) error {
|
|
|
|
return d.UnmarshalJSON([]byte(`"` + v + `"`))
|
2021-10-11 09:46:40 +00:00
|
|
|
}
|