aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/chrometracing/chrometracing_close.go
blob: 1b3a7b949a6b32da82c4e99ea69e419ef2d83d15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package chrometracing

// Close overwrites the trailing (,\n) with (]\n) and closes the trace file.
// Close is implemented in a separate file to keep a separation between custom
// code and upstream from github.com/google/chrometracing. Additionally, we can
// enable linting for code we author, while leaving upstream code alone.
func Close() error {
	trace.fileMu.Lock()
	defer trace.fileMu.Unlock()
	// Seek backwards two bytes (,\n)
	if _, err := trace.file.Seek(-2, 1); err != nil {
		return err
	}
	// Write 1 byte, ']', leaving the trailing '\n' in place
	if _, err := trace.file.Write([]byte{']'}); err != nil {
		return err
	}
	// Force the filesystem to write to disk
	if err := trace.file.Sync(); err != nil {
		return err
	}
	if err := trace.file.Close(); err != nil {
		return err
	}
	return nil
}