2023-09-16 15:25:25 +00:00
|
|
|
//
|
|
|
|
// Copyright ⓒ 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`GoSuki` contributors]
|
|
|
|
// (https://github.com/blob42/gosuki/graphs/contributors).
|
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This file is part of GoSuki.
|
|
|
|
//
|
|
|
|
// GoSuki is free software: you can redistribute it and/or modify it under the terms of
|
|
|
|
// the GNU Affero General Public License as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GoSuki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License along with
|
|
|
|
// gosuki. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-11-24 13:50:15 +00:00
|
|
|
// It is possible to enable debugging for execution time that happens before
|
2023-02-18 23:13:23 +00:00
|
|
|
// the -debug cli arg is parsed. This is possible using the GOSUKI_DEBUG=X env
|
2022-11-24 13:50:15 +00:00
|
|
|
// variable where X is an integer for the debug level
|
2018-11-10 11:47:30 +00:00
|
|
|
package logging
|
2017-11-09 20:27:03 +00:00
|
|
|
|
2018-10-26 16:25:07 +00:00
|
|
|
import (
|
|
|
|
"os"
|
2022-11-21 23:05:24 +00:00
|
|
|
|
2022-11-17 22:03:18 +00:00
|
|
|
"strconv"
|
2018-11-10 11:47:30 +00:00
|
|
|
|
|
|
|
glogging "github.com/op/go-logging"
|
2018-10-26 16:25:07 +00:00
|
|
|
)
|
2017-11-09 20:27:03 +00:00
|
|
|
|
2022-11-17 22:03:18 +00:00
|
|
|
var (
|
|
|
|
log = glogging.MustGetLogger("MODE")
|
2018-11-10 11:47:30 +00:00
|
|
|
|
2022-11-17 22:03:18 +00:00
|
|
|
//RELEASE: Change to Release for release mode
|
|
|
|
LoggingMode = Debug
|
2017-11-09 20:27:03 +00:00
|
|
|
|
|
|
|
)
|
|
|
|
|
2023-02-18 23:13:23 +00:00
|
|
|
const EnvGosukiDebug = "GOSUKI_DEBUG"
|
2022-11-17 22:03:18 +00:00
|
|
|
|
|
|
|
const Test = -1
|
2017-11-09 20:27:03 +00:00
|
|
|
const (
|
2022-11-17 22:03:18 +00:00
|
|
|
Release = iota
|
|
|
|
Info
|
|
|
|
Debug
|
2017-11-09 20:27:03 +00:00
|
|
|
)
|
|
|
|
|
2022-11-17 22:03:18 +00:00
|
|
|
func SetMode(lvl int) {
|
|
|
|
if lvl > Debug || lvl < -1 {
|
|
|
|
log.Warningf("using wrong debug level: %v", lvl)
|
|
|
|
return
|
2017-11-09 20:27:03 +00:00
|
|
|
}
|
2022-11-17 22:03:18 +00:00
|
|
|
LoggingMode = lvl
|
|
|
|
setLogLevel(lvl)
|
2017-11-09 20:27:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 22:03:18 +00:00
|
|
|
func initRuntimeMode() {
|
2018-11-09 17:25:50 +00:00
|
|
|
|
2023-02-18 23:13:23 +00:00
|
|
|
envDebug := os.Getenv(EnvGosukiDebug)
|
2018-11-09 17:25:50 +00:00
|
|
|
|
2022-11-17 22:03:18 +00:00
|
|
|
if envDebug != "" {
|
2023-02-18 23:13:23 +00:00
|
|
|
mode, err := strconv.Atoi(os.Getenv(EnvGosukiDebug))
|
2022-11-17 22:03:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("wrong debug level: %v\n%v", envDebug, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
SetMode(mode)
|
|
|
|
}
|
|
|
|
|
2022-12-10 01:48:50 +00:00
|
|
|
//TODO: disable debug log when testing
|
2018-11-09 17:25:50 +00:00
|
|
|
}
|