Skip to content

Commit

Permalink
core: notify plugins when a log line is emitted.
Browse files Browse the repository at this point in the history
Currently make a plugin that do reportings of logs on
a services like graphana is not possible. So this commit
include the possibility to write a plugin that do the report
of this analisys.

Changelog-Added: core: notify plugins when a log line is emitted.
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jan 11, 2024
1 parent 000fceb commit add6e60
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
8 changes: 8 additions & 0 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,13 @@ static void maybe_print(struct logger *log, const struct log_entry *l)
log->log_book->log_files);
}

static void maybe_notify_log(struct logger *log,
const struct log_entry *l)
{
if (l->level >= log->print_level)
notify_log(log->log_book->ld, l);
}

void logv(struct logger *log, enum log_level level,
const struct node_id *node_id,
bool call_notifier,
Expand All @@ -561,6 +568,7 @@ void logv(struct logger *log, enum log_level level,
l->log[i] = '?';

maybe_print(log, l);
maybe_notify_log(log, l);

add_entry(log, &l);

Expand Down
45 changes: 33 additions & 12 deletions lightningd/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <common/type_to_string.h>
#include <lightningd/channel.h>
#include <lightningd/coin_mvts.h>
#include <lightningd/log.h>
#include <lightningd/notification.h>

static struct notification *find_notification_by_topic(const char* topic)
Expand Down Expand Up @@ -122,29 +123,35 @@ void notify_disconnect(struct lightningd *ld, struct node_id *nodeid)

/*'warning' is based on LOG_UNUSUAL/LOG_BROKEN level log
*(in plugin module, they're 'warn'/'error' level). */
static void warning_notification_serialize(struct json_stream *stream,
static void log_notification_serialize(struct json_stream *stream,
struct log_entry *l)
{
json_object_start(stream, "warning");
/* Choose "BROKEN"/"UNUSUAL" to keep consistent with the habit
* of plugin. But this may confuses the users who want to 'getlog'
* with the level indicated by notifications. It is the duty of a
* plugin to eliminate this misunderstanding.
*/
json_add_string(stream, "level",
l->level == LOG_BROKEN ? "error"
: "warn");
if (l->level >= LOG_UNUSUAL)
json_object_start(stream, "warning");
else
json_object_start(stream, NULL);
if (l->level >= LOG_UNUSUAL)
/* Choose "BROKEN"/"UNUSUAL" to keep consistent with the habit
* of plugin. But this may confuses the users who want to 'getlog'
* with the level indicated by notifications. It is the duty of a
* plugin to eliminate this misunderstanding.
*/
json_add_string(stream, "level",
l->level == LOG_BROKEN ? "error"
: "warn");
else
json_add_string(stream, "level", log_level_name(l->level));
/* unsuaul/broken event is rare, plugin pay more attentions on
* the absolute time, like when channels failed. */
json_add_timestr(stream, "time", l->time.ts);
json_add_timeiso(stream, "timestamp", l->time);
json_add_string(stream, "source", l->prefix->prefix);
json_add_string(stream, "log", l->log);
json_object_end(stream); /* .warning */
json_object_end(stream);
}

REGISTER_NOTIFICATION(warning,
warning_notification_serialize);
log_notification_serialize);

void notify_warning(struct lightningd *ld, struct log_entry *l)
{
Expand Down Expand Up @@ -746,3 +753,17 @@ bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p)
jsonrpc_notification_end(n);
return plugin_single_notify(p, take(n));
}

REGISTER_NOTIFICATION(log, log_notification_serialize);

void notify_log(struct lightningd *ld, const struct log_entry *l)
{
void (*serialize)(struct json_stream *,
const struct log_entry *) = log_notification_gen.serialize;

struct jsonrpc_notification *n =
jsonrpc_notification_start(NULL, log_notification_gen.topic);
serialize(n->stream, l);
jsonrpc_notification_end(n);
plugins_notify(ld->plugins, take(n));
}
4 changes: 4 additions & 0 deletions lightningd/notification.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef LIGHTNING_LIGHTNINGD_NOTIFICATION_H
#define LIGHTNING_LIGHTNINGD_NOTIFICATION_H
#include "config.h"
#include "lightningd/lightningd.h"
#include "lightningd/log.h"
#include <common/coin_mvt.h>
#include <lightningd/chaintopology.h>
#include <lightningd/pay.h>
Expand Down Expand Up @@ -106,4 +108,6 @@ void notify_channel_open_failed(struct lightningd *ld,

/* Tell this plugin to shutdown: returns true if it was subscribed. */
bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p);
/* Inform the plugin when a log line is emitted */
void notify_log(struct lightningd *ld, const struct log_entry *l);
#endif /* LIGHTNING_LIGHTNINGD_NOTIFICATION_H */
9 changes: 0 additions & 9 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,10 @@ bool feature_negotiated(const struct feature_set *our_features UNNEEDED,
/* Generated stub for featurebits_or */
u8 *featurebits_or(const tal_t *ctx UNNEEDED, const u8 *f1 TAKES UNNEEDED, const u8 *f2 TAKES UNNEEDED)
{ fprintf(stderr, "featurebits_or called!\n"); abort(); }
/* Generated stub for find_channel_by_alias */
struct channel *find_channel_by_alias(const struct peer *peer UNNEEDED,
const struct short_channel_id *alias UNNEEDED,
enum side side UNNEEDED)
{ fprintf(stderr, "find_channel_by_alias called!\n"); abort(); }
/* Generated stub for find_channel_by_id */
struct channel *find_channel_by_id(const struct peer *peer UNNEEDED,
const struct channel_id *cid UNNEEDED)
{ fprintf(stderr, "find_channel_by_id called!\n"); abort(); }
/* Generated stub for find_channel_by_scid */
struct channel *find_channel_by_scid(const struct peer *peer UNNEEDED,
const struct short_channel_id *scid UNNEEDED)
{ fprintf(stderr, "find_channel_by_scid called!\n"); abort(); }
/* Generated stub for find_plugin_for_command */
struct plugin *find_plugin_for_command(struct lightningd *ld UNNEEDED,
const char *cmd_name UNNEEDED)
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-log-pruning.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ bool log_level_parse(const char *levelstr UNNEEDED, size_t len UNNEEDED,
/* Generated stub for node_id_to_hexstr */
char *node_id_to_hexstr(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "node_id_to_hexstr called!\n"); abort(); }
/* Generated stub for notify_log */
void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEEDED)
{ fprintf(stderr, "notify_log called!\n"); abort(); }
/* Generated stub for notify_warning */
void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED)
{ fprintf(stderr, "notify_warning called!\n"); abort(); }
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-log_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ void json_stream_log_suppress_for_cmd(struct json_stream *js UNNEEDED,
/* Generated stub for json_stream_success */
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
/* Generated stub for notify_log */
void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEEDED)
{ fprintf(stderr, "notify_log called!\n"); abort(); }
/* Generated stub for notify_warning */
void notify_warning(struct lightningd *ld UNNEEDED, struct log_entry *l UNNEEDED)
{ fprintf(stderr, "notify_warning called!\n"); abort(); }
Expand Down

0 comments on commit add6e60

Please sign in to comment.