FileTopicWriter.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX
17* @author Mirko Waechter( mirko.waechter at kit dot edu)
18* @date 2016
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22#include "FileTopicWriter.h"
23
24#include <fstream>
25#include <sstream>
26#include <string>
27
31
32namespace armarx
33{
34
35 FileTopicWriter::FileTopicWriter(const std::filesystem::path& path) : filepath(path)
36 {
37 std::ofstream* o = new std::ofstream(path.string().c_str());
38 if (!o->is_open())
39 {
40 ARMARX_ERROR_S << "Could not open '" << path.string() << "'!";
41 }
42 stream = o;
43 }
44
45 FileTopicWriter::FileTopicWriter(std::ostream* stream) : stream(stream)
46 {
47 }
48
50 {
51 if (!filepath.empty())
52 {
53 delete stream;
54 }
55 }
56
57 std::filesystem::path
59 {
60 return filepath;
61 }
62
63 void
65 {
66 JSONObjectPtr json = new JSONObject();
67 json->setString("topic", topicData.topicName);
68 json->setString("op", topicData.operationName);
69 json->setDouble("t", topicData.timestamp.toMicroSecondsDouble());
70 json->setString(
71 "data",
72 TopicUtil::Encode64(std::string(topicData.inParams.begin(), topicData.inParams.end())));
73
74 (*stream) << json->asString() << std::endl;
75 }
76
77} // namespace armarx
std::filesystem::path getFilepath() const
void write(const TopicUtil::TopicData &topicData) override
FileTopicWriter(const std::filesystem::path &path)
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
std::string Encode64(const std::string &val)
Definition TopicUtil.cpp:35
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< JSONObject > JSONObjectPtr
Definition JSONObject.h:34
std::vector< Ice::Byte > inParams
Definition TopicUtil.h:48