Skip to content
Open
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
4 changes: 2 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@
return 1, fmt.Errorf("command %v doesn't support execution through agent", strings.Join(name, " "))
}

cmd.SetLogInfo(ctx.GetLogger().EnabledInfo)
cmd.SetLogTraces(ctx.GetLogger().EnabledTracing)
cmd.SetLogInfo(ctx.GetLogger().InfoEnabled())

Check failure on line 163 in agent/agent.go

View workflow job for this annotation

GitHub Actions / build (1.24.0)

ctx.GetLogger().InfoEnabled undefined (type *logging.Logger has no field or method InfoEnabled)

Check failure on line 163 in agent/agent.go

View workflow job for this annotation

GitHub Actions / build (1.24.0)

ctx.GetLogger().InfoEnabled undefined (type *logging.Logger has no field or method InfoEnabled)
cmd.SetLogTraces(ctx.GetLogger().TracingEnabled())

Check failure on line 164 in agent/agent.go

View workflow job for this annotation

GitHub Actions / build (1.24.0)

ctx.GetLogger().TracingEnabled undefined (type *logging.Logger has no field or method TracingEnabled)

Check failure on line 164 in agent/agent.go

View workflow job for this annotation

GitHub Actions / build (1.24.0)

ctx.GetLogger().TracingEnabled undefined (type *logging.Logger has no field or method TracingEnabled)

if err := subcommands.EncodeRPC(c.enc, name, cmd, storeConfig); err != nil {
return 1, err
Expand Down
101 changes: 101 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package logging

import (
"io"
"log"
"strings"
"sync"
)

type Logger struct {
EnabledInfo bool
EnabledTracing string
mutraceSubsystems sync.Mutex
traceSubsystems map[string]bool
stdoutLogger *log.Logger
stderrLogger *log.Logger
}

func NewLogger(stdout io.Writer, stderr io.Writer) *Logger {
return &Logger{
EnabledInfo: false,
EnabledTracing: "",
stdoutLogger: log.New(stdout, "", 0),
stderrLogger: log.New(stderr, "", 0),
traceSubsystems: make(map[string]bool),
}
}

func (l *Logger) SetOutput(w io.Writer) {
l.stdoutLogger.SetOutput(w)
l.stderrLogger.SetOutput(w)
}

func (l *Logger) SetSyslogOutput(w io.Writer) {
l.stdoutLogger = log.New(w, "stdout", 0)
l.stderrLogger = log.New(w, "stderr", 0)
}

func (l *Logger) Printf(format string, args ...interface{}) {
l.stdoutLogger.Printf(format, args...)
}

func (l *Logger) Stdout(format string, args ...interface{}) {
l.stdoutLogger.Printf(format, args...)
}

func (l *Logger) Stderr(format string, args ...interface{}) {
l.stderrLogger.Printf(format, args...)
}

func (l *Logger) Info(format string, args ...interface{}) {
if l.EnabledInfo {
l.stdoutLogger.Printf("info: "+format, args...)
}
}

func (l *Logger) Warn(format string, args ...interface{}) {
l.stderrLogger.Printf("warn: "+format, args...)
}

func (l *Logger) Error(format string, args ...interface{}) {
l.stderrLogger.Printf("error: "+format, args...)
}

func (l *Logger) Debug(format string, args ...interface{}) {
l.stderrLogger.Printf("debug: "+format, args...)
}

func (l *Logger) Trace(subsystem string, format string, args ...interface{}) {
if l.EnabledTracing != "" {
l.mutraceSubsystems.Lock()
_, exists := l.traceSubsystems[subsystem]
if !exists {
_, exists = l.traceSubsystems["all"]
}
l.mutraceSubsystems.Unlock()
if exists {
l.stdoutLogger.Printf("trace: "+subsystem+": "+format, args...)
}
}
}

func (l *Logger) EnableInfo() {
l.EnabledInfo = true
}

func (l *Logger) InfoEnabled() bool {
return l.EnabledInfo
}

func (l *Logger) EnableTracing(traces string) {
l.EnabledTracing = traces
l.traceSubsystems = make(map[string]bool)
for _, subsystem := range strings.Split(traces, ",") {
l.traceSubsystems[subsystem] = true
}
}

func (l *Logger) TracingEnabled() string {
return l.EnabledTracing
}
155 changes: 155 additions & 0 deletions logging/logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package logging

import (
"bytes"
"sync"
"testing"
)

func TestLogger(t *testing.T) {
bufOut := bytes.NewBuffer(nil)
bufErr := bytes.NewBuffer(nil)

// Create a new logger
logger := NewLogger(bufOut, bufErr)

// Test Printf
logger.Printf("Test message")
if bufOut.String() != "Test message\n" {
t.Errorf("Printf did not produce expected output")
}
bufOut.Reset()

// Test Stdout
logger.Stdout("Test message")
if bufOut.String() != "Test message\n" {
t.Errorf("Stdout did not produce expected output")
}
bufOut.Reset()

// Test Stderr
logger.Stderr("Test message")
if bufErr.String() != "Test message\n" {
t.Errorf("Stderr did not produce expected output")
}
bufErr.Reset()

// Test Warn
logger.Warn("Test message")
if bufErr.String() != "warn: Test message\n" {
t.Errorf("Warn did not produce expected output")
}
bufErr.Reset()

// Test Error
logger.Error("Test message")
if bufErr.String() != "error: Test message\n" {
t.Errorf("Error did not produce expected output")
}
bufErr.Reset()

// Test Debug
logger.Debug("Test message")
if bufErr.String() != "debug: Test message\n" {
t.Errorf("Debug did not produce expected output")
}
bufOut.Reset()

// Test Info without enabling info
logger.Info("Test message")
if bufOut.String() != "" {
t.Errorf("Info should not produce output")
}

// Test EnableInfo
logger.EnableInfo()
if !logger.EnabledInfo {
t.Errorf("EnableInfo did not enable info logging")
}

// Test Info
logger.Info("Test message")
if bufOut.String() != "info: Test message\n" {
t.Errorf("Info did not produce expected output")
}
bufOut.Reset()

// Test Trace without enabling trace
logger.Trace("subsystem", "Test message")
if bufOut.String() != "" {
t.Errorf("Trace should not produce output")
}
bufOut.Reset()

// Test EnableTrace
logger.EnableTracing("subsystem")
if logger.EnabledTracing == "" {
t.Errorf("EnableTrace did not enable tracing")
}
if _, ok := logger.traceSubsystems["subsystem"]; !ok {
t.Errorf("EnableTrace did not add subsystem to tracing")
}

// Test Trace
bufOut.Reset()
logger.Trace("subsystem", "Test message")
if bufOut.String() != "trace: subsystem: Test message\n" {
t.Errorf("Trace did not produce expected output")
}

// Test Trace with unknown subsystem but not all tracing subsystem enabled
bufOut.Reset()
logger.Trace("unknown", "Test message")
if bufOut.String() != "" {
t.Errorf("Trace should not produce output")
}
logger.EnableTracing("all")
logger.Trace("unknown", "Test message")
if bufOut.String() != "trace: unknown: Test message\n" {
t.Errorf("Trace did not produce expected output")
}
bufOut.Reset()
}

func TestLoggerConcurrency(t *testing.T) {
// Create a new logger
bufOut := bytes.NewBuffer(nil)
bufErr := bytes.NewBuffer(nil)

// Create a new logger
logger := NewLogger(bufOut, bufErr)

// Test concurrent logging
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
logger.Printf("Test message %d", i)
}()
}
wg.Wait()
if bufOut.String() == "" {
t.Errorf("Concurrent logging produced unexpected output")
}
}

