Browse Source

plugin: Add new notification type: warning

This notification bases on `LOG_BROKEN` and `LOG_UNUSUAL` level log.

--Introduction

A notification for topic `warning` is sent every time a new `BROKEN`/
`UNUSUAL` level(in plugins, we use `error`/`warn`) log generated, which
 means an unusual/borken thing happens, such as channel failed,
message resolving failed...

```json
{
	"warning": {
	"level": "warn",
	"time": "1559743608.565342521",
	"source": "lightningd(17652): 0821f80652fb840239df8dc99205792bba2e559a05469915804c08420230e23c7c chan #7854:",
	"log": "Peer permanent failure in CHANNELD_NORMAL: lightning_channeld: sent ERROR bad reestablish dataloss msg"
  }
}
```
1. `level` is `warn` or `error`:
`warn` means something seems bad happened and it's under control, but
we'd better check it;
`error` means something extremely bad is out of control, and it may lead
to crash;

2. `time` is the second since epoch;

3. `source`, in fact, is the `prefix` of the log_entry. It means where
the event happened, it may have the following forms:
`<node_id> chan #<db_id_of_channel>:`, `lightningd(<lightningd_pid>):`,
`plugin-<plugin_name>:`, `<daemon_name>(<daemon_pid>):`, `jsonrpc:`,
`jcon fd <error_fd_to_jsonrpc>:`, `plugin-manager`;

4. `log` is the context of the original log entry.

--Note:

1. The main code uses `UNUSUAL`/`BROKEN`, and plugin module uses `warn`
/`error`, considering the consistency with plugin, warning choose `warn`
/`error`. But users who use c-lightning with plugins may want to
`getlog` with specified level when receive warning. It's the duty for
plugin dev to turn `warn`/`error` into `UNUSUAL`/`BROKEN` and present it
 to the users, or pass it directly to `getlog`;

2. About time, `json_log()` in `log` module uses the Relative Time, from
 the time when `log_book` inited to the time when this event happend.
 But I consider the `UNUSUAL`/`BROKEN` event is rare, and it is very
 likely to happen after running for a long time, so for users, they will
  pay more attention to Absolute Time.

-- Related Change

1. Remove the definitions of `log`, `log_book`, `log_entry` from `log.c`
to `log.h`, then they can be used in warning declaration and definition.

2. Remove `void json_add_time(struct json_stream *result, const char
*fieldname, struct timespec ts)` from `log.c` to `json.c`, and add
related declaration in `json.h`. Now the notification function in
`notification.c` can call it.

2. Add a pointer to `struct lightningd` in `struct log_book`. This may
affect the independence of the `log` module, but storing a pointer to
`ld` is more direct;
htlc_accepted_hook
trueptolemy 6 years ago
committed by Rusty Russell
parent
commit
231703cc7f
  1. 11
      lightningd/json.c
  2. 4
      lightningd/json.h
  3. 46
      lightningd/log.c
  4. 34
      lightningd/log.h
  5. 27
      lightningd/notification.c
  6. 3
      lightningd/notification.h

11
lightningd/json.c

@ -429,3 +429,14 @@ void json_add_timeabs(struct json_stream *result, const char *fieldname,
json_add_member(result, fieldname, "%" PRIu64 ".%03" PRIu64,
(u64)t.ts.tv_sec, (u64)t.ts.tv_nsec / 1000000);
}
void json_add_time(struct json_stream *result, const char *fieldname,
struct timespec ts)
{
char timebuf[100];
snprintf(timebuf, sizeof(timebuf), "%lu.%09u",
(unsigned long)ts.tv_sec,
(unsigned)ts.tv_nsec);
json_add_string(result, fieldname, timebuf);
}

4
lightningd/json.h

