Allow for console output to be saved to a file

This commit is contained in:
MikeIsAStar 2024-04-21 22:40:00 -04:00
parent 28c65cc55a
commit 4bbb966634
5 changed files with 62 additions and 5 deletions

4
.gitignore vendored
View File

@ -4,9 +4,11 @@ salt.bin
*.der
*.pem
# Logs
logs
# Payload data
payload
# Editor files
.vscode

View File

@ -20,6 +20,7 @@ type Config struct {
EnableHTTPSExploitWii *bool `xml:"enableHttpsExploitWii,omitempty"`
EnableHTTPSExploitDS *bool `xml:"enableHttpsExploitDS,omitempty"`
LogLevel *int `xml:"logLevel"`
LogOutput string `xml:"logOutput"`
CertPath string `xml:"certPath"`
KeyPath string `xml:"keyPath"`
CertPathWii string `xml:"certDerPathWii"`
@ -78,5 +79,9 @@ func GetConfig() Config {
config.LogLevel = &level
}
if config.LogOutput == "" {
config.LogOutput = "StdOutAndFile"
}
return config
}

View File

@ -33,12 +33,25 @@
<username>username</username>
<password>password</password>
<!-- Database information-->
<!-- Database information -->
<databaseAddress>127.0.0.1</databaseAddress>
<databaseName>wwfc</databaseName>
<!-- Log verbosity -->
<!-- Logging configuration -->
<!-- Log verbosity
0: No messages are logged.
1: General messages are logged.
2: General and error messages are logged.
3: General, error, and warning messages are logged.
4: General, error, warning, and informational messages are logged.
-->
<logLevel>4</logLevel>
<!-- Log output
None : Messages are discarded.
StdOut : Messages are written to standard output.
StdOutAndFile: Messages are written to both standard output and a file.
-->
<logOutput>StdOutAndFile</logOutput>
<!-- API secret -->
<apiSecret>hQ3f57b3tW2WnjJH3v</apiSecret>

View File

@ -1,18 +1,52 @@
package logging
import (
"errors"
"fmt"
"io"
"log"
"os"
"time"
"github.com/logrusorgru/aurora/v3"
)
var logLevel = 0
var (
logDir = "./logs"
logLevel = 0
)
func SetLevel(level int) {
logLevel = level
}
func SetOutput(output string) error {
switch output {
case "None":
log.SetOutput(io.Discard)
case "StdOut":
log.SetOutput(os.Stdout)
case "StdOutAndFile":
if err := os.MkdirAll(logDir, 0700); err != nil {
return err
}
time := time.Now()
logFilePath := time.Format(logDir + "/2006-01-02-15-04-05.log")
file, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE, 0400)
if err != nil {
return err
}
log.SetOutput(io.MultiWriter(os.Stdout, file))
default:
return errors.New("invalid output value provided")
}
return nil
}
func Notice(module string, arguments ...any) {
if logLevel < 1 {
return

View File

@ -18,6 +18,9 @@ import (
func main() {
config := common.GetConfig()
logging.SetLevel(*config.LogLevel)
if err := logging.SetOutput(config.LogOutput); err != nil {
logging.Error("MAIN", err)
}
wg := &sync.WaitGroup{}
actions := []func(){nas.StartServer, gpcm.StartServer, qr2.StartServer, gpsp.StartServer, serverbrowser.StartServer, sake.StartServer, natneg.StartServer, api.StartServer, gamestats.StartServer}