...

Source file src/go.etcd.io/etcd/raft/v3/rafttest/interaction_env_logger.go

Documentation: go.etcd.io/etcd/raft/v3/rafttest

     1  // Copyright 2019 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rafttest
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"go.etcd.io/etcd/raft/v3"
    22  )
    23  
    24  type logLevels [6]string
    25  
    26  var lvlNames logLevels = [...]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE"}
    27  
    28  type RedirectLogger struct {
    29  	*strings.Builder
    30  	Lvl int // 0 = DEBUG, 1 = INFO, 2 = WARNING, 3 = ERROR, 4 = FATAL, 5 = NONE
    31  }
    32  
    33  var _ raft.Logger = (*RedirectLogger)(nil)
    34  
    35  func (l *RedirectLogger) printf(lvl int, format string, args ...interface{}) {
    36  	if l.Lvl <= lvl {
    37  		fmt.Fprint(l, lvlNames[lvl], " ")
    38  		fmt.Fprintf(l, format, args...)
    39  		if n := len(format); n > 0 && format[n-1] != '\n' {
    40  			l.WriteByte('\n')
    41  		}
    42  	}
    43  }
    44  func (l *RedirectLogger) print(lvl int, args ...interface{}) {
    45  	if l.Lvl <= lvl {
    46  		fmt.Fprint(l, lvlNames[lvl], " ")
    47  		fmt.Fprintln(l, args...)
    48  	}
    49  }
    50  
    51  func (l *RedirectLogger) Debug(v ...interface{}) {
    52  	l.print(0, v...)
    53  }
    54  
    55  func (l *RedirectLogger) Debugf(format string, v ...interface{}) {
    56  	l.printf(0, format, v...)
    57  }
    58  
    59  func (l *RedirectLogger) Info(v ...interface{}) {
    60  	l.print(1, v...)
    61  }
    62  
    63  func (l *RedirectLogger) Infof(format string, v ...interface{}) {
    64  	l.printf(1, format, v...)
    65  }
    66  
    67  func (l *RedirectLogger) Warning(v ...interface{}) {
    68  	l.print(2, v...)
    69  }
    70  
    71  func (l *RedirectLogger) Warningf(format string, v ...interface{}) {
    72  	l.printf(2, format, v...)
    73  }
    74  
    75  func (l *RedirectLogger) Error(v ...interface{}) {
    76  	l.print(3, v...)
    77  }
    78  
    79  func (l *RedirectLogger) Errorf(format string, v ...interface{}) {
    80  	l.printf(3, format, v...)
    81  }
    82  
    83  func (l *RedirectLogger) Fatal(v ...interface{}) {
    84  	l.print(4, v...)
    85  }
    86  
    87  func (l *RedirectLogger) Fatalf(format string, v ...interface{}) {
    88  
    89  	l.printf(4, format, v...)
    90  }
    91  
    92  func (l *RedirectLogger) Panic(v ...interface{}) {
    93  	l.print(4, v...)
    94  }
    95  
    96  func (l *RedirectLogger) Panicf(format string, v ...interface{}) {
    97  	l.printf(4, format, v...)
    98  }
    99  

View as plain text