code.gitea.io/gitea@v1.22.3/modules/log/event_writer.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package log
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  // EventWriter is the general interface for all event writers
    11  // EventWriterBase is only used as its base interface
    12  // A writer implementation could override the default EventWriterBase functions
    13  // eg: a writer can override the Run to handle events in its own way with its own goroutine
    14  type EventWriter interface {
    15  	EventWriterBase
    16  }
    17  
    18  // WriterMode is the mode for creating a new EventWriter, it contains common options for all writers
    19  // Its WriterOption field is the specified options for a writer, it should be passed by value but not by pointer
    20  type WriterMode struct {
    21  	BufferLen int
    22  
    23  	Level    Level
    24  	Prefix   string
    25  	Colorize bool
    26  	Flags    Flags
    27  
    28  	Expression string
    29  
    30  	StacktraceLevel Level
    31  
    32  	WriterOption any
    33  }
    34  
    35  // EventWriterProvider is the function for creating a new EventWriter
    36  type EventWriterProvider func(writerName string, writerMode WriterMode) EventWriter
    37  
    38  var eventWriterProviders = map[string]EventWriterProvider{}
    39  
    40  func RegisterEventWriter(writerType string, p EventWriterProvider) {
    41  	eventWriterProviders[writerType] = p
    42  }
    43  
    44  func HasEventWriter(writerType string) bool {
    45  	_, ok := eventWriterProviders[writerType]
    46  	return ok
    47  }
    48  
    49  func NewEventWriter(name, writerType string, mode WriterMode) (EventWriter, error) {
    50  	if p, ok := eventWriterProviders[writerType]; ok {
    51  		return p(name, mode), nil
    52  	}
    53  	return nil, fmt.Errorf("unknown event writer type %q for writer %q", writerType, name)
    54  }