github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/debugtracer/debug.go (about)

     1  // Copyright 2020 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package debugtracer
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"path/filepath"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/google/syzkaller/pkg/osutil"
    14  )
    15  
    16  type DebugTracer interface {
    17  	Log(msg string, args ...interface{})
    18  	SaveFile(filename string, data []byte)
    19  }
    20  
    21  type GenericTracer struct {
    22  	WithTime    bool
    23  	TraceWriter io.Writer
    24  	OutDir      string
    25  }
    26  
    27  type TestTracer struct {
    28  	T *testing.T
    29  }
    30  
    31  type NullTracer struct {
    32  }
    33  
    34  func (gt *GenericTracer) Log(msg string, args ...interface{}) {
    35  	if gt.WithTime {
    36  		timeStr := time.Now().Format("02-Jan-2006 15:04:05")
    37  		newArgs := append([]interface{}{timeStr}, args...)
    38  		fmt.Fprintf(gt.TraceWriter, "%s: "+msg+"\n", newArgs...)
    39  	} else {
    40  		fmt.Fprintf(gt.TraceWriter, msg+"\n", args...)
    41  	}
    42  }
    43  
    44  func (gt *GenericTracer) SaveFile(filename string, data []byte) {
    45  	if gt.OutDir == "" {
    46  		return
    47  	}
    48  	osutil.MkdirAll(gt.OutDir)
    49  	osutil.WriteFile(filepath.Join(gt.OutDir, filename), data)
    50  }
    51  
    52  func (tt *TestTracer) Log(msg string, args ...interface{}) {
    53  	tt.T.Log(msg, args)
    54  }
    55  
    56  func (tt *TestTracer) SaveFile(filename string, data []byte) {
    57  	// Not implemented.
    58  }
    59  
    60  func (nt *NullTracer) Log(msg string, args ...interface{}) {
    61  	// Not implemented.
    62  }
    63  
    64  func (nt *NullTracer) SaveFile(filename string, data []byte) {
    65  	// Not implemented.
    66  }