Output messages in text files

* Add support for saving the messages on a text file
* Add timestamp support for each message in ISO 8601 format
This commit is contained in:
Manolis Surligas 2017-04-07 23:54:23 +03:00
parent e114eb0cd0
commit ca1b8b1242
4 changed files with 173 additions and 39 deletions

View File

@ -3,7 +3,7 @@
<name>Multi Format Message Sink</name>
<key>satnogs_multi_format_msg_sink</key>
<import>import satnogs</import>
<make>satnogs.multi_format_msg_sink($format)</make>
<make>satnogs.multi_format_msg_sink($format, $timestamp, $outstream, $filename)</make>
<param>
<name>Output format</name>
@ -22,6 +22,41 @@
<key>2</key>
</option>
</param>
<param>
<name>Output Timestamp</name>
<key>timestamp</key>
<type>enum</type>
<option>
<name>No</name>
<key>False</key>
</option>
<option>
<name>Yes</name>
<key>True</key>
</option>
</param>
<param>
<name>Output Result</name>
<key>outstream</key>
<type>enum</type>
<option>
<name>STDOUT</name>
<key>True</key>
<opt>t:stdout</opt>
</option>
<option>
<name>File</name>
<key>False</key>
<opt>t:file</opt>
</option>
</param>
<param>
<name>File</name>
<key>filename</key>
<value></value>
<type>file_save</type>
<hide>#if $outstream.t == "file" then 'none' else 'all'#</hide>
</param>
<sink>
<name>in</name>

View File

@ -2,7 +2,7 @@
/*
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
*
* Copyright (C) 2016, Libre Space Foundation <http://librespacefoundation.org/>
* Copyright (C) 2016,2017, Libre Space Foundation <http://librespacefoundation.org/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -44,12 +44,24 @@ namespace gr
/*!
* \brief Block accepting clear text messages from various decoders.
* Its purpose is to forward these messages at other services, programs,
* stdout, etc,
* Its purpose is to either print these messages to stdout or save them
* in text format in a file.
*
* Depending on format parameter, the contents of each message are
* converted to hexademical, binary or ASCII format.
*
* @param format the format that will used to display the messages.
* 0: Clear Text 1: Hexademical 2: Binary
* @param timestamp if set, a ISO 8601 timestamp is inserted in front of
* each message
* @param out_stdout if set, the messages are displayed in the stdout.
* Otherwise messages are saved in a text file
* @param filepath specifies the file path of the text file
*/
static sptr
make (size_t format);
make (size_t format, bool timestamp = true,
bool out_stdout = true,
const std::string& filepath = "");
};
} // namespace satnogs

View File

