<commit_before>// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package debugtracer

import (
	"fmt"
	"io"
	"path/filepath"
	"testing"

	"github.com/google/syzkaller/pkg/osutil"
)

type DebugTracer interface {
	Log(msg string, args ...interface{})
	SaveFile(filename string, data []byte)
}

type GenericTracer struct {
	TraceWriter io.Writer
	OutDir      string
}

type TestTracer struct {
	T *testing.T
}

type NullTracer struct {
}

func (gt *GenericTracer) Log(msg string, args ...interface{}) {
	fmt.Fprintf(gt.TraceWriter, msg+"\n", args...)
}

func (gt *GenericTracer) SaveFile(filename string, data []byte) {
	if gt.OutDir == "" {
		return
	}
	osutil.MkdirAll(gt.OutDir)
	osutil.WriteFile(filepath.Join(gt.OutDir, filename), data)
}

func (tt *TestTracer) Log(msg string, args ...interface{}) {
	tt.T.Log(msg, args)<commit_msg>pkg/debugtracer: add the ability to capture time in logs<commit_after>// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package debugtracer

import (
	"fmt"
	"io"
	"path/filepath"
	"testing"
	"time"

	"github.com/google/syzkaller/pkg/osutil"
)

type DebugTracer interface {
	Log(msg string, args ...interface{})
	SaveFile(filename string, data []byte)
}

type GenericTracer struct {
	WithTime    bool
	TraceWriter io.Writer
	OutDir      string
}

type TestTracer struct {
	T *testing.T
}

type NullTracer struct {
}

func (gt *GenericTracer) Log(msg string, args ...interface{}) {
	if gt.WithTime {
		timeStr := time.Now().Format("02-Jan-2006 15:04:05")
		newArgs := append([]interface{}{timeStr}, args...)
		fmt.Fprintf(gt.TraceWriter, "%s: "+msg+"\n", newArgs...)
	} else {
		fmt.Fprintf(gt.TraceWriter, msg+"\n", args...)
	}
}

func (gt *GenericTracer) SaveFile(filename string, data []byte) {
	if gt.OutDir == "" {
		return
	}
	osutil.MkdirAll(gt.OutDir)
	osutil.WriteFile(filepath.Join(gt.OutDir, filename), data)
}

func (tt *TestTracer) Log(msg string, args ...interface{}) {
	tt.T.Log(msg, args)
{'fraction_non_alphanumeric': 0.14068265682656828, 'fraction_numerical': 0.01014760147601476, 'mean_word_length': 4.0324825986078885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5415248', 'n_tokens_mistral': 786, 'n_tokens_neox': 762, 'n_words': 213}