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