X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=log.go;fp=log.go;h=08367cba92d784a21e6714f5bd008e0f672e27d6;hp=0000000000000000000000000000000000000000;hb=b7fb219307483d2c31b5dad1f559f325f2fd1a5e;hpb=def58d0f4944397faa8cdd4a9cf3515125e0b548 diff --git a/log.go b/log.go new file mode 100644 index 0000000..08367cb --- /dev/null +++ b/log.go @@ -0,0 +1,72 @@ +/* +goircd -- minimalistic simple Internet Relay Chat (IRC) server +Copyright (C) 2014-2020 Sergey Matveev + +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 . +*/ + +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) + } + } +}