github.com/haraldrudell/parl@v0.4.176/plog/output-invoker.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package plog
     7  
     8  import (
     9  	"github.com/haraldrudell/parl/pruntime"
    10  )
    11  
    12  // OutputInvoker provides Invoke function for specific code location
    13  type OutputInvoker struct {
    14  	// the code location appended to debug print
    15  	codeLocation *pruntime.CodeLocation
    16  	// [LogInstance.invokeOutput]
    17  	invokeOutput func(s string)
    18  }
    19  
    20  // NewOutputInvoker returns a [LogInstance.invokeOutput] function for a specific code location
    21  func NewOutputInvoker(
    22  	codeLocation *pruntime.CodeLocation,
    23  	invokeOutput func(s string),
    24  ) (outputInvoker *OutputInvoker) {
    25  	return &OutputInvoker{
    26  		codeLocation: codeLocation,
    27  		invokeOutput: invokeOutput,
    28  	}
    29  }
    30  
    31  // Invoke invokes the [LogInstance.invokeOutput] function for a stored code location
    32  func (o *OutputInvoker) Invoke(format string, a ...any) {
    33  	o.invokeOutput(
    34  		pruntime.AppendLocation(
    35  			Sprintf(format, a...), // collapse format and a to single string
    36  			o.codeLocation,
    37  		))
    38  }