| 556 | } |
| 557 | |
| 558 | static void handle_logs(cbm_http_conn_t *c, const cbm_http_req_t *req) { |
| 559 | char lines_str[16] = {0}; |
| 560 | int max_lines = 100; |
| 561 | if (cbm_http_query_param(req->query, "lines", lines_str, (int)sizeof(lines_str))) { |
| 562 | int v = atoi(lines_str); |
| 563 | if (v > 0 && v <= LOG_RING_SIZE) |
| 564 | max_lines = v; |
| 565 | } |
| 566 | |
| 567 | char after_str[32] = {0}, severity[16] = {0}; |
| 568 | int64_t after = -1; |
| 569 | if (cbm_http_query_param(req->query, "after", after_str, sizeof(after_str))) |
| 570 | after = strtoll(after_str, NULL, 10); |
| 571 | int min_level = 0; |
| 572 | if (cbm_http_query_param(req->query, "min_level", severity, sizeof(severity)) || |
| 573 | cbm_http_query_param(req->query, "level", severity, sizeof(severity))) { |
| 574 | if (!strcmp(severity, "error")) |
| 575 | min_level = 3; |
| 576 | else if (!strcmp(severity, "warn")) |
| 577 | min_level = 2; |
| 578 | else if (!strcmp(severity, "info")) |
| 579 | min_level = 1; |
| 580 | } |
| 581 | char project[256] = {0}, scope[32] = {0}, query[257] = {0}; |
| 582 | bool has_query = cbm_http_query_param(req->query, "q", query, sizeof(query)); |
| 583 | bool has_project = cbm_http_query_param(req->query, "project", project, sizeof(project)); |
| 584 | bool has_scope = cbm_http_query_param(req->query, "scope", scope, sizeof(scope)); |
| 585 | bool unattributed = has_scope && !strcmp(scope, "unattributed"); |
| 586 | if ((log_query_has_key(req->query, "q") && !has_query) || |
| 587 | (log_query_has_key(req->query, "project") && !has_project) || |
| 588 | (log_query_has_key(req->query, "scope") && !has_scope) || |
| 589 | (has_scope && strcmp(scope, "daemon") && !unattributed) || (has_project && has_scope)) { |
| 590 | cbm_http_replyf(c, 400, g_cors_json, |
| 591 | "{\"error\":\"choose an exact project or unattributed scope\"}"); |
| 592 | return; |
| 593 | } |
| 594 | cbm_ui_log_init(); |
| 595 | cbm_mutex_lock(&g_log_mutex); |
| 596 | char *persistent = |
| 597 | cbm_activity_logs_filtered(activity_db_locked(), after, max_lines, min_level, |
| 598 | has_project ? project : NULL, unattributed, query); |
| 599 | cbm_mutex_unlock(&g_log_mutex); |
| 600 | if (persistent) { |
| 601 | cbm_http_replyf(c, 200, g_cors_json, "%s", persistent); |
| 602 | cbm_free(CBM_MEM_CLASS_OTHER, persistent); |
| 603 | return; |
| 604 | } |
| 605 | /* The legacy memory ring has no trustworthy project provenance. Never |
| 606 | * substitute daemon-wide rows for a failed project-scoped SQLite read. */ |
| 607 | if (has_project || unattributed || has_query || min_level > 0) { |
| 608 | cbm_http_replyf(c, 503, g_cors_json, |
| 609 | "{\"error\":\"scoped log storage unavailable\",\"persistent\":false}"); |
| 610 | return; |
| 611 | } |
| 612 | /* Keep the established endpoint usable if local storage is unavailable. |
| 613 | * The frontend sees persistent=false and must report the limitation. */ |
| 614 | cbm_mutex_lock(&g_log_mutex); |
| 615 | int count = g_log_count < max_lines ? g_log_count : max_lines; |
no test coverage detected