| 467 | } |
| 468 | |
| 469 | static bool handle_notify(const char *buf, jsmntok_t *toks, |
| 470 | enum log_level notification_level, |
| 471 | bool *last_was_progress) |
| 472 | { |
| 473 | const jsmntok_t *id, *method, *params; |
| 474 | |
| 475 | if (toks->type != JSMN_OBJECT) |
| 476 | return false; |
| 477 | |
| 478 | id = json_get_member(buf, toks, "id"); |
| 479 | if (id) |
| 480 | return false; |
| 481 | |
| 482 | method = json_get_member(buf, toks, "method"); |
| 483 | if (!method) |
| 484 | return false; |
| 485 | |
| 486 | params = json_get_member(buf, toks, "params"); |
| 487 | if (!params) |
| 488 | return false; |
| 489 | |
| 490 | /* Print nothing if --notifications=none */ |
| 491 | if (notification_level == LOG_LEVEL_MAX + 1) |
| 492 | return true; |
| 493 | |
| 494 | /* We try to be robust if malformed */ |
| 495 | if (json_tok_streq(buf, method, "message")) { |
| 496 | const jsmntok_t *message, *leveltok; |
| 497 | enum log_level level; |
| 498 | |
| 499 | leveltok = json_get_member(buf, params, "level"); |
| 500 | if (!leveltok |
| 501 | || !log_level_parse(buf + leveltok->start, |
| 502 | leveltok->end - leveltok->start, |
| 503 | &level) |
| 504 | || level < notification_level) |
| 505 | return true; |
| 506 | |
| 507 | if (*last_was_progress) |
| 508 | printf("\n"); |
| 509 | *last_was_progress = false; |
| 510 | message = json_get_member(buf, params, "message"); |
| 511 | if (!message) |
| 512 | return true; |
| 513 | |
| 514 | printf("# %.*s\n", |
| 515 | message->end - message->start, |
| 516 | buf + message->start); |
| 517 | } else if (json_tok_streq(buf, method, "progress")) { |
| 518 | const jsmntok_t *num, *total, *stage; |
| 519 | u32 n, tot; |
| 520 | char bar[60 + 1]; |
| 521 | char totstr[STR_MAX_CHARS(u32)]; |
| 522 | |
| 523 | num = json_get_member(buf, params, "num"); |
| 524 | total = json_get_member(buf, params, "total"); |
| 525 | if (!num || !total) |
| 526 | return true; |
no test coverage detected