Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log interface #67

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"log"
"net"

xdr2 "github.com/rasky/go-xdr/xdr2"
Expand Down Expand Up @@ -60,17 +59,17 @@ func (c *conn) serve(ctx context.Context) {
}
return
}
log.Printf("request: %v", w.req)
Log.Tracef("request: %v", w.req)
err = c.handle(connCtx, w)
respErr := w.finish(connCtx)
if err != nil {
log.Printf("error handling req: %v", err)
Log.Errorf("error handling req: %v", err)
// failure to handle at a level needing to close the connection.
c.Close()
return
}
if respErr != nil {
log.Printf("error sending response: %v", respErr)
Log.Errorf("error sending response: %v", respErr)
c.Close()
return
}
Expand Down Expand Up @@ -117,7 +116,7 @@ func (c *conn) serializeWrites(ctx context.Context) {
func (c *conn) handle(ctx context.Context, w *response) error {
handler := c.Server.handlerFor(w.req.Header.Prog, w.req.Header.Proc)
if handler == nil {
log.Printf("No handler for %d.%d", w.req.Header.Prog, w.req.Header.Proc)
Log.Errorf("No handler for %d.%d", w.req.Header.Prog, w.req.Header.Proc)
if err := w.drain(ctx); err != nil {
return err
}
Expand All @@ -128,13 +127,13 @@ func (c *conn) handle(ctx context.Context, w *response) error {
return drainErr
}
if appError != nil && !w.responded {
log.Printf("call to %+v failed: %v", handler, appError)
Log.Errorf("call to %+v failed: %v", handler, appError)
if err := c.err(ctx, w, appError); err != nil {
return err
}
}
if !w.responded {
log.Printf("Handler did not indicate response status via writing or erroring")
Log.Errorf("Handler did not indicate response status via writing or erroring")
if err := c.err(ctx, w, &ResponseCodeSystemError{}); err != nil {
return err
}
Expand Down Expand Up @@ -288,7 +287,7 @@ func (c *conn) readRequestHeader(ctx context.Context, reader *bufio.Reader) (w *
return nil, err
}
if fragment&(1<<31) == 0 {
log.Printf("Warning: haven't implemented fragment reconstruction.\n")
Log.Warnf("Warning: haven't implemented fragment reconstruction.\n")
return nil, ErrInputInvalid
}
reqLen := fragment - uint32(1<<31)
Expand Down
3 changes: 1 addition & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nfs
import (
"errors"
"io"
"log"
"math"
"os"
"time"
Expand Down Expand Up @@ -131,7 +130,7 @@ func ToFileAttribute(info os.FileInfo) *FileAttribute {
func tryStat(fs billy.Filesystem, path []string) *FileAttribute {
attrs, err := fs.Stat(fs.Join(path...))
if err != nil || attrs == nil {
log.Printf("err loading attrs for %s: %v", fs.Join(path...), err)
Log.Errorf("err loading attrs for %s: %v", fs.Join(path...), err)
return nil
}
return ToFileAttribute(attrs)
Expand Down
216 changes: 216 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package nfs

import (
"fmt"
"log"
"os"
)

var (
Log Logger = &DefaultLogger{}
)

type LogLevel int

const (
PanicLevel LogLevel = iota
FatalLevel
ErrorLevel
WarnLevel
InfoLevel
DebugLevel
TraceLevel

panicLevelStr string = "[PANIC] "
fatalLevelStr string = "[FATAL] "
errorLevelStr string = "[ERROR] "
warnLevelStr string = "[WARN] "
infoLevelStr string = "[INFO] "
debugLevelStr string = "[DEBUG] "
traceLevelStr string = "[TRACE] "
)

type Logger interface {
SetLevel(level LogLevel)
GetLevel() LogLevel
ParseLevel(level string) (LogLevel, error)

Panic(args ...interface{})
Fatal(args ...interface{})
Error(args ...interface{})
Warn(args ...interface{})
Info(args ...interface{})
Debug(args ...interface{})
Trace(args ...interface{})
Print(args ...interface{})

Panicf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Warnf(format string, args ...interface{})
Infof(format string, args ...interface{})
Debugf(format string, args ...interface{})
Tracef(format string, args ...interface{})
Printf(format string, args ...interface{})
}

type DefaultLogger struct {
Level LogLevel
}

func SetLogger(logger Logger) {
Log = logger
}

func init() {
if os.Getenv("LOG_LEVEL") != "" {
if level, err := Log.ParseLevel(os.Getenv("LOG_LEVEL")); err == nil {
Log.SetLevel(level)
}
} else {
// set default log level to info
Log.SetLevel(InfoLevel)
}
}

func (l *DefaultLogger) GetLevel() LogLevel {
return l.Level
}

func (l *DefaultLogger) SetLevel(level LogLevel) {
l.Level = level
}

func (l *DefaultLogger) ParseLevel(level string) (LogLevel, error) {
switch level {
case "panic":
return PanicLevel, nil
case "fatal":
return FatalLevel, nil
case "error":
return ErrorLevel, nil
case "warn":
return WarnLevel, nil
case "info":
return InfoLevel, nil
case "debug":
return DebugLevel, nil
case "trace":
return TraceLevel, nil
}
var ll LogLevel
return ll, fmt.Errorf("invalid log level %q", level)
}

func (l *DefaultLogger) Panic(args ...interface{}) {
if l.Level < PanicLevel {
return
}
args = append([]interface{}{panicLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Panicf(format string, args ...interface{}) {
if l.Level < PanicLevel {
return
}
log.Printf(panicLevelStr+format, args...)
}

func (l *DefaultLogger) Fatal(args ...interface{}) {
if l.Level < FatalLevel {
return
}
args = append([]interface{}{fatalLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Fatalf(format string, args ...interface{}) {
if l.Level < FatalLevel {
return
}
log.Printf(fatalLevelStr+format, args...)
}

func (l *DefaultLogger) Error(args ...interface{}) {
if l.Level < ErrorLevel {
return
}
args = append([]interface{}{errorLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Errorf(format string, args ...interface{}) {
if l.Level < ErrorLevel {
return
}
log.Printf(errorLevelStr+format, args...)
}

func (l *DefaultLogger) Warn(args ...interface{}) {
if l.Level < WarnLevel {
return
}
args = append([]interface{}{warnLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Warnf(format string, args ...interface{}) {
if l.Level < WarnLevel {
return
}
log.Printf(warnLevelStr+format, args...)
}

func (l *DefaultLogger) Info(args ...interface{}) {
if l.Level < InfoLevel {
return
}
args = append([]interface{}{infoLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Infof(format string, args ...interface{}) {
if l.Level < InfoLevel {
return
}
log.Printf(infoLevelStr+format, args...)
}

func (l *DefaultLogger) Debug(args ...interface{}) {
if l.Level < DebugLevel {
return
}
args = append([]interface{}{debugLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Debugf(format string, args ...interface{}) {
if l.Level < DebugLevel {
return
}
log.Printf(debugLevelStr+format, args...)
}

func (l *DefaultLogger) Trace(args ...interface{}) {
if l.Level < TraceLevel {
return
}
args = append([]interface{}{traceLevelStr}, args...)
log.Print(args...)
}

func (l *DefaultLogger) Tracef(format string, args ...interface{}) {
if l.Level < TraceLevel {
return
}
log.Printf(traceLevelStr+format, args...)
}

func (l *DefaultLogger) Print(args ...interface{}) {
log.Print(args...)
}

func (l *DefaultLogger) Printf(format string, args ...interface{}) {
log.Printf(format, args...)
}
9 changes: 4 additions & 5 deletions nfs_oncreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nfs
import (
"bytes"
"context"
"log"
"os"

"github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -40,7 +39,7 @@ func onCreate(ctx context.Context, w *response, userHandle Handler) error {
if err := xdr.Read(w.req.Body, &verf); err != nil {
return &NFSStatusError{NFSStatusInval, err}
}
log.Printf("failing create to indicate lack of support for 'exclusive' mode.")
Log.Errorf("failing create to indicate lack of support for 'exclusive' mode.")
// TODO: support 'exclusive' mode.
return &NFSStatusError{NFSStatusNotSupp, os.ErrPermission}
} else {
Expand Down Expand Up @@ -78,18 +77,18 @@ func onCreate(ctx context.Context, w *response, userHandle Handler) error {

file, err := fs.Create(newFilePath)
if err != nil {
log.Printf("Error Creating: %v", err)
Log.Errorf("Error Creating: %v", err)
return &NFSStatusError{NFSStatusAccess, err}
}
if err := file.Close(); err != nil {
log.Printf("Error Creating: %v", err)
Log.Errorf("Error Creating: %v", err)
return &NFSStatusError{NFSStatusAccess, err}
}

fp := userHandle.ToHandle(fs, append(path, file.Name()))
changer := userHandle.Change(fs)
if err := attrs.Apply(changer, fs, newFilePath); err != nil {
log.Printf("Error applying attributes: %v\n", err)
Log.Errorf("Error applying attributes: %v\n", err)
return &NFSStatusError{NFSStatusIO, err}
}

Expand Down
3 changes: 1 addition & 2 deletions nfs_onlookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nfs
import (
"bytes"
"context"
"log"
"os"

"github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -87,6 +86,6 @@ func onLookup(ctx context.Context, w *response, userHandle Handler) error {
}
}

log.Printf("No file for lookup of %v\n", string(obj.Filename))
Log.Errorf("No file for lookup of %v\n", string(obj.Filename))
return &NFSStatusError{NFSStatusNoEnt, os.ErrNotExist}
}
5 changes: 2 additions & 3 deletions nfs_onwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"io"
"log"
"math"
"os"

Expand Down Expand Up @@ -79,11 +78,11 @@ func onWrite(ctx context.Context, w *response, userHandle Handler) error {
}
writtenCount, err := file.Write(req.Data[:end])
if err != nil {
log.Printf("Error writing: %v", err)
Log.Errorf("Error writing: %v", err)
return &NFSStatusError{NFSStatusIO, err}
}
if err := file.Close(); err != nil {
log.Printf("error closing: %v", err)
Log.Errorf("error closing: %v", err)
return &NFSStatusError{NFSStatusIO, err}
}

Expand Down
Loading