Updated: April 19, 2023 |
Parse a slogger2 buffer
#include <slog2_parse.h> int slog2_parse_dynamic_buffer( slog2_log_t log, int buffer_index, slog2_packet_info_t *packet_info, slog2_packet_callback callback, void *param ); int slog2_parse_static_buffer( slog2_log_t log, int buffer_index, slog2_packet_info_t *packet_info, slog2_packet_callback callback, void *param );
typedef int (*slog2_packet_callback)( slog2_packet_info_t *__info, void *__payload, void *__param );
that's called to process each packet in the buffer; see below.
The slog2_parse_dynamic_buffer() and slog2_parse_static_buffer() functions parse through a live or static slogger2 buffer (respectively) from beginning to end, finding valid packets and calling the given callback function for each packet. If the callback returns a nonzero value, then these functions return immediately.
The prototype of the callback function is:
int callback( slog2_packet_info_t *info, void *payload, void *param );
The arguments are:
The callback should return 0 on success, or a nonzero value on failure.
0 on success, or -1 on failure.
#include <stdlib.h> #include <stdio.h> #include <sys/slog2.h> #include <slog2_parse.h> int my_callback (slog2_packet_info_t *info, void *payload, void *param) { printf (" Processing packet %d of size %d\n", info->sequence_number, info->data_size); switch (info->severity) { case SLOG2_SHUTDOWN: printf (" SHUTDOWN: "); break; case SLOG2_CRITICAL: printf (" CRITICAL: "); break; case SLOG2_ERROR: printf (" ERROR: "); break; case SLOG2_WARNING: printf (" Warning: "); break; case SLOG2_NOTICE: printf (" Notice: "); break; case SLOG2_INFO: printf (" Info: "); break; case SLOG2_DEBUG1: printf (" debug1: "); break; case SLOG2_DEBUG2: printf (" debug2: "); break; default: printf (" Unknown: "); } printf ("%s\n", (char *)payload); return 0; } int main(int argc, char *argv[]) { char log_path[PATH_MAX] = "/dev/shmem/slogger2/slogger_two.184341"; slog2_log_t my_log; slog2_log_info_t log_info = SLOG2_LOG_INFO_INIT; int buffer_index; slog2_packet_info_t packet_info = SLOG2_PACKET_INFO_INIT; slog2_buffer_info_t buffer_info = SLOG2_BUFFER_INFO_INIT; /* Open a log file */ my_log = slog2_open_log(log_path); if (my_log == 0) { printf ("Couldn't open %s.\n", log_path); return EXIT_FAILURE; } /* What do we know about the log? */ if (slog2_get_log_info(my_log, &log_info) == -1) { printf ("Couldn't get information about the log.\n"); return EXIT_FAILURE; } printf ("Log information:\n"); printf (" Num buffers: %d\n", log_info.num_buffers); printf (" Owner: %d\n", log_info.owner_pid); printf (" Buffer set name: %s\n", log_info.buffer_set_name); printf (" Verbosity: %d\n", log_info.verbosity_level); /* Parse the log file. */ printf ("\nBuffers:\n"); for (buffer_index = 0; buffer_index < log_info.num_buffers; buffer_index++) { if (slog2_get_buffer_info(my_log, buffer_index, &buffer_info) == -1) { printf ("Couldn't get info about buffer %d.\n", buffer_index); return EXIT_FAILURE; } printf (" Buffer %d: size: %d Name: %s\n", buffer_index, buffer_info.buffer_size, buffer_info.buffer_name); if (slog2_parse_static_buffer( my_log, buffer_index, &packet_info, my_callback, NULL) == -1) { printf ("slog2_parse_static_buffer() failed.\n"); return EXIT_FAILURE; } } /* Close the log file. */ slog2_close_log( my_log); return EXIT_SUCCESS; }
When run on the log produced by the example for slog2_register(), this program produces the following output:
Log information: Num buffers: 2 Owner: 184341 Buffer set name: slogger_two Verbosity: 5 Buffers: Buffer 0: size: 28672 Name: hi_rate_logging Processing packet 0 of size 63 Info: Writing a formatted string into the buffer: ./root/slogger_two Processing packet 1 of size 43 Info: Writing a constant string into the buffer. Processing packet 2 of size 37 Warning: string:Hello world, some_number:5108 Buffer 1: size: 4096 Name: lo_rate_logging Processing packet 3 of size 28 Notice: This string will be logged.
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |