Compatibility endpoint: original JSON lines, now from the authoritative * journal. Bound both rows and bytes; export-file rotation cannot change them. */
| 292 | /* Compatibility endpoint: original JSON lines, now from the authoritative |
| 293 | * journal. Bound both rows and bytes; export-file rotation cannot change them. */ |
| 294 | char *cbm_activity_ui_logs(sqlite3 *db, int limit, const char *path, const char *export_path, |
| 295 | const char *previous_path) { |
| 296 | if (!db) |
| 297 | return NULL; |
| 298 | if (limit <= 0 || limit > 1000) |
| 299 | limit = 200; |
| 300 | sqlite3_stmt *st = NULL; |
| 301 | if (sqlite3_exec(db, "SAVEPOINT ui_tail", NULL, NULL, NULL) != SQLITE_OK) |
| 302 | return NULL; |
| 303 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 304 | yyjson_mut_val *root = yyjson_mut_obj(doc), *lines = yyjson_mut_arr(doc); |
| 305 | yyjson_mut_doc_set_root(doc, root); |
| 306 | int rc = sqlite3_prepare_v2( |
| 307 | db, |
| 308 | "SELECT COUNT(*),coalesce(SUM(length(CAST(message AS BLOB))),0) FROM daemon_logs " |
| 309 | "WHERE origin='frontend'", |
| 310 | -1, &st, NULL); |
| 311 | if (rc != SQLITE_OK || sqlite3_step(st) != SQLITE_ROW) |
| 312 | goto fail; |
| 313 | int64_t total = sqlite3_column_int64(st, 0), bytes = sqlite3_column_int64(st, 1); |
| 314 | sqlite3_finalize(st); |
| 315 | st = NULL; |
| 316 | bool historical_partial = false; |
| 317 | rc = sqlite3_prepare_v2(db, "SELECT value FROM activity_meta WHERE key='ui_legacy_import'", -1, |
| 318 | &st, NULL); |
| 319 | if (rc != SQLITE_OK) |
| 320 | goto fail; |
| 321 | rc = sqlite3_step(st); |
| 322 | if (rc == SQLITE_ROW) |
| 323 | historical_partial = !strcmp((const char *)sqlite3_column_text(st, 0), "bounded"); |
| 324 | else if (rc != SQLITE_DONE) |
| 325 | goto fail; |
| 326 | sqlite3_finalize(st); |
| 327 | st = NULL; |
| 328 | rc = sqlite3_prepare_v2( |
| 329 | db, "SELECT message FROM daemon_logs WHERE origin='frontend' ORDER BY id DESC LIMIT ?1", -1, |
| 330 | &st, NULL); |
| 331 | if (rc != SQLITE_OK) |
| 332 | goto fail; |
| 333 | sqlite3_bind_int(st, 1, limit); |
| 334 | yyjson_mut_val *rows[1000]; |
| 335 | int count = 0; |
| 336 | size_t emitted_bytes = 0; |
| 337 | while ((rc = sqlite3_step(st)) == SQLITE_ROW) { |
| 338 | const char *message = (const char *)sqlite3_column_text(st, 0); |
| 339 | size_t length = (size_t)sqlite3_column_bytes(st, 0); |
| 340 | if (emitted_bytes + length > 256 * 1024) |
| 341 | break; |
| 342 | while (length && (message[length - 1] == '\n' || message[length - 1] == '\r')) |
| 343 | length--; |
| 344 | rows[count++] = yyjson_mut_strncpy(doc, message, length); |
| 345 | emitted_bytes += length; |
| 346 | } |
| 347 | if (rc != SQLITE_ROW && rc != SQLITE_DONE) |
| 348 | goto fail; |
| 349 | for (int i = count - 1; i >= 0; i--) |
| 350 | yyjson_mut_arr_append(lines, rows[i]); |
| 351 | sqlite3_finalize(st); |
no test coverage detected