summaryrefslogtreecommitdiff
path: root/internal/io/dlog
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-01-29 21:39:06 +0200
committerPaul Buetow <paul@buetow.org>2026-01-29 21:39:06 +0200
commit599bdb74efcc97e86ce6023c1ae8265b1c2ff33b (patch)
tree6642a7401de622efc90947586f4db7a3e6ccd75d /internal/io/dlog
parenta32f028487c2e0b9e3144cf82d4153d1cd4a5243 (diff)
refactor: improve Go best practices compliance
- Add explicit interface satisfaction checks (var _ Interface = (*Type)(nil)) for compile-time verification: - TurboWriter implementations (DirectTurboWriter, TurboChannelWriter) - Processor implementations (GrepLineProcessor, ChannellessLineProcessor) - Parser implementations (genericParser, csvParser, genericKVParser, custom parsers, mimecastParser) - Logger implementations (file, stdout) - Handler implementations (ServerHandler, ClientHandler) - Connector implementations (Serverless, ServerConnection) - SSH callback implementations (KnownHostsCallback) - Improve error handling with context wrapping (%w): - SSH operations: GeneratePrivateRSAKey, Agent - Query parsing: Query.parse - SSH client connections: dial, session, handle methods - Fix receiver consistency: - Convert Query.String() from value to pointer receiver - Convert Outfile.String() from value to pointer receiver - Convert all KnownHostsCallback methods to pointer receivers - Convert mapCommand.Start() to pointer receiver - Reorganize file structure for better clarity: - internal/io/dlog/dlog.go: Move type definition before public functions - internal/mapr/token.go: Reorganize helper functions after public ones - Add documentation comments: - Query.String() method - Outfile.String() method - Regex.String() method - Improve config variable documentation All unit tests and integration tests pass. Amp-Thread-ID: https://ampcode.com/threads/T-019c0b08-0eeb-705d-a1f7-31bb764b659a Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'internal/io/dlog')
-rw-r--r--internal/io/dlog/dlog.go156
-rw-r--r--internal/io/dlog/loggers/file.go2
-rw-r--r--internal/io/dlog/loggers/stdout.go2
3 files changed, 82 insertions, 78 deletions
diff --git a/internal/io/dlog/dlog.go b/internal/io/dlog/dlog.go
index 180d2e4..951bedc 100644
--- a/internal/io/dlog/dlog.go
+++ b/internal/io/dlog/dlog.go
@@ -31,36 +31,6 @@ var Common *DLog
var mutex sync.Mutex
var started bool
-// Start logger(s).
-func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source.Source) {
- mutex.Lock()
- defer mutex.Unlock()
-
- if started {
- Common.FatalPanic("Logger already started")
- }
-
- Client = new(sourceProcess, source.Client)
- Server = new(sourceProcess, source.Server)
- Common = Client
- if sourceProcess == source.Server {
- Common = Server
- }
-
- var wg2 sync.WaitGroup
- wg2.Add(2)
- go Client.start(ctx, &wg2)
- go Server.start(ctx, &wg2)
-
- go rotation(ctx)
- go func() {
- wg2.Wait()
- wg.Done()
- }()
-
- started = true
-}
-
// DLog is the DTail logger.
type DLog struct {
logger loggers.Logger
@@ -94,62 +64,43 @@ func new(sourceProcess, sourcePackage source.Source) *DLog {
}
}
-func (d *DLog) start(ctx context.Context, wg *sync.WaitGroup) {
- defer wg.Done()
- var wg2 sync.WaitGroup
- wg2.Add(1)
- d.logger.Start(ctx, &wg2)
- <-ctx.Done()
- wg2.Wait()
-}
+// Start logger(s).
+func Start(ctx context.Context, wg *sync.WaitGroup, sourceProcess source.Source) {
+ mutex.Lock()
+ defer mutex.Unlock()
-func (d *DLog) log(level level, args []interface{}) string {
- if d.maxLevel < level {
- return ""
+ if started {
+ Common.FatalPanic("Logger already started")
}
- sb := pool.BuilderBuffer.Get().(*strings.Builder)
- defer pool.RecycleBuilderBuffer(sb)
- now := time.Now()
- switch d.sourceProcess {
- case source.Client:
- sb.WriteString(d.sourcePackage.String())
- sb.WriteString(protocol.FieldDelimiter)
- sb.WriteString(d.hostname)
- sb.WriteString(protocol.FieldDelimiter)
- sb.WriteString(level.String())
- default:
- sb.WriteString(level.String())
- sb.WriteString(protocol.FieldDelimiter)
- sb.WriteString(now.Format("0102-150405"))
+ Client = new(sourceProcess, source.Client)
+ Server = new(sourceProcess, source.Server)
+ Common = Client
+ if sourceProcess == source.Server {
+ Common = Server
}
- sb.WriteString(protocol.FieldDelimiter)
- d.writeArgStrings(sb, args)
- message := sb.String()
- if !config.Client.TermColorsEnable || !d.logger.SupportsColors() {
- d.logger.Log(now, message)
- return message
- }
+ var wg2 sync.WaitGroup
+ wg2.Add(2)
+ go Client.start(ctx, &wg2)
+ go Server.start(ctx, &wg2)
- d.logger.LogWithColors(now, message, brush.Colorfy(message))
- return message
+ go rotation(ctx)
+ go func() {
+ wg2.Wait()
+ wg.Done()
+ }()
+
+ started = true
}
-func (d *DLog) writeArgStrings(sb *strings.Builder, args []interface{}) {
- for i, arg := range args {
- if i > 0 {
- sb.WriteString(protocol.FieldDelimiter)
- }
- switch v := arg.(type) {
- case string:
- sb.WriteString(v)
- case error:
- sb.WriteString(v.Error())
- default:
- sb.WriteString(fmt.Sprintf("%v", v))
- }
- }
+func (d *DLog) start(ctx context.Context, wg *sync.WaitGroup) {
+ defer wg.Done()
+ var wg2 sync.WaitGroup
+ wg2.Add(1)
+ d.logger.Start(ctx, &wg2)
+ <-ctx.Done()
+ wg2.Wait()
}
// FatalPanic terminates the process with a fatal error.
@@ -278,3 +229,52 @@ func (d *DLog) Pause() { d.logger.Pause() }
// Resume the logging.
func (d *DLog) Resume() { d.logger.Resume() }
+
+func (d *DLog) log(level level, args []interface{}) string {
+ if d.maxLevel < level {
+ return ""
+ }
+ sb := pool.BuilderBuffer.Get().(*strings.Builder)
+ defer pool.RecycleBuilderBuffer(sb)
+ now := time.Now()
+
+ switch d.sourceProcess {
+ case source.Client:
+ sb.WriteString(d.sourcePackage.String())
+ sb.WriteString(protocol.FieldDelimiter)
+ sb.WriteString(d.hostname)
+ sb.WriteString(protocol.FieldDelimiter)
+ sb.WriteString(level.String())
+ default:
+ sb.WriteString(level.String())
+ sb.WriteString(protocol.FieldDelimiter)
+ sb.WriteString(now.Format("0102-150405"))
+ }
+ sb.WriteString(protocol.FieldDelimiter)
+ d.writeArgStrings(sb, args)
+
+ message := sb.String()
+ if !config.Client.TermColorsEnable || !d.logger.SupportsColors() {
+ d.logger.Log(now, message)
+ return message
+ }
+
+ d.logger.LogWithColors(now, message, brush.Colorfy(message))
+ return message
+}
+
+func (d *DLog) writeArgStrings(sb *strings.Builder, args []interface{}) {
+ for i, arg := range args {
+ if i > 0 {
+ sb.WriteString(protocol.FieldDelimiter)
+ }
+ switch v := arg.(type) {
+ case string:
+ sb.WriteString(v)
+ case error:
+ sb.WriteString(v.Error())
+ default:
+ sb.WriteString(fmt.Sprintf("%v", v))
+ }
+ }
+}
diff --git a/internal/io/dlog/loggers/file.go b/internal/io/dlog/loggers/file.go
index 8e567bc..5ac8d9e 100644
--- a/internal/io/dlog/loggers/file.go
+++ b/internal/io/dlog/loggers/file.go
@@ -32,6 +32,8 @@ type file struct {
strategy Strategy
}
+var _ Logger = (*file)(nil)
+
func newFile(strategy Strategy) *file {
return &file{
bufferCh: make(chan *fileMessageBuf, runtime.NumCPU()*100),
diff --git a/internal/io/dlog/loggers/stdout.go b/internal/io/dlog/loggers/stdout.go
index b024243..a2575c8 100644
--- a/internal/io/dlog/loggers/stdout.go
+++ b/internal/io/dlog/loggers/stdout.go
@@ -13,6 +13,8 @@ type stdout struct {
mutex sync.Mutex
}
+var _ Logger = (*stdout)(nil)
+
func newStdout() *stdout {
return &stdout{
pauseCh: make(chan struct{}),