- FIX: Query logger: moved to different thread to serialize access to log file

pull/1/head
bloved 2021-01-17 16:15:12 +01:00
parent 749029ba5b
commit 0752a7e443
2 changed files with 34 additions and 7 deletions

View File

@ -40,7 +40,7 @@ func ZabovCreateKDB(conf string) {
if err != nil { if err != nil {
fmt.Println("Cannot create Killfile db: ", err.Error()) fmt.Println("Cannot create Killfile db: ", err.Error())
} else { } else {
fmt.Println("Killfile DB created") fmt.Println("Killfile DB created:", dbname)
} }
MyZabovKDBs[conf] = KDB MyZabovKDBs[conf] = KDB

View File

@ -16,7 +16,20 @@ var reqTypes map[uint16]string
var weekdays []string var weekdays []string
type logItem struct {
clientIP string
name string
reqType uint16
config string
timetable string
killed string
}
// logChannel used by logging thread
var logChannel chan logItem
func init() { func init() {
weekdays = []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"} weekdays = []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}
if len(ZabovDebugDBPath) > 0 { if len(ZabovDebugDBPath) > 0 {
@ -107,13 +120,18 @@ func init() {
dns.TypeReserved: "TypeReserved"} dns.TypeReserved: "TypeReserved"}
fmt.Println("Local Time:", getLocalTime().Format(time.ANSIC)) fmt.Println("Local Time:", getLocalTime().Format(time.ANSIC))
if len(ZabovDebugDBPath) > 0 {
logChannel = make(chan logItem, 1024)
go logWriteThread()
}
} }
func logQuery(clientIP string, name string, reqType uint16, config string, timetable string, killed string) { func logWriteThread() {
if len(ZabovDebugDBPath) > 0 { for item := range logChannel {
var header string var header string
d := time.Now().Format("2006-01-02") d := time.Now().Format("2006-01-02")
logpath := path.Join(ZabovDebugDBPath, strings.Replace(clientIP, ":", "_", -1)+"-"+d+".log") logpath := path.Join(ZabovDebugDBPath, strings.Replace(item.clientIP, ":", "_", -1)+"-"+d+".log")
_, err1 := os.Stat(logpath) _, err1 := os.Stat(logpath)
if os.IsNotExist(err1) { if os.IsNotExist(err1) {
@ -121,12 +139,12 @@ func logQuery(clientIP string, name string, reqType uint16, config string, timet
} }
f, err := os.OpenFile(logpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(logpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil { if err == nil {
reqTypeName, err := reqTypes[reqType] reqTypeName, err := reqTypes[item.reqType]
if !err { if !err {
reqTypeName = fmt.Sprintf("%d", reqType) reqTypeName = fmt.Sprintf("%d", item.reqType)
} }
ct := time.Now().Format(time.RFC3339) ct := time.Now().Format(time.RFC3339)
log := strings.Join([]string{ct, clientIP, strings.TrimRight(name, "."), reqTypeName, config, timetable, killed}, "\t") log := strings.Join([]string{ct, item.clientIP, strings.TrimRight(item.name, "."), reqTypeName, item.config, item.timetable, item.killed}, "\t")
if len(header) > 0 { if len(header) > 0 {
f.Write([]byte(header)) f.Write([]byte(header))
f.Write([]byte("\n")) f.Write([]byte("\n"))
@ -138,6 +156,15 @@ func logQuery(clientIP string, name string, reqType uint16, config string, timet
} }
} }
func logQuery(clientIP string, name string, reqType uint16, config string, timetable string, killed string) {
if len(ZabovDebugDBPath) > 0 {
k := logItem{clientIP: clientIP, name: name, reqType: reqType, config: config, timetable: timetable, killed: killed}
logChannel <- k
}
}
func getLocalTime() time.Time { func getLocalTime() time.Time {
return time.Now().Local() return time.Now().Local()
} }