| 170 | |
| 171 | public: |
| 172 | void add(enum ggml_log_level level, const char * fmt, va_list args) { |
| 173 | std::lock_guard<std::mutex> lock(mtx); |
| 174 | |
| 175 | if (!running) { |
| 176 | // discard messages while the worker thread is paused |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | auto & entry = entries[tail]; |
| 181 | |
| 182 | { |
| 183 | // cannot use args twice, so make a copy in case we need to expand the buffer |
| 184 | va_list args_copy; |
| 185 | va_copy(args_copy, args); |
| 186 | |
| 187 | #if 1 |
| 188 | const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args); |
| 189 | if (n >= entry.msg.size()) { |
| 190 | entry.msg.resize(n + 1); |
| 191 | vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args_copy); |
| 192 | } |
| 193 | #else |
| 194 | // hack for bolding arguments |
| 195 | |
| 196 | std::stringstream ss; |
| 197 | for (int i = 0; fmt[i] != 0; i++) { |
| 198 | if (fmt[i] == '%') { |
| 199 | ss << LOG_COL_BOLD; |
| 200 | while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++]; |
| 201 | ss << LOG_COL_DEFAULT; |
| 202 | if (fmt[i] == 0) break; |
| 203 | } |
| 204 | ss << fmt[i]; |
| 205 | } |
| 206 | const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args); |
| 207 | if (n >= entry.msg.size()) { |
| 208 | entry.msg.resize(n + 1); |
| 209 | vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args_copy); |
| 210 | } |
| 211 | #endif |
| 212 | va_end(args_copy); |
| 213 | } |
| 214 | |
| 215 | entry.level = level; |
| 216 | entry.prefix = prefix; |
| 217 | entry.timestamp = 0; |
| 218 | if (timestamps) { |
| 219 | entry.timestamp = t_us() - t_start; |
| 220 | } |
| 221 | entry.is_end = false; |
| 222 | |
| 223 | tail = (tail + 1) % entries.size(); |
| 224 | if (tail == head) { |
| 225 | // expand the buffer |
| 226 | std::vector<common_log_entry> new_entries(2*entries.size()); |
| 227 | |
| 228 | size_t new_tail = 0; |
| 229 | |