github.com/go-spring/spring-base@v1.1.3/log/plugin.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package log
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"runtime"
    23  )
    24  
    25  const (
    26  	PluginTypeAppender = "Appender"
    27  	PluginTypeFilter   = "Filter"
    28  	PluginTypeLayout   = "Layout"
    29  )
    30  
    31  // plugins stores user registered Plugin(s) .
    32  var plugins = map[string]*Plugin{}
    33  
    34  // Plugin is the name of node label or XML element.
    35  type Plugin struct {
    36  	Name  string
    37  	Type  string
    38  	Class reflect.Type
    39  	File  string
    40  	Line  int
    41  }
    42  
    43  // RegisterPlugin registers a Plugin, `i` is used to obtain the type of Plugin.
    44  func RegisterPlugin(name string, typ string, i interface{}) {
    45  	_, file, line, _ := runtime.Caller(1)
    46  	if p, ok := plugins[name]; ok {
    47  		panic(fmt.Errorf("duplicate plugin %s in %s:%d and %s:%d", typ, p.File, p.Line, file, line))
    48  	}
    49  	class := reflect.TypeOf(i).Elem()
    50  	p := &Plugin{
    51  		Name:  name,
    52  		Type:  typ,
    53  		Class: class,
    54  		File:  file,
    55  		Line:  line,
    56  	}
    57  	plugins[name] = p
    58  }