aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/logger/logreplay.c
blob: ff95972f7df4fadd58d0498e50f44f4c21a5a193 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
    char line[10000];
    
    double delta = 0;

    if (argc != 1) {
        fprintf(stderr, "Usage: %s < <vespa.log>\n"
                "Replays a vespa log file with the same timing delta between each log message.\n"
                "Reprints the log messages without timestamps.\n\n",
                argv[0]);
        return EXIT_FAILURE;
    }
    
    while (fgets(line, sizeof line, stdin)) {
        struct timeval tv;
        char *s;
        double log_time;
        double now;
        double delay;

        gettimeofday(&tv, NULL);
        now = tv.tv_sec + 1e-6 * tv.tv_usec;
        
        log_time = strtod(line, NULL);
        if (!delta) {
            delta = now - log_time;
        }

        delay = log_time + delta - now;
        if (delay > 0) {
            tv.tv_sec = floor(delay);
            tv.tv_usec = 1e6 * (delay - floor(delay));
            /* fprintf(stderr, "Sleeping for %.3fs\n", delay); */
            select(0, NULL, NULL, NULL, &tv);
        }

        s = strchr(line, '\t');
        if (s) {
            s = s + 1;
        } else {
            s = line;
        }
        fwrite(s, strlen(s), 1, stdout);
        fflush(stdout);
    }
    return EXIT_SUCCESS;
}