73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
/*
|
|
goircd -- minimalistic simple Internet Relay Chat (IRC) server
|
|
Copyright (C) 2014-2020 Sergey Matveev <stargrave@stargrave.org>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, version 3 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
FormatMsg = "[%s] <%s> %s\n"
|
|
FormatMeta = "[%s] * %s %s\n"
|
|
)
|
|
|
|
var logSink chan LogEvent = make(chan LogEvent)
|
|
|
|
type LogEvent struct {
|
|
where string
|
|
who string
|
|
what string
|
|
meta bool
|
|
}
|
|
|
|
func Logger(logdir string, events <-chan LogEvent) {
|
|
mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
|
perm := os.FileMode(permLogFile)
|
|
var format string
|
|
var logfile string
|
|
var fd *os.File
|
|
var err error
|
|
for event := range events {
|
|
logfile = path.Join(logdir, event.where+".log")
|
|
fd, err = os.OpenFile(logfile, mode, perm)
|
|
if err != nil {
|
|
log.Println("can not open logfile", logfile, err)
|
|
continue
|
|
}
|
|
if event.meta {
|
|
format = FormatMeta
|
|
} else {
|
|
format = FormatMsg
|
|
}
|
|
_, err = fd.WriteString(fmt.Sprintf(
|
|
format,
|
|
time.Now().Format(time.RFC3339),
|
|
event.who,
|
|
event.what,
|
|
))
|
|
fd.Close()
|
|
if err != nil {
|
|
log.Println("error writing to logfile", logfile, err)
|
|
}
|
|
}
|
|
}
|