github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/core/log/log.go (about)

     1  /*
     2   * Copyright 2022 The Gremlins 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   *        http://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  	"io"
    22  	"sync"
    23  
    24  	"github.com/fatih/color"
    25  
    26  	"github.com/go-maxhub/gremlins/core/configuration"
    27  )
    28  
    29  var fgRed = color.New(color.FgRed).SprintFunc()
    30  
    31  var mutex = &sync.Mutex{}
    32  var instance *log
    33  
    34  // Init initializes a new logger with the given out and eOut io.Writer.
    35  // If no out is  provided the logger behaves as NoOp. The initialized instance
    36  // is a singleton.
    37  //
    38  // If one of the logging methods is called, and the logger hasn't been
    39  // initialized yet, a new logger will be initialized with a noOp out.
    40  func Init(out, eOut io.Writer) {
    41  	if out == nil || eOut == nil {
    42  		return
    43  	}
    44  	if instance == nil {
    45  		mutex.Lock()
    46  		defer mutex.Unlock()
    47  		if instance == nil {
    48  			instance = &log{out: out, eOut: eOut}
    49  		}
    50  	}
    51  }
    52  
    53  // Reset removes the current log instance.
    54  func Reset() {
    55  	instance = nil
    56  }
    57  
    58  // Infof logs an information using format.
    59  func Infof(f string, args ...any) {
    60  	if instance == nil {
    61  		return
    62  	}
    63  	instance.writef(f, args...)
    64  }
    65  
    66  // Infoln logs an information line.
    67  func Infoln(a any) {
    68  	if instance == nil {
    69  		return
    70  	}
    71  	instance.writeln(a)
    72  }
    73  
    74  // Errorf logs an error using format.
    75  func Errorf(f string, args ...any) {
    76  	if instance == nil {
    77  		return
    78  	}
    79  	msg := fmt.Sprintf(f, args...)
    80  	instance.eWritef("%s: %s", fgRed("ERROR"), msg)
    81  }
    82  
    83  // Errorln logs an error line.
    84  func Errorln(a any) {
    85  	if instance == nil {
    86  		return
    87  	}
    88  	msg := fmt.Sprintf("%s: %s", fgRed("ERROR"), a)
    89  	instance.eWriteln(msg)
    90  }
    91  
    92  type log struct {
    93  	out  io.Writer
    94  	eOut io.Writer
    95  }
    96  
    97  func (l *log) writef(f string, args ...any) {
    98  	if configuration.Get[bool]("silent") {
    99  		return
   100  	}
   101  	_, _ = fmt.Fprintf(l.out, f, args...)
   102  }
   103  
   104  func (l *log) writeln(a any) {
   105  	if configuration.Get[bool]("silent") {
   106  		return
   107  	}
   108  	_, _ = fmt.Fprintln(l.out, a)
   109  }
   110  
   111  func (l *log) eWritef(f string, args ...any) {
   112  	_, _ = fmt.Fprintf(l.eOut, f, args...)
   113  }
   114  
   115  func (l *log) eWriteln(a any) {
   116  	_, _ = fmt.Fprintln(l.eOut, a)
   117  }