@ -2,7 +2,7 @@
/*
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
*
* Copyright (C) 2016, Libre Space Foundation <http://librespacefoundation.org/>
* Copyright (C) 2016,2017 Libre Space Foundation <http://librespacefoundation.org/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -24,58 +24,136 @@
#include <gnuradio/io_signature.h>
#include "multi_format_msg_sink_impl.h"
#include <ctime>
#include <iostream>
#include <iomanip>
namespace gr {
namespace satnogs {
namespace gr
{
namespace satnogs
{
multi_format_msg_sink::sptr
multi_format_msg_sink::make(size_t format)
multi_format_msg_sink::make (size_t format,
bool timestamp,
bool out_stdout,
const std::string& filepath)
{
return gnuradio::get_initial_sptr
(new multi_format_msg_sink_impl(format));
return gnuradio::get_initial_sptr (
new multi_format_msg_sink_impl (format, timestamp,
out_stdout, filepath));
}
void
multi_format_msg_sink_impl::msg_handler (pmt::pmt_t msg)
multi_format_msg_sink_impl::msg_handler_file (pmt::pmt_t msg)
{
uint8_t *su;
std::string s((const char *)pmt::blob_data(msg), pmt::blob_length(msg));
switch(d_format){
std::string s ((const char *) pmt::blob_data (msg),
pmt::blob_length (msg));
if(d_timestamp) {
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
d_fos << "[" << std::put_time(&tm, "%F %T %z") << "] ";
}
switch (d_format)
{
case 0:
std::cout << "Received text sequence:" << s << " " << std::endl;
d_fos << s << std::endl;
break;
case 1:
su = (uint8_t *)pmt::blob_data(msg);
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
printf ("0x%02x ", su[i]);
}
std::cout << std::endl;
su = (uint8_t *) pmt::blob_data (msg);
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
d_fos << std::hex << std::showbase << std::setw (4)
<< (uint32_t) su[i] << " ";
}
d_fos << std::endl;
break;
case 2:
su = (uint8_t *)pmt::blob_data(msg);
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
std::cout << "0b" << std::bitset<8> (su[i]) << " ";
}
std::cout << std::endl;
su = (uint8_t *) pmt::blob_data (msg);
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
d_fos << "0b" << std::bitset<8> (su[i]) << " ";
}
d_fos << std::endl;
break;
default:
printf("Invalid format");
throw std::invalid_argument("Invalid format");
}
}
void
multi_format_msg_sink_impl::msg_handler_stdout (pmt::pmt_t msg)
{
uint8_t *su;
std::string s ((const char *) pmt::blob_data (msg),
pmt::blob_length (msg));
if(d_timestamp) {
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::cout << "[" << std::put_time(&tm, "%F %T %z") << "] ";
}
switch (d_format)
{
case 0:
std::cout << s << std::endl;
break;
case 1:
su = (uint8_t *) pmt::blob_data (msg);
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
std::cout << std::hex << std::showbase << std::setw (4)
<< (uint32_t) su[i] << " ";
}
std::cout << std::endl;
break;
case 2:
su = (uint8_t *) pmt::blob_data (msg);
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
std::cout << "0b" << std::bitset<8> (su[i]) << " ";
}
std::cout << std::endl;
break;
default:
throw std::invalid_argument("Invalid format");
}
}
/*
* The private constructor
*/
multi_format_msg_sink_impl::multi_format_msg_sink_impl(size_t format)
: gr::block("multi_format_msg_sink",
gr::io_signature::make(0, 0, 0),
gr::io_signature::make(0, 0, 0)),
d_format(format)
multi_format_msg_sink_impl::multi_format_msg_sink_impl (
size_t format, bool timestamp, bool out_stdout,
const std::string& filepath) :
gr::block ("multi_format_msg_sink",
gr::io_signature::make (0, 0, 0),
gr::io_signature::make (0, 0, 0)),
d_format (format),
d_timestamp (timestamp),
d_stdout (out_stdout)
{
message_port_register_in(pmt::mp("in"));
set_msg_handler (
pmt::mp ("in"),
boost::bind (&multi_format_msg_sink_impl::msg_handler, this, _1));
message_port_register_in (pmt::mp ("in"));
if(out_stdout) {
set_msg_handler (
pmt::mp ("in"),
boost::bind (&multi_format_msg_sink_impl::msg_handler_stdout,
this, _1));
}
else{
d_fos.open(filepath);
set_msg_handler (
pmt::mp ("in"),
boost::bind (&multi_format_msg_sink_impl::msg_handler_file,
this, _1));
}
}
multi_format_msg_sink_impl::~multi_format_msg_sink_impl ()
{
if(!d_stdout) {
d_fos.close();
}
}
} /* namespace satnogs */

View File

@ -2,7 +2,7 @@
/*
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
*
* Copyright (C) 2016, Libre Space Foundation <http://librespacefoundation.org/>
* Copyright (C) 2016,2017, Libre Space Foundation <http://librespacefoundation.org/>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,6 +22,7 @@
#define INCLUDED_SATNOGS_MULTI_FORMAT_MSG_SINK_IMPL_H
#include <satnogs/multi_format_msg_sink.h>
#include <fstream>
namespace gr
{
@ -32,12 +33,20 @@ namespace gr
{
private:
void
msg_handler(pmt::pmt_t msg);
msg_handler_stdout (pmt::pmt_t msg);
void
msg_handler_file (pmt::pmt_t msg);
size_t d_format;
const size_t d_format;
const bool d_timestamp;
const bool d_stdout;
std::ofstream d_fos;
public:
multi_format_msg_sink_impl (size_t format);
multi_format_msg_sink_impl (size_t format, bool timestamp,
bool out_stdout, const std::string& filepath);
~multi_format_msg_sink_impl ();
};