mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-18 04:27:06 +02:00
Using this `LoggingManager` all the log flows (reading and writing) were refactored. Inteface of `LoggingManager` should be now generic enough to replace log handling with almost any implementation - log rotation, sending logs to remote destination, keeping logs in memory, etc. There should be no functional changes. As part of changes, `follow.Reader` was implemented which makes appending file feel like a stream. `file.NewChunker` was refactored to use `follow.Reader` and `stream.NewChunker` to do the actual work. So basically now we have only a single instance of chunker - stream chunker, as everything is represented as a stream. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
package file
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/talos-systems/talos/pkg/chunker"
|
|
"github.com/talos-systems/talos/pkg/chunker/stream"
|
|
"github.com/talos-systems/talos/pkg/follow"
|
|
)
|
|
|
|
// Options is the functional options struct.
|
|
type Options struct {
|
|
Size int
|
|
Follow bool
|
|
}
|
|
|
|
// Option is the functional option func.
|
|
type Option func(*Options)
|
|
|
|
// WithSize sets the chunk size of the Chunker.
|
|
func WithSize(s int) Option {
|
|
return func(args *Options) {
|
|
args.Size = s
|
|
}
|
|
}
|
|
|
|
// WithFollow file updates using inotify().
|
|
func WithFollow() Option {
|
|
return func(args *Options) {
|
|
args.Follow = true
|
|
}
|
|
}
|
|
|
|
// Source is an interface describing the source of a File.
|
|
type Source = *os.File
|
|
|
|
// NewChunker initializes a Chunker with default values.
|
|
func NewChunker(ctx context.Context, source Source, setters ...Option) chunker.Chunker {
|
|
opts := &Options{
|
|
Size: 1024,
|
|
}
|
|
|
|
for _, setter := range setters {
|
|
setter(opts)
|
|
}
|
|
|
|
var r io.ReadCloser = source
|
|
|
|
if opts.Follow {
|
|
r = follow.NewReader(ctx, source)
|
|
}
|
|
|
|
return stream.NewChunker(ctx, r, stream.Size(opts.Size))
|
|
}
|