diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-05-22 07:28:07 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-05-22 07:28:07 +0200 |
commit | 31faad73568712e9e77cfbd0bf50f3b7cc2ede00 (patch) | |
tree | 72329d00812aafab57c593d6d818845a8afc5fa8 | |
parent | 94e42b28636c7b629574dddd082f1ac9caab403e (diff) | |
download | ODR-AudioEnc-31faad73568712e9e77cfbd0bf50f3b7cc2ede00.tar.gz ODR-AudioEnc-31faad73568712e9e77cfbd0bf50f3b7cc2ede00.tar.bz2 ODR-AudioEnc-31faad73568712e9e77cfbd0bf50f3b7cc2ede00.zip |
Make stats output JSON-compatible
JSON is a subset of YAML, so YAML parsers still work
-rwxr-xr-x | example_stats_receiver.py | 4 | ||||
-rw-r--r-- | src/StatsPublish.cpp | 26 | ||||
-rw-r--r-- | src/StatsPublish.h | 2 |
3 files changed, 17 insertions, 15 deletions
diff --git a/example_stats_receiver.py b/example_stats_receiver.py index a48f560..9c9750c 100755 --- a/example_stats_receiver.py +++ b/example_stats_receiver.py @@ -7,7 +7,7 @@ import os import os.path import socket import argparse -import yaml +import json parser = argparse.ArgumentParser( description="Example Stats UNIX Datagram Socket Receiver") @@ -34,5 +34,5 @@ while True: data, addr = sock.recvfrom(256) logging.info("RX from {}". format(addr)) - data = yaml.safe_load(data) + data = json.loads(data) print(data) diff --git a/src/StatsPublish.cpp b/src/StatsPublish.cpp index 5998913..7e49ad1 100644 --- a/src/StatsPublish.cpp +++ b/src/StatsPublish.cpp @@ -88,28 +88,30 @@ void StatsPublisher::notify_overrun() void StatsPublisher::send_stats() { - // Manually build YAML, as it's quite easy. - stringstream yaml; - yaml << "---\n"; - yaml << "program: " << PACKAGE_NAME << "\n"; - yaml << "version: " << + // Manually build JSON. We can be certain that + // our fields don't contain quotes + stringstream json; + json << "{ "; + json << "\"program\": \"" << PACKAGE_NAME << "\", "; + json << "\"version\": \"" << #if defined(GITVERSION) GITVERSION #else PACKAGE_VERSION #endif - << "\n"; - yaml << "audiolevels: { left: " << m_audio_left << ", right: " << m_audio_right << "}\n"; - yaml << "driftcompensation: { underruns: " << m_num_underruns << ", overruns: " << m_num_overruns << "}\n"; + << "\", "; + json << "\"audiolevels\": { \"left\": " << m_audio_left << ", \"right\": " << m_audio_right << "}, "; + json << "\"driftcompensation\": { \"underruns\": " << m_num_underruns << ", \"overruns\": " << m_num_overruns << "} "; + json << "}"; - const auto yamlstr = yaml.str(); + const auto jsonstr = json.str(); struct sockaddr_un claddr; memset(&claddr, 0, sizeof(struct sockaddr_un)); claddr.sun_family = AF_UNIX; snprintf(claddr.sun_path, sizeof(claddr.sun_path), "%s", m_socket_path.c_str()); - int ret = ::sendto(m_sock, yamlstr.data(), yamlstr.size(), 0, + int ret = ::sendto(m_sock, jsonstr.data(), jsonstr.size(), 0, (struct sockaddr *) &claddr, sizeof(struct sockaddr_un)); if (ret == -1) { // This suppresses the -Wlogical-op warning @@ -128,9 +130,9 @@ void StatsPublisher::send_stats() fprintf(stderr, "Statistics send failed: %s\n", strerror(errno)); } } - else if (ret != (ssize_t)yamlstr.size()) { + else if (ret != (ssize_t)jsonstr.size()) { fprintf(stderr, "Statistics send incorrect length: %d bytes of %zu transmitted\n", - ret, yamlstr.size()); + ret, jsonstr.size()); } else if (not m_destination_available) { fprintf(stderr, "Stats destination is now available at %s\n", m_socket_path.c_str()); diff --git a/src/StatsPublish.h b/src/StatsPublish.h index 7ff7da4..06a738d 100644 --- a/src/StatsPublish.h +++ b/src/StatsPublish.h @@ -29,7 +29,7 @@ * * Currently, only audio levels are collected. * - * Output is formatted in YAML + * Output is formatted in JSON */ class StatsPublisher { public: |