Skip to content

Commit 32af429

Browse files
authored
fix: dont affect original go build command (#224)
* fix: dont affect original go build command * update readme and usage
1 parent fec9612 commit 32af429

21 files changed

+265
-225
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The configuration for the tool can be set by the following command:
4444

4545
```bash
4646
$ otel set -verbose # print verbose logs
47+
$ otel set -log=/path/to/file.log # set log file
4748
$ otel set -debug # enable debug mode
4849
$ otel set -debug -verbose -rule=custom.json # set multiple configs
4950
$ otel set -disabledefault -rule=custom.json # disable default rules, use custom rules only

docs/usage.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ This guide provides a detailed overview of configuring and using the otel tool e
66
## Configuration
77
The primary method of configuring the tool is through the `otel set` command. This command allows you to specify various settings tailored to your needs:
88

9+
Logging: Set a custom log file to store logs generated by the tool.
10+
```bash
11+
$ otel set -log=/path/to/file.log
12+
```
13+
The default log file is `.otel-build/preprocess/debug.log`. You can
14+
log to stdout by setting `-log=/dev/stdout`.
15+
916
Verbose Logging: Enable verbose logging to receive detailed output from the tool, which is helpful for troubleshooting and understanding the tool's processes.
1017
```bash
1118
$ otel set -verbose

test/flags_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestFlags(t *testing.T) {
4444
RunGoBuild(t, "")
4545
}
4646

47-
func TestFlagConfigOverwriteNo(t *testing.T) {
47+
func TestFlagEnvOverwrite(t *testing.T) {
4848
UseApp(AppName)
4949

5050
RunSet(t, "-verbose=false")

test/infra.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func runCmd(args []string) *exec.Cmd {
5555
}
5656

5757
func ReadInstrumentLog(t *testing.T, fileName string) string {
58-
path := filepath.Join(shared.TempBuildDir, shared.PInstrument, fileName)
58+
path := filepath.Join(shared.TempBuildDir, util.PInstrument, fileName)
5959
content, err := util.ReadFile(path)
6060
if err != nil {
6161
t.Fatal(err)
@@ -64,7 +64,7 @@ func ReadInstrumentLog(t *testing.T, fileName string) string {
6464
}
6565

6666
func ReadPreprocessLog(t *testing.T, fileName string) string {
67-
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, fileName)
67+
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, fileName)
6868
content, err := util.ReadFile(path)
6969
if err != nil {
7070
t.Fatal(err)
@@ -102,7 +102,6 @@ func RunSet(t *testing.T, args ...string) {
102102

103103
func RunGoBuild(t *testing.T, args ...string) {
104104
util.Assert(pwd != "", "pwd is empty")
105-
RunSet(t, "-debuglog")
106105
path := filepath.Join(filepath.Dir(pwd), getExecName())
107106
cmd := runCmd(append([]string{path}, args...))
108107
err := cmd.Run()
@@ -113,17 +112,14 @@ func RunGoBuild(t *testing.T, args ...string) {
113112
t.Log("\n\n\n")
114113
t.Log(stderr)
115114
log1 := ReadPreprocessLog(t, shared.DebugLogFile)
116-
log2 := ReadInstrumentLog(t, shared.DebugLogFile)
117115
text := fmt.Sprintf("failed to run instrument: %v\n", err)
118-
text += fmt.Sprintf("preprocess: %v\n", log1)
119-
text += fmt.Sprintf("instrument: %v\n", log2)
116+
text += fmt.Sprintf("text: %v\n", log1)
120117
t.Fatal(text)
121118
}
122119
}
123120

124121
func RunGoBuildWithEnv(t *testing.T, envs []string, args ...string) {
125122
util.Assert(pwd != "", "pwd is empty")
126-
RunSet(t, "-debuglog")
127123
path := filepath.Join(filepath.Dir(pwd), getExecName())
128124
cmd := runCmd(append([]string{path}, args...))
129125
cmd.Env = append(cmd.Env, envs...)
@@ -135,17 +131,14 @@ func RunGoBuildWithEnv(t *testing.T, envs []string, args ...string) {
135131
t.Log("\n\n\n")
136132
t.Log(stderr)
137133
log1 := ReadPreprocessLog(t, shared.DebugLogFile)
138-
log2 := ReadInstrumentLog(t, shared.DebugLogFile)
139134
text := fmt.Sprintf("failed to run instrument: %v\n", err)
140-
text += fmt.Sprintf("preprocess: %v\n", log1)
141-
text += fmt.Sprintf("instrument: %v\n", log2)
135+
text += fmt.Sprintf("text: %v\n", log1)
142136
t.Fatal(text)
143137
}
144138
}
145139

146140
func RunGoBuildFallible(t *testing.T, args ...string) {
147141
util.Assert(pwd != "", "pwd is empty")
148-
RunSet(t, "-debuglog")
149142
path := filepath.Join(filepath.Dir(pwd), getExecName())
150143
cmd := runCmd(append([]string{path}, args...))
151144
err := cmd.Run()
@@ -219,25 +212,25 @@ func ExpectStderrContains(t *testing.T, expect string) {
219212
}
220213

221214
func ExpectInstrumentContains(t *testing.T, log string, rule string) {
222-
path := filepath.Join(shared.TempBuildDir, shared.PInstrument, log)
215+
path := filepath.Join(shared.TempBuildDir, util.PInstrument, log)
223216
content := readLog(t, path)
224217
ExpectContains(t, content, rule)
225218
}
226219

227220
func ExpectInstrumentNotContains(t *testing.T, log string, rule string) {
228-
path := filepath.Join(shared.TempBuildDir, shared.PInstrument, log)
221+
path := filepath.Join(shared.TempBuildDir, util.PInstrument, log)
229222
content := readLog(t, path)
230223
ExpectNotContains(t, content, rule)
231224
}
232225

233226
func ExpectPreprocessContains(t *testing.T, log string, rule string) {
234-
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, log)
227+
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, log)
235228
content := readLog(t, path)
236229
ExpectContains(t, content, rule)
237230
}
238231

239232
func ExpectPreprocessNotContains(t *testing.T, log string, rule string) {
240-
path := filepath.Join(shared.TempBuildDir, shared.PPreprocess, log)
233+
path := filepath.Join(shared.TempBuildDir, util.PPreprocess, log)
241234
content := readLog(t, path)
242235
ExpectNotContains(t, content, rule)
243236
}

tool/cmd/main.go

+20-24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
import (
1818
"fmt"
19-
"log"
2019
"os"
2120
"path/filepath"
2221
"strings"
@@ -49,26 +48,25 @@ Command:
4948
`
5049

5150
func printUsage() {
52-
usage = strings.ReplaceAll(usage, "{}", config.GetToolName())
51+
usage = strings.ReplaceAll(usage, "{}", util.GetToolName())
5352
fmt.Print(usage)
5453
}
5554

56-
func initLogs(names ...string) error {
57-
for _, name := range names {
58-
path := shared.GetTempBuildDirWith(name)
59-
logPath := filepath.Join(path, shared.DebugLogFile)
60-
_, err := os.Create(logPath)
61-
if err != nil {
62-
return fmt.Errorf("failed to create log file: %w", err)
63-
}
55+
func initLog() error {
56+
name := util.PPreprocess
57+
path := shared.GetTempBuildDirWith(name)
58+
logPath := filepath.Join(path, shared.DebugLogFile)
59+
_, err := os.Create(logPath)
60+
if err != nil {
61+
return fmt.Errorf("failed to create log file: %w", err)
6462
}
6563
return nil
6664
}
6765

6866
func initTempDir() error {
6967
// All temp directories are prepared before, instrument phase should not
7068
// create any new directories.
71-
if shared.GetRunPhase() == shared.PInstrument {
69+
if util.GetRunPhase() == util.PInstrument {
7270
return nil
7371
}
7472

@@ -83,14 +81,14 @@ func initTempDir() error {
8381
// we always recreate the preprocess and instrument directories, but only
8482
// create the configure directory if it does not exist. This is because
8583
// the configure directory can be used across multiple runs.
86-
exist, _ := util.PathExists(shared.GetTempBuildDirWith(shared.PConfigure))
84+
exist, _ := util.PathExists(shared.GetTempBuildDirWith(util.PConfigure))
8785
if !exist {
88-
err := os.MkdirAll(shared.GetTempBuildDirWith(shared.PConfigure), 0777)
86+
err := os.MkdirAll(shared.GetTempBuildDirWith(util.PConfigure), 0777)
8987
if err != nil {
9088
return fmt.Errorf("failed to make log directory: %w", err)
9189
}
9290
}
93-
for _, subdir := range []string{shared.PPreprocess, shared.PInstrument} {
91+
for _, subdir := range []string{util.PPreprocess, util.PInstrument} {
9492
exist, _ = util.PathExists(shared.GetTempBuildDirWith(subdir))
9593
if exist {
9694
err := os.RemoveAll(shared.GetTempBuildDirWith(subdir))
@@ -114,35 +112,33 @@ func initEnv() error {
114112
switch {
115113
case os.Args[1] == SubcommandSet:
116114
// otel set?
117-
shared.SetRunPhase(shared.PConfigure)
115+
util.SetRunPhase(util.PConfigure)
118116
case strings.HasSuffix(os.Args[1], SubcommandGo):
119117
// otel go build?
120-
shared.SetRunPhase(shared.PPreprocess)
118+
util.SetRunPhase(util.PPreprocess)
121119
case os.Args[1] == SubcommandRemix:
122120
// otel remix?
123-
shared.SetRunPhase(shared.PInstrument)
121+
util.SetRunPhase(util.PInstrument)
124122
default:
125123
// do nothing
126124
}
127125

128-
log.SetPrefix("[" + shared.GetRunPhase().String() + "] ")
129-
130126
// Create temp build directory
131127
err := initTempDir()
132128
if err != nil {
133129
return fmt.Errorf("failed to init temp dir: %w", err)
134130
}
135131

136132
// Create log files under temp build directory
137-
if shared.InPreprocess() {
138-
err := initLogs(shared.PPreprocess, shared.PInstrument)
133+
if util.InPreprocess() {
134+
err := initLog()
139135
if err != nil {
140136
return fmt.Errorf("failed to init logs: %w", err)
141137
}
142138
}
143139

144140
// Prepare shared configuration
145-
if shared.InPreprocess() || shared.InInstrument() {
141+
if util.InPreprocess() || util.InInstrument() {
146142
err = config.InitConfig()
147143
if err != nil {
148144
return fmt.Errorf("failed to init config: %w", err)
@@ -159,7 +155,7 @@ func main() {
159155

160156
err := initEnv()
161157
if err != nil {
162-
log.Printf("failed to init env: %v", err)
158+
util.LogFatal("failed to init env: %v", err)
163159
os.Exit(1)
164160
}
165161

@@ -177,7 +173,7 @@ func main() {
177173
printUsage()
178174
}
179175
if err != nil {
180-
log.Printf("failed to run command %s: %v", subcmd, err)
176+
util.LogFatal("failed to run command %s: %v", subcmd, err)
181177
os.Exit(1)
182178
}
183179
}

0 commit comments

Comments
 (0)