about summary refs log tree commit diff
path: root/pkg/k9p/logger/logger.go
blob: 6b2e4372b25acdfde4ed158cad7657e34c9cfe88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package logger

import (
	"context"
	"time"

	p9p "github.com/docker/go-p9p"
	"github.com/rs/zerolog"
)

type Logger struct {
	logger  zerolog.Logger
	session p9p.Session
}

func New(logger zerolog.Logger, session p9p.Session) *Logger {
	return &Logger{
		logger:  logger,
		session: session,
	}
}

func (l *Logger) Auth(ctx context.Context, afid p9p.Fid, uname string, aname string) (qid p9p.Qid, err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "auth").
			Uint32("afid", uint32(afid)).
			Str("uname", uname).
			Str("aname", aname).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Str("qid", qid.String()).
				Err(err)).
			Msg("")
	}(time.Now())

	return l.session.Auth(ctx, afid, uname, aname)
}

func (l *Logger) Attach(ctx context.Context, fid p9p.Fid, afid p9p.Fid, uname string, aname string) (qid p9p.Qid, err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "attach").
			Uint32("fid", uint32(fid)).
			Uint32("afid", uint32(afid)).
			Str("uname", uname).
			Str("aname", aname).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Str("qid", qid.String()).
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Attach(ctx, fid, afid, uname, aname)
}

func (l *Logger) Clunk(ctx context.Context, fid p9p.Fid) (err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "clunk").
			Uint32("fid", uint32(fid)).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Clunk(ctx, fid)
}

func (l *Logger) Remove(ctx context.Context, fid p9p.Fid) (err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "remove").
			Uint32("fid", uint32(fid)).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Remove(ctx, fid)
}

func (l *Logger) Walk(ctx context.Context, fid p9p.Fid, newfid p9p.Fid, names ...string) (qids []p9p.Qid, err error) {
	defer func(t1 time.Time) {
		arr := zerolog.Arr()
		for _, qid := range qids {
			arr = arr.Str(qid.String())
		}

		l.logger.Debug().
			Str("request", "walk").
			Uint32("fid", uint32(fid)).
			Uint32("newfid", uint32(newfid)).
			Strs("names", names).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Array("qids", arr).
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Walk(ctx, fid, newfid, names...)
}

// Read follows the semantics of io.ReaderAt.ReadAtt method except it takes
// a contxt and Fid.
func (l *Logger) Read(ctx context.Context, fid p9p.Fid, p []byte, offset int64) (n int, err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "read").
			Uint32("fid", uint32(fid)).
			Int64("offset", offset).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Int("n", n).
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Read(ctx, fid, p, offset)
}

// Write follows the semantics of io.WriterAt.WriteAt except takes a context and an Fid.
//
// If n == len(p), no error is returned.
// If n < len(p), io.ErrShortWrite will be returned.
func (l *Logger) Write(ctx context.Context, fid p9p.Fid, p []byte, offset int64) (n int, err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "write").
			Uint32("fid", uint32(fid)).
			Int64("offset", offset).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Int("n", n).
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Write(ctx, fid, p, offset)
}

func (l *Logger) Open(ctx context.Context, fid p9p.Fid, mode p9p.Flag) (qid p9p.Qid, iounit uint32, err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "open").
			Uint32("fid", uint32(fid)).
			Uint8("mode", uint8(mode)).
			TimeDiff("duration", time.Now(), t1).
			Dict("ret", zerolog.Dict().
				Str("qid", qid.String()).
				Uint32("iounit", iounit).
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Open(ctx, fid, mode)
}

func (l *Logger) Create(ctx context.Context, parent p9p.Fid, name string, perm uint32, mode p9p.Flag) (p9p.Qid, uint32, error) {
	l.logger.Debug().
		Str("request", "create").
		Msg("")
	return l.session.Create(ctx, parent, name, perm, mode)
}

func (l *Logger) Stat(ctx context.Context, fid p9p.Fid) (dir p9p.Dir, err error) {
	defer func(t1 time.Time) {
		l.logger.Debug().
			Str("request", "stat").
			Uint32("fid", uint32(fid)).
			Dict("ret", zerolog.Dict().
				Dict("dir", zerolog.Dict().
					Str("name", dir.Name).
					Str("qid", dir.Qid.String()).
					Uint64("length", dir.Length).
					Time("modTime", dir.ModTime)).
				Err(err)).
			Msg("")
	}(time.Now())
	return l.session.Stat(ctx, fid)
}

func (l *Logger) WStat(ctx context.Context, fid p9p.Fid, dir p9p.Dir) error {
	l.logger.Debug().
		Str("request", "stat").
		Msg("")
	return l.session.WStat(ctx, fid, dir)
}

// Version returns the supported version and msize of the session. This
// can be affected by negotiating or the level of support provided by the
// session implementation.
func (l *Logger) Version() (msize int, version string) {
	l.logger.Debug().
		Str("request", "stat").
		Msg("")
	return l.session.Version()
}