github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/assert.go (about)

     1  // Package cos provides common low-level types and utilities for all aistore projects.
     2  /*
     3   * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package cos
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/NVIDIA/aistore/cmn/nlog"
    11  )
    12  
    13  const assertMsg = "assertion failed"
    14  
    15  // NOTE: Not to be used in the datapath - consider instead one of the flavors below.
    16  func Assertf(cond bool, f string, a ...any) {
    17  	if !cond {
    18  		AssertMsg(cond, fmt.Sprintf(f, a...))
    19  	}
    20  }
    21  
    22  func Assert(cond bool) {
    23  	if !cond {
    24  		nlog.Flush(nlog.ActExit)
    25  		panic(assertMsg)
    26  	}
    27  }
    28  
    29  // NOTE: when using Sprintf and such, `if (!cond) { AssertMsg(false, msg) }` is the preferable usage.
    30  func AssertMsg(cond bool, msg string) {
    31  	if !cond {
    32  		nlog.Flush(nlog.ActExit)
    33  		panic(assertMsg + ": " + msg)
    34  	}
    35  }
    36  
    37  func AssertNoErr(err error) {
    38  	if err != nil {
    39  		nlog.Flush(nlog.ActExit)
    40  		panic(err)
    41  	}
    42  }