@ -204,4 +204,8 @@ enum address_parse_result json_tok_address_scriptpubkey(const tal_t *ctx,
void json_add_timeabs(struct json_stream *result, const char *fieldname,
struct timeabs t);
/* used in log.c and notification.c*/
void json_add_time(struct json_stream *result, const char *fieldname,
struct timespec ts);
#endif /* LIGHTNING_LIGHTNINGD_JSON_H */

46
lightningd/log.c

@ -4,7 +4,6 @@
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/list/list.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/str/hex/hex.h>
@ -19,6 +18,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/options.h>
@ -31,39 +31,6 @@
/* Once we're up and running, this is set up. */
struct log *crashlog;
struct log_entry {
struct list_node list;
struct timeabs time;
enum log_level level;
unsigned int skipped;
const char *prefix;
char *log;
/* Iff LOG_IO */
const u8 *io;
};
struct log_book {
size_t mem_used;
size_t max_mem;
void (*print)(const char *prefix,
enum log_level level,
bool continued,
const struct timeabs *time,
const char *str,
const u8 *io, size_t io_len,
void *arg);
void *print_arg;
enum log_level print_level;
struct timeabs init_time;
struct list_head log;
};
struct log {
struct log_book *lr;
const char *prefix;
};
static void log_to_file(const char *prefix,
enum log_level level,
bool continued,
@ -664,17 +631,6 @@ static void add_skipped(struct log_info *info)
}
}
static void json_add_time(struct json_stream *result, const char *fieldname,
struct timespec ts)
{
char timebuf[100];
snprintf(timebuf, sizeof(timebuf), "%lu.%09u",
(unsigned long)ts.tv_sec,
(unsigned)ts.tv_nsec);
json_add_string(result, fieldname, timebuf);
}
static void log_to_json(unsigned int skipped,
struct timerel diff,
enum log_level level,

34
lightningd/log.h

@ -1,6 +1,7 @@
#ifndef LIGHTNING_LIGHTNINGD_LOG_H
#define LIGHTNING_LIGHTNINGD_LOG_H
#include "config.h"
#include <ccan/list/list.h>
#include <ccan/tal/tal.h>
#include <ccan/time/time.h>
#include <ccan/typesafe_cb/typesafe_cb.h>
@ -14,6 +15,39 @@ struct json_stream;
struct lightningd;
struct timerel;
struct log_entry {
struct list_node list;
struct timeabs time;
enum log_level level;
unsigned int skipped;
const char *prefix;
char *log;
/* Iff LOG_IO */
const u8 *io;
};
struct log_book {
size_t mem_used;
size_t max_mem;
void (*print)(const char *prefix,
enum log_level level,
bool continued,
const struct timeabs *time,
const char *str,
const u8 *io, size_t io_len,
void *arg);
void *print_arg;
enum log_level print_level;
struct timeabs init_time;
struct list_head log;
};
struct log {
struct log_book *lr;
const char *prefix;
};
/* We can have a single log book, with multiple logs in it: it's freed by
* the last struct log itself. */
struct log_book *new_log_book(size_t max_mem,

27
lightningd/notification.c

@ -1,9 +1,11 @@
#include "lightningd/notification.h"
#include <ccan/array_size/array_size.h>
#include <lightningd/json.h>
const char *notification_topics[] = {
"connect",
"disconnect",
"warning"
};
bool notifications_have_topic(const char *topic)
@ -33,3 +35,28 @@ void notify_disconnect(struct lightningd *ld, struct node_id *nodeid)
jsonrpc_notification_end(n);
plugins_notify(ld->plugins, take(n));
}
/*'warning' is based on LOG_UNUSUAL/LOG_BROKEN level log
*(in plugin module, they're 'warn'/'error' level). */
void notify_warning(struct lightningd *ld, struct log_entry *l)
{
struct jsonrpc_notification *n =
jsonrpc_notification_start(NULL, notification_topics[2]);
json_object_start(n->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(n->stream, "level",
l->level == LOG_BROKEN ? "error"
: "warn");
/* unsuaul/broken event is rare, plugin pay more attentions on
* the absolute time, like when channels failed. */
json_add_time(n->stream, "time", l->time.ts);
json_add_string(n->stream, "source", l->prefix);
json_add_string(n->stream, "log", l->log);
json_object_end(n->stream); /* .warning */
jsonrpc_notification_end(n);
plugins_notify(ld->plugins, take(n));
}

3
lightningd/notification.h

@ -3,6 +3,7 @@
#include "config.h"
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/plugin.h>
bool notifications_have_topic(const char *topic);
@ -11,4 +12,6 @@ void notify_connect(struct lightningd *ld, struct node_id *nodeid,
struct wireaddr_internal *addr);
void notify_disconnect(struct lightningd *ld, struct node_id *nodeid);
void notify_warning(struct lightningd *ld, struct log_entry *l);
#endif /* LIGHTNING_LIGHTNINGD_NOTIFICATION_H */

Loading…
Cancel
Save