aboutsummaryrefslogtreecommitdiffstats
path: root/logforwarder/src/apps/vespa-logforwarder-start/main.cpp
blob: d24c9c982a104a485e0461c2468be5528a6fe223 (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
59
60
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <csignal>
#include <unistd.h>

#include <vespa/log/log.h>
LOG_SETUP("vespa-logforwarder-start");

#include "splunk-starter.h"
#include "splunk-stopper.h"
#include <vespa/vespalib/util/sig_catch.h>

class Wrapper {
    const char *_configId;
public:
    Wrapper(const char *cfid) : _configId(cfid) {}
    void run() {
        vespalib::SigCatch catcher;
        SplunkStarter handler;
        handler.start(_configId);
        while (! catcher.receivedStopSignal()) {
            handler.check();
            usleep(12500); // Avoid busy looping;
        }
        handler.stop();
    }
};

int
main(int argc, char** argv)
{
    int c = -1;
    bool stopMode = false;
    const char *cfid = nullptr;
    while ((c = getopt(argc, argv, "Sc:")) != -1) {
        switch (c) {
        case 'S':
            stopMode = true;
            break;
        case 'c':
            cfid = optarg;
            break;
        default:
            cfid = nullptr;
            break;
        }
    }
    if (cfid == nullptr) {
        LOG(error, "Usage: %s -c <config-id>", argv[0]);
        return EXIT_FAILURE;
    }
    if (stopMode) {
        SplunkStopper stopper(cfid);
        stopper.check();
    } else {
        Wrapper wrapper(cfid);
        wrapper.run();
    }
    return 0;
}