MCPcopy Create free account
hub / github.com/ElementsProject/lightning / plugin_log_handle

Function plugin_log_handle

lightningd/plugin.c:478–535  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

476 const jsmntok_t *paramstok)
477 WARN_UNUSED_RESULT;
478static const char *plugin_log_handle(struct plugin *plugin,
479 const char *buffer,
480 const jsmntok_t *paramstok)
481{
482 const jsmntok_t *msgtok, *leveltok;
483 enum log_level level;
484 bool call_notifier;
485 msgtok = json_get_member(buffer, paramstok, "message");
486 leveltok = json_get_member(buffer, paramstok, "level");
487
488 if (!msgtok || msgtok->type != JSMN_STRING) {
489 return tal_fmt(plugin, "Log notification from plugin doesn't have "
490 "a string \"message\" field");
491 }
492
493 if (!leveltok)
494 level = LOG_INFORM;
495 else if (!log_level_parse(buffer + leveltok->start,
496 leveltok->end - leveltok->start,
497 &level)
498 /* FIXME: Allow io logging? */
499 || level == LOG_IO_IN
500 || level == LOG_IO_OUT) {
501 return tal_fmt(plugin,
502 "Unknown log-level %.*s, valid values are "
503 "\"trace\", \"debug\", \"info\", \"warn\", or \"error\".",
504 json_tok_full_len(leveltok),
505 json_tok_full(buffer, leveltok));
506 }
507
508 call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false;
509
510 /* Only bother unescaping and splitting if it has \ */
511 if (memchr(buffer + msgtok->start, '\\', msgtok->end - msgtok->start)) {
512 const char *log_msg = json_escape_unescape_len(tmpctx,
513 buffer + msgtok->start,
514 msgtok->end - msgtok->start);
515 char **lines;
516
517 /* Weird \ escapes aren't handled by json_escape_unescape. This is for you, clboss! */
518 if (!log_msg)
519 goto print_raw;
520
521 lines = tal_strsplit(tmpctx, log_msg, "\n", STR_EMPTY_OK);
522
523 for (size_t i = 0; lines[i]; i++) {
524 /* FIXME: Let plugin specify node_id? */
525 log_(plugin->log, level, NULL, call_notifier, "%s", lines[i]);
526 }
527 } else {
528 print_raw:
529 log_(plugin->log, level, NULL, call_notifier, "%.*s",
530 msgtok->end - msgtok->start,
531 buffer + msgtok->start);
532 }
533
534 return NULL;
535}

Callers 1

Calls 6

json_get_memberFunction · 0.85
log_level_parseFunction · 0.85
json_escape_unescape_lenFunction · 0.85
log_Function · 0.70
json_tok_full_lenFunction · 0.50
json_tok_fullFunction · 0.50

Tested by

no test coverage detected