-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
andy
committed
Mar 14, 2023
1 parent
b81192b
commit 950b317
Showing
5 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var mutex sync.Mutex | ||
|
||
func setLog(logPath string, discard bool) { | ||
if discard { | ||
log.SetOutput(io.Discard) | ||
return | ||
} | ||
if logPath == "" { | ||
homePath, err := os.UserHomeDir() | ||
if err != nil { | ||
logPath = "/tmp/chatbash" | ||
} | ||
if runtime.GOOS == "darwin" { | ||
logPath = filepath.Join(homePath, "Library/Logs/chatbash") | ||
} else if runtime.GOOS == "linux" { | ||
logPath = filepath.Join(homePath, ".log/chatbash") | ||
} | ||
} | ||
if err := os.MkdirAll(logPath, 0755); err != nil { | ||
log.Fatal("Failed to create log dir: " + err.Error()) | ||
} | ||
logFile, err := os.OpenFile(filepath.Join(logPath, time.Now().Format("20060102")+".log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
log.Fatal("Failed to open log file: " + err.Error()) | ||
} | ||
log.SetOutput(logFile) | ||
} | ||
|
||
func writeLog(level string, format string, v ...any) { | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
msg := fmt.Sprintf(format, v...) | ||
log.Printf("[%s] %s", level, msg) | ||
} | ||
|
||
func InfoLog(format string, v ...any) { | ||
go writeLog("info", format, v...) | ||
} | ||
|
||
func ErrLog(format string, v ...any) { | ||
go writeLog("error", format, v...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters