aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/print/fileconfigsnapshotwriter.cpp
blob: d2b1ce8e122e84255befea7c4bd15ab6a3cef08a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "fileconfigsnapshotwriter.h"
#include "jsonconfigformatter.h"
#include <vespa/config/common/exceptions.h>
#include <fstream>

namespace config {

FileConfigSnapshotWriter::FileConfigSnapshotWriter(const vespalib::string & fileName)
    : _fileName(fileName)
{
}

bool
FileConfigSnapshotWriter::write(const ConfigSnapshot & snapshot)
{
    std::ofstream file(_fileName.c_str());
    if (!file.is_open())
        throw ConfigWriteException("error: could not open output file '%s'\n", _fileName.c_str());

    ConfigDataBuffer buffer;
    snapshot.serialize(buffer);
    JsonConfigFormatter formatter(true);
    formatter.encode(buffer);
    file << buffer.getEncodedString();
    return !file.fail();
}

} // namespace config