MCPcopy
hub / github.com/kopia/kopia / TempLogDirectory

Function TempLogDirectory

internal/testutil/tmpdir.go:102–132  ·  view source on GitHub ↗

TempLogDirectory returns a temporary directory used for storing logs. If KOPIA_LOGS_DIR is provided.

(tb testing.TB)

Source from the content-addressed store, hash-verified

100// TempLogDirectory returns a temporary directory used for storing logs.
101// If KOPIA_LOGS_DIR is provided.
102func TempLogDirectory(tb testing.TB) string {
103 tb.Helper()
104
105 cleanName := strings.NewReplacer("/", "_", "\\", "_", ":", "_").Replace(tb.Name())
106
107 tb.Helper()
108
109 logsBaseDir := os.Getenv("KOPIA_LOGS_DIR")
110 if logsBaseDir == "" {
111 logsBaseDir = filepath.Join(os.TempDir(), "kopia-logs")
112 }
113
114 logsDir := filepath.Join(logsBaseDir, cleanName+"."+clock.Now().Local().Format("20060102150405"))
115
116 require.NoError(tb, os.MkdirAll(logsDir, logsDirPermissions))
117
118 tb.Cleanup(func() {
119 if getEnvVarBool("KOPIA_KEEP_LOGS") {
120 tb.Logf("logs preserved in %v", logsDir)
121 return
122 }
123
124 if tb.Failed() && !getEnvVarBool("KOPIA_DISABLE_LOG_DUMP_ON_FAILURE") {
125 dumpLogs(tb, logsDir)
126 }
127
128 os.RemoveAll(logsDir) //nolint:errcheck
129 })
130
131 return logsDir
132}
133
134func dumpLogs(tb testing.TB, dirname string) {
135 tb.Helper()

Callers 5

runStressFunction · 0.92
NewExeRunnerWithBinaryFunction · 0.92

Calls 11

NowFunction · 0.92
getEnvVarBoolFunction · 0.85
dumpLogsFunction · 0.85
HelperMethod · 0.80
ReplaceMethod · 0.80
LogfMethod · 0.80
NameMethod · 0.65
FormatMethod · 0.65
MkdirAllMethod · 0.65
CleanupMethod · 0.65
RemoveAllMethod · 0.45

Tested by 4

runStressFunction · 0.74