Initial commit
This commit is contained in:
56
internal/moon/chatlog.go
Normal file
56
internal/moon/chatlog.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package moon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ChatLog logs message to separate file, placed in basedir(Cfg.LogFile).
|
||||
func ChatLog(chatid int64, text string) {
|
||||
file := os.DevNull
|
||||
|
||||
if Cfg.LogFile != "" {
|
||||
file = fmt.Sprintf("%s/%s", filepath.Dir(Cfg.LogFile), strconv.FormatInt(chatid, 10))
|
||||
}
|
||||
|
||||
var (
|
||||
isOpened = false
|
||||
)
|
||||
|
||||
for _, l := range chatlogs {
|
||||
if l.LogFilePath == file {
|
||||
isOpened = true
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isOpened {
|
||||
// open log file
|
||||
logFile, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to open log file %s: %s", file, err)
|
||||
}
|
||||
|
||||
l := Chatlog{LogFilePath: file, File: logFile}
|
||||
|
||||
chatlogs = append(chatlogs, l)
|
||||
}
|
||||
|
||||
for _, l := range chatlogs {
|
||||
if l.LogFilePath == file {
|
||||
log.SetOutput(l.File)
|
||||
// optional: log date-time, filename, and line number
|
||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
||||
log.Println(text)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||
Reference in New Issue
Block a user