github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/framework/log/logger.go (about)

     1  /*
     2  Copyright 2019 The Interconnectedcloud 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  	"time"
    22  
    23  	"github.com/onsi/ginkgo"
    24  
    25  	"github.com/interconnectedcloud/qdr-operator/test/e2e/framework/ginkgowrapper"
    26  )
    27  
    28  func nowStamp() string {
    29  	return time.Now().Format(time.StampMilli)
    30  }
    31  
    32  func log(level string, format string, args ...interface{}) {
    33  	fmt.Fprintf(ginkgo.GinkgoWriter, nowStamp()+": "+level+": "+format+"\n", args...)
    34  }
    35  
    36  // Logf logs the info.
    37  func Logf(format string, args ...interface{}) {
    38  	log("INFO", format, args...)
    39  }
    40  
    41  // Failf logs the fail info.
    42  func Failf(format string, args ...interface{}) {
    43  	FailfWithOffset(1, format, args...)
    44  }
    45  
    46  // FailfWithOffset calls "Fail" and logs the error at "offset" levels above its caller
    47  // (for example, for call chain f -> g -> FailfWithOffset(1, ...) error would be logged for "f").
    48  func FailfWithOffset(offset int, format string, args ...interface{}) {
    49  	msg := fmt.Sprintf(format, args...)
    50  	log("INFO", msg)
    51  	ginkgowrapper.Fail(nowStamp()+": "+msg, 1+offset)
    52  }