mirror of
https://github.com/fosrl/newt.git
synced 2026-03-03 01:06:44 +00:00
Log to file optionally and update config locations
This commit is contained in:
@@ -2,6 +2,7 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -48,6 +49,11 @@ func (l *Logger) SetLevel(level LogLevel) {
|
|||||||
l.level = level
|
l.level = level
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetOutput sets the output destination for the logger
|
||||||
|
func (l *Logger) SetOutput(w io.Writer) {
|
||||||
|
l.logger.SetOutput(w)
|
||||||
|
}
|
||||||
|
|
||||||
// log handles the actual logging
|
// log handles the actual logging
|
||||||
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
|
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
|
||||||
if level < l.level {
|
if level < l.level {
|
||||||
@@ -120,3 +126,8 @@ func Error(format string, args ...interface{}) {
|
|||||||
func Fatal(format string, args ...interface{}) {
|
func Fatal(format string, args ...interface{}) {
|
||||||
GetLogger().Fatal(format, args...)
|
GetLogger().Fatal(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetOutput sets the output destination for the default logger
|
||||||
|
func SetOutput(w io.Writer) {
|
||||||
|
GetLogger().SetOutput(w)
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,15 +8,16 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getConfigPath() string {
|
func getConfigPath(clientType string) string {
|
||||||
var configDir string
|
var configDir string
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "darwin":
|
case "darwin":
|
||||||
configDir = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "newt-client")
|
configDir = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", clientType+"-client")
|
||||||
case "windows":
|
case "windows":
|
||||||
configDir = filepath.Join(os.Getenv("APPDATA"), "newt-client")
|
logDir := filepath.Join(os.Getenv("PROGRAMDATA"), "olm")
|
||||||
|
configDir = filepath.Join(logDir, clientType+"-client")
|
||||||
default: // linux and others
|
default: // linux and others
|
||||||
configDir = filepath.Join(os.Getenv("HOME"), ".config", "newt-client")
|
configDir = filepath.Join(os.Getenv("HOME"), ".config", clientType+"-client")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||||
@@ -31,7 +32,7 @@ func (c *Client) loadConfig() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
configPath := getConfigPath()
|
configPath := getConfigPath(c.clientType)
|
||||||
data, err := os.ReadFile(configPath)
|
data, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -63,7 +64,7 @@ func (c *Client) loadConfig() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) saveConfig() error {
|
func (c *Client) saveConfig() error {
|
||||||
configPath := getConfigPath()
|
configPath := getConfigPath(c.clientType)
|
||||||
data, err := json.MarshalIndent(c.config, "", " ")
|
data, err := json.MarshalIndent(c.config, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user