/**
 * @file wrtd-logging.c
 *
 * Copyright (c) 2018-2019 CERN (home.cern)
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <inttypes.h>
#include "libwrtd.h"
#include "libwrtd-private.h"
#include "wrtd-internal.h"

static void help(void)
{
        fprintf(stderr, "wrtd-logging -D <device>\n");
        fprintf(stderr, "It shows logging information coming from Real-Time applications\n");
        fprintf(stderr, "-D device id\n");
        fprintf(stderr, "-n number of messages to read (0 means infinite)\n");
        exit(1);
}

static void print_logging (struct wrtd_dev *wrtd, int n_read)
{
        char log_entry[WRTD_LOG_ENTRY_SIZE];
        enum wrtd_status status;
        unsigned int i;

        for(i = 0; i < n_read || n_read == 0; i++) {
                do {
                        status = wrtd_get_next_event_log_entry(
                                wrtd, WRTD_LOG_ENTRY_SIZE, log_entry);
                        if (status > WRTD_SUCCESS) {
                                fprintf(stderr, "Warning: log entry truncated, "
                                        "missing %d characters\n",
                                        status - WRTD_LOG_ENTRY_SIZE);
                        }
                        else if (status != WRTD_SUCCESS) {
                                char error_message[256];
                                enum wrtd_status error_status;
                                wrtd_get_error (wrtd, &error_status, 256, error_message);
                                fprintf(stderr, "Error: %s\n", error_message);
                                return;
                        }

                } while (strlen(log_entry) == 0);
                printf("%s\n", log_entry);
                fflush(stdout);
        }
}

int main(int argc, char *argv[])
{
        struct wrtd_dev *wrtd;
        const char *dev_name = NULL;
        enum wrtd_status status;
        char *endptr;
        int n_read = 0;
        char c;

        while ((c = getopt (argc, argv, "hD:n:")) != -1) {
                switch (c) {
                default:
                        help();
                        break;
                case 'D':
                        dev_name = optarg;
                        break;
                case 'n':
                        n_read = strtoul(optarg, &endptr, 0);
                        if (*endptr != 0) {
                                fprintf(stderr, "bad value for -n");
                                exit (1);
                        }
                        break;
                }
        }

        if (dev_name == NULL) {
                help();
                exit(1);
        }

        /* Open.  */
        status = wrtd_init(dev_name, 0, NULL, &wrtd);
        if (status != WRTD_SUCCESS) {
                char error_message[256];
                enum wrtd_status error_status;
                wrtd_get_error (wrtd, &error_status, 256, error_message);
                fprintf(stderr, "Cannot open WRTD: %s\n", error_message);
                return 1;
        }

        print_logging (wrtd, n_read);

        wrtd_close(wrtd);
        exit(0);
}
