goircd/log.go
Sergey Matveev b7fb219307 Many fixes and additions
* NAMES and WALLOPS command
* -cloak option
* -v renamed to -verbose
* -passwords renamed to -passwd
* -debug option prints traffic messages
* without -verbose only startup/shutdown and error messages are printed
* -timestamped option provides timestamps in printed messages, as
  earlier. No timestamps is useful for running under daemontools
* passwords are stored in SHA256-hashed format
* state files replaced with state directory with files
* removed many unnecessary pointers and locks
* graceful shutdown with all clients notification
* fixed time structure printing in log files, instead of short human
  readable timestamp
* PART is sent to the user itself, to notify his client about leaving
* log messages are printed to stdout, instead of stderr, for
  friendliness with daemontools logger
* ability to configure newly created directories and files with
  -perm-state-dir, -perm-state-file, -perm-log-file
2020-11-07 16:52:53 +03:00

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)
}
}
}