4
4
#include < cstdarg>
5
5
#include < cstdio>
6
6
#include < mutex>
7
+ #include < sstream>
7
8
#include < thread>
8
9
#include < vector>
9
10
@@ -100,6 +101,9 @@ struct gpt_log {
100
101
running = false ;
101
102
t_start = t_us ();
102
103
entries.resize (capacity);
104
+ for (auto & entry : entries) {
105
+ entry.msg .resize (256 );
106
+ }
103
107
head = 0 ;
104
108
tail = 0 ;
105
109
@@ -144,11 +148,15 @@ struct gpt_log {
144
148
auto & entry = entries[tail];
145
149
146
150
{
151
+ // cannot use args twice, so make a copy in case we need to expand the buffer
152
+ va_list args_copy;
153
+ va_copy (args_copy, args);
154
+
147
155
#if 1
148
156
const size_t n = vsnprintf (entry.msg .data (), entry.msg .size (), fmt, args);
149
157
if (n >= entry.msg .size ()) {
150
158
entry.msg .resize (n + 1 );
151
- vsnprintf (entry.msg .data (), entry.msg .size (), fmt, args );
159
+ vsnprintf (entry.msg .data (), entry.msg .size (), fmt, args_copy );
152
160
}
153
161
#else
154
162
// hack for bolding arguments
@@ -166,7 +174,7 @@ struct gpt_log {
166
174
const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
167
175
if (n >= entry.msg.size()) {
168
176
entry.msg.resize(n + 1);
169
- vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args );
177
+ vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args_copy );
170
178
}
171
179
#endif
172
180
}
@@ -195,6 +203,10 @@ struct gpt_log {
195
203
head = 0 ;
196
204
tail = new_tail;
197
205
206
+ for (size_t i = tail; i < new_entries.size (); i++) {
207
+ new_entries[i].msg .resize (256 );
208
+ }
209
+
198
210
entries = std::move (new_entries);
199
211
}
200
212
@@ -280,7 +292,7 @@ struct gpt_log {
280
292
};
281
293
282
294
struct gpt_log * gpt_log_init () {
283
- return new gpt_log{1024 };
295
+ return new gpt_log{256 };
284
296
}
285
297
286
298
struct gpt_log * gpt_log_main () {
0 commit comments