summaryrefslogtreecommitdiffstats
path: root/tools/slang-replay/main.cpp
blob: d27faacb382a23648f994105cd40f595a8225697 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "../../source/core/slang-io.h"

#include <memory>
#include <replay/json-consumer.h>
#include <replay/recordFile-processor.h>
#include <replay/replay-consumer.h>
#include <replay/slang-decoder.h>
#include <stdio.h>

struct Options
{
    bool convertToJson{false};
    Slang::String recordFileName;
};

void printUsage()
{
    printf("Usage: slang-replay [options] <record-file>\n");
    printf("Options:\n");
    printf(
        "  --convert-json, -cj: Convert the record file to a JSON file in the same directory with record file.\n\
                       When this option is set, it won't replay the record file.\n");
}

Options parseOption(int argc, char* argv[])
{
    Options option;
    char const* arg{};
    if (argc <= 1)
    {
        printUsage();
        exit(1);
    }

    int argIndex = 1;
    while (argIndex < argc)
    {
        arg = argv[argIndex];

        // For anything not starting with a '-', it is a file name
        if (arg[0] != '-')
        {
            option.recordFileName = arg;
            argIndex++;
        }
        else if ((strcmp("--convert-json", arg) == 0) || (strcmp("-cj", arg) == 0))
        {
            option.convertToJson = true;
            argIndex++;
        }
        else if ((strcmp("--help", arg) == 0) || (strcmp("-h", arg) == 0))
        {
            printUsage();
            exit(0);
        }
        else
        {
            // Unknown option
            printf("Unknown option: %s\n", arg);
            printUsage();
            exit(1);
        }
    }

    if (option.recordFileName.getLength() == 0)
    {
        printUsage();
        exit(1);
    }

    return option;
}

int main(int argc, char* argv[])
{
    Options options = parseOption(argc, argv);

    SlangRecord::RecordFileProcessor recordFileProcessor(options.recordFileName);

    Slang::String jsonPath = Slang::Path::replaceExt(options.recordFileName, "json");
    Slang::RefPtr<SlangRecord::JsonConsumer> jsonConsumer;
    SlangRecord::ReplayConsumer replayConsumer;

    SlangRecord::SlangDecoder decoder;

    if (options.convertToJson)
    {
        jsonConsumer = new SlangRecord::JsonConsumer(jsonPath);
        decoder.addConsumer(jsonConsumer.get());
    }
    else
    {
        decoder.addConsumer(&replayConsumer);
    }

    recordFileProcessor.addDecoder(&decoder);

    while (true)
    {
        if (!recordFileProcessor.processNextBlock())
        {
            break;
        }
    }
    return 0;
}