gitstack

grindlemire/go-tui code browser

1.5 KB Go 77 lines 2026-01-31 · 3130a1f raw
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package log provides centralized logging for the LSP server.
package log

import (
	"fmt"
	"os"
	"sync"
)

var (
	file *os.File
	mu   sync.Mutex
)

// SetOutput sets the log output file. Pass nil to disable logging.
func SetOutput(f *os.File) {
	mu.Lock()
	defer mu.Unlock()
	file = f
}

// Debug writes a debug log message if logging is enabled.
func Debug(format string, args ...any) {
	mu.Lock()
	defer mu.Unlock()
	if file != nil {
		fmt.Fprintf(file, format+"\n", args...)
	}
}

// Debugf is an alias for Debug.
func Debugf(format string, args ...any) {
	Debug(format, args...)
}

// Server writes a server-prefixed log message.
func Server(format string, args ...any) {
	mu.Lock()
	defer mu.Unlock()
	if file != nil {
		fmt.Fprintf(file, "[server] "+format+"\n", args...)
	}
}

// Gopls writes a gopls-prefixed log message.
func Gopls(format string, args ...any) {
	mu.Lock()
	defer mu.Unlock()
	if file != nil {
		fmt.Fprintf(file, "[gopls] "+format+"\n", args...)
	}
}

// Generate writes a generate-prefixed log message.
func Generate(format string, args ...any) {
	mu.Lock()
	defer mu.Unlock()
	if file != nil {
		fmt.Fprintf(file, "[generate] "+format+"\n", args...)
	}
}

// Mapping writes a mapping-prefixed log message.
func Mapping(format string, args ...any) {
	mu.Lock()
	defer mu.Unlock()
	if file != nil {
		fmt.Fprintf(file, "[mapping] "+format+"\n", args...)
	}
}

// Enabled returns true if logging is enabled.
func Enabled() bool {
	mu.Lock()
	defer mu.Unlock()
	return file != nil
}