maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1/*
 2Maddy Mail Server - Composable all-in-one email server.
 3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 4
 5This program is free software: you can redistribute it and/or modify
 6it under the terms of the GNU General Public License as published by
 7the Free Software Foundation, either version 3 of the License, or
 8(at your option) any later version.
 9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19package log
20
21import (
22	"time"
23)
24
25type Output interface {
26	Write(stamp time.Time, debug bool, msg string)
27	Close() error
28}
29
30type multiOut struct {
31	outs []Output
32}
33
34func (m multiOut) Write(stamp time.Time, debug bool, msg string) {
35	for _, out := range m.outs {
36		out.Write(stamp, debug, msg)
37	}
38}
39
40func (m multiOut) Close() error {
41	for _, out := range m.outs {
42		if err := out.Close(); err != nil {
43			return err
44		}
45	}
46	return nil
47}
48
49func MultiOutput(outputs ...Output) Output {
50	return multiOut{outputs}
51}
52
53type funcOut struct {
54	out   func(time.Time, bool, string)
55	close func() error
56}
57
58func (f funcOut) Write(stamp time.Time, debug bool, msg string) {
59	f.out(stamp, debug, msg)
60}
61
62func (f funcOut) Close() error {
63	return f.close()
64}
65
66func FuncOutput(f func(time.Time, bool, string), close func() error) Output {
67	return funcOut{f, close}
68}
69
70type NopOutput struct{}
71
72func (NopOutput) Write(time.Time, bool, string) {}
73
74func (NopOutput) Close() error { return nil }