func TestLoggerPanic(t *testing.T) {
// Create a new logger
bufOut := bytes.NewBuffer(nil)
bufErr := bytes.NewBuffer(nil)

// Create a new logger
logger := NewLogger(bufOut, bufErr)

// Test panic logging
defer func() {
if r := recover(); r != nil {
logger.Printf("Recovered panic: %v", r)
}
if bufOut.String() != "Recovered panic: Test panic\n" {
t.Errorf("Panic logging did not produce expected output")
}
}()
panic("Test panic")
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (

"github.com/PlakarKorp/kloset/caching"
"github.com/PlakarKorp/kloset/encryption"
"github.com/PlakarKorp/kloset/logging"
"github.com/PlakarKorp/kloset/repository"
"github.com/PlakarKorp/kloset/storage"
"github.com/PlakarKorp/kloset/versioning"
"github.com/PlakarKorp/plakar/agent"
"github.com/PlakarKorp/plakar/appcontext"
"github.com/PlakarKorp/plakar/cookies"
"github.com/PlakarKorp/plakar/logging"
"github.com/PlakarKorp/plakar/plugins"
"github.com/PlakarKorp/plakar/subcommands"
"github.com/PlakarKorp/plakar/task"
Expand Down
2 changes: 1 addition & 1 deletion reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Report struct {
Snapshot *ReportSnapshot `json:"report_snapshot,omitempty"`

repo *repository.Repository `json:"-"`
logger *logging.Logger `json:"-"`
logger logging.Logger `json:"-"`
reporter chan *Report `json:"-"`
ignore bool `json:"-"`
}
2 changes: 1 addition & 1 deletion subcommands/agent/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"time"

"github.com/PlakarKorp/kloset/encryption"
"github.com/PlakarKorp/kloset/logging"
"github.com/PlakarKorp/plakar/logging"
"github.com/PlakarKorp/kloset/repository"
"github.com/PlakarKorp/kloset/storage"
"github.com/PlakarKorp/plakar/agent"
Expand Down
Loading