]> Cypherpunks.ru repositories - goircd.git/blob - log.go
Unify copyright comment format
[goircd.git] / log.go
1 // goircd -- minimalistic simple Internet Relay Chat (IRC) server
2 // Copyright (C) 2014-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "fmt"
20         "log"
21         "os"
22         "path"
23         "time"
24 )
25
26 const (
27         FormatMsg  = "[%s] <%s> %s\n"
28         FormatMeta = "[%s] * %s %s\n"
29 )
30
31 var logSink chan LogEvent = make(chan LogEvent)
32
33 type LogEvent struct {
34         where string
35         who   string
36         what  string
37         meta  bool
38 }
39
40 func Logger(logdir string, events <-chan LogEvent) {
41         mode := os.O_CREATE | os.O_WRONLY | os.O_APPEND
42         perm := os.FileMode(permLogFile)
43         var format string
44         var logfile string
45         var fd *os.File
46         var err error
47         for event := range events {
48                 logfile = path.Join(logdir, event.where+".log")
49                 fd, err = os.OpenFile(logfile, mode, perm)
50                 if err != nil {
51                         log.Println("can not open logfile", logfile, err)
52                         continue
53                 }
54                 if event.meta {
55                         format = FormatMeta
56                 } else {
57                         format = FormatMsg
58                 }
59                 _, err = fd.WriteString(fmt.Sprintf(
60                         format,
61                         time.Now().Format(time.RFC3339),
62                         event.who,
63                         event.what,
64                 ))
65                 fd.Close()
66                 if err != nil {
67                         log.Println("error writing to logfile", logfile, err)
68                 }
69         }
70 }