Skip to content

Instantly share code, notes, and snippets.

@AlexanderFabisch
Last active May 24, 2018 13:32
Show Gist options
  • Save AlexanderFabisch/b5a4ae58317fdf3d5356b79ae4c1c283 to your computer and use it in GitHub Desktop.
Save AlexanderFabisch/b5a4ae58317fdf3d5356b79ae4c1c283 to your computer and use it in GitHub Desktop.
[InFuse] intermediate log format / minimal example

msgpack Logs

This is an example of how to create an InFuse-compatible intermediate log file in the msgpack format.

Setup

Install msgpack: run install.sh.

Compile Example

compile with g++:

g++ main.cpp

Run Example

Write logfile to output.msg:

./a.out

Read logfile from output.msg and print content on stdout:

python3 load.py

msgpack Documentation

git clone https://github.com/msgpack/msgpack-c.git
cd msgpack-c
mkdir build
cd build
sudo make install
import msgpack
log = msgpack.unpack(open("output.msg", "rb"))
print(log)
#include <msgpack.h>
#include <msgpack/fbuffer.h>
#include <cstdlib>
#include <cstring>
#include <string>
int main()
{
const unsigned NUM_DATASTREAMS = 1;
const char* STREAM_NAME = "component.port";
const char* META_STREAM_NAME = "component.port.meta";
const char* TYPENAME = "int";
const char* OUTPUT_FILE = "output.msg";
const unsigned STREAM_LENGTH = 2; // we have to know the stream length before we write the log...
int data[2];
data[0] = 2;
data[1] = 3;
int timestamps[2];
timestamps[0] = 0;
timestamps[1] = 1;
FILE* fp = fopen(OUTPUT_FILE, "w");
msgpack_packer packer;
msgpack_packer_init(&packer, fp, msgpack_fbuffer_write);
msgpack_pack_map(&packer, 2 * NUM_DATASTREAMS);
// Data stream (key)
msgpack_pack_str(&packer, strlen(STREAM_NAME));
msgpack_pack_str_body(&packer, STREAM_NAME, strlen(STREAM_NAME));
// Data stream (content)
msgpack_pack_array(&packer, STREAM_LENGTH);
// Write whatever you want here
for(unsigned i = 0; i < STREAM_LENGTH; i++)
msgpack_pack_int64(&packer, data[i]);
// Encode complex objects in maps, e.g.,
// msgpack_pack_map(&packer, NUM_FIELDS);
// msgpack_pack_str(&packer, ...);
// msgpack_pack_str_body(&packer, FIELD_NAME, ...);
// msgpack_pack_...(&packer, ...); // field content
// Meta data (key)
msgpack_pack_str(&packer, strlen(META_STREAM_NAME));
msgpack_pack_str_body(&packer, META_STREAM_NAME, strlen(META_STREAM_NAME));
// Meta data (content)
msgpack_pack_map(&packer, 2);
// Timestamps
const std::string TIME_KEY = "timestamps";
msgpack_pack_str(&packer, TIME_KEY.size());
msgpack_pack_str_body(&packer, TIME_KEY.c_str(), TIME_KEY.size());
msgpack_pack_array(&packer, STREAM_LENGTH);
for(unsigned i = 0; i < STREAM_LENGTH; i++)
msgpack_pack_int64(&packer, timestamps[i]);
// Type name
const std::string TYPE_KEY = "type";
msgpack_pack_str(&packer, TYPE_KEY.size());
msgpack_pack_str_body(&packer, TYPE_KEY.c_str(), TYPE_KEY.size());
msgpack_pack_str(&packer, strlen(TYPENAME));
msgpack_pack_str_body(&packer, TYPENAME, strlen(TYPENAME));
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment