-
Notifications
You must be signed in to change notification settings - Fork 5
/
logging.go
168 lines (149 loc) · 4.2 KB
/
logging.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package imgui
import (
"fmt"
"os"
)
// LogBegin Logging/Capture
// . BeginCapture() when we design v2 api, for now stay under the radar by using the old name.
func LogBegin(ltype ImGuiLogType, auto_open_depth int) {
var g = GImGui
var window = g.CurrentWindow
IM_ASSERT(!g.LogEnabled)
IM_ASSERT(g.LogFile == nil)
IM_ASSERT(g.LogBuffer.Len() == 0)
g.LogEnabled = true
g.LogType = ltype
g.LogNextPrefix = ""
g.LogNextSuffix = ""
g.LogDepthRef = window.DC.TreeDepth
g.LogDepthToExpand = g.LogDepthToExpandDefault
if auto_open_depth >= 0 {
g.LogDepthToExpand = auto_open_depth
}
g.LogLinePosY = FLT_MAX
g.LogLineFirstItem = true
}
// LogToBuffer Start logging/capturing to internal buffer
func LogToBuffer(auto_open_depth int /*= -1*/) {
var g = GImGui
if g.LogEnabled {
return
}
LogBegin(ImGuiLogType_Buffer, auto_open_depth)
}
// LogRenderedText Internal version that takes a position to decide on newline placement and pad items according to their depth.
// We split text into individual lines to add current tree level padding
// FIXME: This code is a little complicated perhaps, considering simplifying the whole system.
func LogRenderedText(ref_pos *ImVec2, text string) {
LogText(text)
}
// LogSetNextTextDecoration Important: doesn't copy underlying data, use carefully (prefix/suffix must be in scope at the time of the next LogRenderedText)
func LogSetNextTextDecoration(prefix string, suffix string) {
var g = GImGui
g.LogNextPrefix = prefix
g.LogNextSuffix = suffix
}
// Logging/Capture
// - All text output from the interface can be captured into tty/file/clipboard. By default, tree nodes are automatically opened during logging.
// LogToTTY start logging to tty (stdout)
func LogToTTY(auto_open_depth int /*= -1*/) {
var g = GImGui
if g.LogEnabled {
return
}
LogBegin(ImGuiLogType_TTY, auto_open_depth)
g.LogFile = os.Stdout
}
// LogToFile Start logging/capturing text output to given file
func LogToFile(auto_open_depth int /*= 1*/, filename string) {
var g = GImGui
if g.LogEnabled {
return
}
// FIXME: We could probably open the file in text mode "at", however note that clipboard/buffer logging will still
// be subject to outputting OS-incompatible carriage return if within strings the user doesn't use IM_NEWLINE.
// By opening the file in binary mode "ab" we have consistent output everywhere.
if filename == "" {
filename = g.IO.LogFilename
}
if filename == "" || filename[0] == 0 {
return
}
var f = ImFileOpen(filename, "ab")
if f == nil {
IM_ASSERT(false)
return
}
LogBegin(ImGuiLogType_File, auto_open_depth)
g.LogFile = f
}
// LogToClipboard start logging to OS clipboard
func LogToClipboard(auto_open_depth int /*= -1*/) {
var g = GImGui
if g.LogEnabled {
return
}
LogBegin(ImGuiLogType_Clipboard, auto_open_depth)
}
func LogFinish() {
var g = GImGui
if !g.LogEnabled {
return
}
LogText(IM_NEWLINE)
switch g.LogType {
case ImGuiLogType_TTY:
//g.LogFile
case ImGuiLogType_File:
ImFileClose(g.LogFile)
case ImGuiLogType_Buffer:
case ImGuiLogType_Clipboard:
if g.LogBuffer.Len() > 0 {
SetClipboardText(g.LogBuffer.String())
}
case ImGuiLogType_None:
IM_ASSERT(false)
}
g.LogEnabled = false
g.LogType = ImGuiLogType_None
g.LogFile = nil
g.LogBuffer.Reset()
} // stop logging (close file, etc.)
// LogButtons helper to display buttons for logging to tty/file/clipboard
func LogButtons() {
var g = GImGui
PushString("LogButtons")
var log_to_tty = Button("Log To TTY")
SameLine(0, 0)
var log_to_file = Button("Log To File")
SameLine(0, 0)
var log_to_clipboard = Button("Log To Clipboard")
SameLine(0, 0)
PushAllowKeyboardFocus(false)
SetNextItemWidth(80.0)
SliderInt("Default Depth", &g.LogDepthToExpandDefault, 0, 9, "", 0)
PopAllowKeyboardFocus()
PopID()
// Start logging at the end of the function so that the buttons don't appear in the log
if log_to_tty {
LogToTTY(-1)
}
if log_to_file {
LogToFile(-1, "")
}
if log_to_clipboard {
LogToClipboard(-1)
}
}
// LogText pass text data straight to log (without being displayed)
func LogText(format string, args ...any) {
var g = GImGui
if !g.LogEnabled {
return
}
if g.LogFile != nil {
fmt.Fprintf(g.LogFile, format, args...)
} else {
fmt.Fprintf(&g.LogBuffer, format, args...)
}
}