auxoncorp / modality-probe

A distributed causal tracing probe suitable for embedded systems.
Apache License 2.0
8 stars 1 forks source link

Add a more log-friendly API #264

Open jonlamb-gh opened 4 years ago

jonlamb-gh commented 4 years ago

It'd be nice to have some ability to log events in a way that more closely resembles traditional logging frameworks. Currently users have to manage unique event identifiers when using the CLI-assisted macros and tooling. One thing we could do for a more log-orientated API is to lean on the C preprocessor's standard predefined macro __LINE__ (in Rust this is less of a problem). Where we generate per-scanned-source-file headers with the generated constants/definitions specific to the source file they appear in. This allows us to concatenate some event name prefix with the line number of the instrumentation in the code, something like LOG_EVENT_LINE_N, where N is the line number. To make the events unique in the manifest, we could prepend/append the source file or some other token.

Here's an example of that:

/* probe.h or probe-log.h */

#define MODALITY_CAT2(a, b) a##b
#define MODALITY_CAT(a, b) MODALITY_CAT2(a, b)

/* Optional tags at the end */
#define MODALITY_PROBE_LOG(probe, desc, ...) \
    modality_probe_record_event(probe, MODALITY_CAT(LOG_EVENT_LINE_, __LINE__))

/* Optional tags at the end */
#define MODALITY_PROBE_LOG_W_I8(probe, desc, payload, ...) \
    modality_probe_record_event_with_payload_i8(probe, MODALITY_CAT(LOG_EVENT_LINE_, __LINE__), payload)
/* main.c */

/* CLI generates the header file generated_ids_4_main_dot_c.h with these definitions:
 * - #define LOG_EVENT_LINE_9 (1234)
 * - #define LOG_EVENT_LINE_13 (12345)
 */
#include "generated_ids_4_main_dot_c.h"

err = MODALITY_PROBE_LOG(probe, "This is a log message");
assert(err == MODALITY_PROBE_ERROR_OK);

int8_t payload = -2;
err = MODALITY_PROBE_LOG_W_I8(probe, "This is a log message, payload = {}", payload);
assert(err == MODALITY_PROBE_ERROR_OK);