Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
application_runtime.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file application_runtime.h
20 * @brief Shared logging and operating-system checks for the survey executable.
21 * @details Session output is mirrored rather than redirected exclusively so
22 * interactive progress remains visible while a diagnostic trail is collected.
23 * The stream-buffer lock keeps concurrent worker messages from interleaving
24 * between the console and session log.
25 */
26#ifndef PANGEA_APPLICATION_RUNTIME_H
27#define PANGEA_APPLICATION_RUNTIME_H
28
29#include <filesystem>
30#include <fstream>
31#include <iostream>
32#include <iomanip>
33#include <mutex>
34#include <sstream>
35#include <cstring>
36#include <cerrno>
37#include <algorithm>
38
39namespace fs = std::filesystem;
40/** @brief Stream one message to both the original console and the session log. */
41class TeeBuffer : public std::streambuf {
42 std::streambuf* console_;
43 std::streambuf* file_;
44 std::mutex& mutex_;
45protected:
46 /** @brief Write a block to both outputs under the shared lock.
47 * @param data Borrowed character block.
48 * @param size Character count.
49 * @return The shorter of the two writes, exposing failure of either output.
50 * @details Keep the lock across both writes so competing diagnostics
51 * appear in the same order in the console and session log. */
52 std::streamsize xsputn(const char* data, std::streamsize size) override {
53 std::lock_guard<std::mutex> lock(mutex_);
54 const auto consoleSize = console_->sputn(data, size);
55 const auto fileSize = file_->sputn(data, size);
56 return std::min(consoleSize, fileSize);
57 }
58 /** @brief Mirror a single character not handled by a put area.
59 * @param character Streambuf integer character, or the EOF sentinel.
60 * @return The written character, or EOF on either write failure.
61 * @details Treat an EOF sentinel as a no-op, not as a byte to write. */
62 int overflow(int character) override {
63 if (traits_type::eq_int_type(character, traits_type::eof()))
64 return traits_type::not_eof(character);
65 std::lock_guard<std::mutex> lock(mutex_);
66 const auto value = traits_type::to_char_type(character);
67 if (traits_type::eq_int_type(console_->sputc(value), traits_type::eof()) ||
68 traits_type::eq_int_type(file_->sputc(value), traits_type::eof()))
69 return traits_type::eof();
70 return character;
71 }
72 /** @brief Synchronize the two outputs without interleaving other writes.
73 * @return Zero if both sync calls succeed, otherwise -1.
74 * @details A console sync failure short-circuits the file sync. */
75 int sync() override {
76 std::lock_guard<std::mutex> lock(mutex_);
77 return console_->pubsync() == 0 && file_->pubsync() == 0 ? 0 : -1;
78 }
79public:
80 /**
81 * @brief Bind two destinations to a caller-owned synchronization lock.
82 * @param[in] console Original console stream buffer.
83 * @param[in] file Session-log stream buffer.
84 * @param[in,out] mutex Lock shared by the stdout and stderr tee buffers.
85 */
86 TeeBuffer(std::streambuf* console, std::streambuf* file, std::mutex& mutex)
87 : console_(console), file_(file), mutex_(mutex) {}
88};
89
90/**
91 * @brief Mirror stdout and stderr into one session file for a scoped run.
92 * @details Construction installs the tee buffers and destruction restores the
93 * streams that were active before the session. Keep this object alive for the
94 * whole operation so worker diagnostics cannot outlive the file stream.
95 */
97 std::ofstream file_;
98 std::mutex mutex_;
101 std::streambuf* oldOutput_;
102 std::streambuf* oldErrors_;
103public:
104 /**
105 * @brief Open an append-only session log and install console mirroring.
106 * @param[in] directory Existing working directory for the session.
107 * @param[in] filename Log name relative to @p directory.
108 * @throws std::runtime_error If the log cannot be opened.
109 */
110 explicit SessionLog(const fs::path& directory, const fs::path& filename = "pangea.log")
111 : file_(directory / filename, std::ios::app),
112 output_(std::cout.rdbuf(), file_.rdbuf(), mutex_), errors_(std::cerr.rdbuf(), file_.rdbuf(), mutex_),
113 oldOutput_(std::cout.rdbuf()), oldErrors_(std::cerr.rdbuf()) {
114 if (!file_) throw std::runtime_error("Cannot create application log in output directory");
115 std::cout.rdbuf(&output_);
116 std::cerr.rdbuf(&errors_);
117 std::cout << std::unitbuf << "\n--- Pangea survey session ---\n";
118 }
119 /** @brief Detach the tee buffers before their file and lock are destroyed.
120 * @details Workers must already be stopped before restoring global streams. */
122 std::cout.rdbuf(oldOutput_);
123 std::cerr.rdbuf(oldErrors_);
124 }
125};
126
127/** @brief Convert a failed POSIX result into an operation-specific exception. */
128inline void posixCheck(bool success, const char* operation) {
129 if (!success) {
130 throw std::runtime_error(std::string(operation) + " failed (" +
131 std::strerror(errno) + ")");
132 }
133}
134
135/** @brief Compatibility name for the shared POSIX failure check. */
136inline void winCheck(bool success, const char* operation) {
137 posixCheck(success, operation);
138}
139
140#endif
void posixCheck(bool success, const char *operation)
Convert a failed POSIX result into an operation-specific exception.
void winCheck(bool success, const char *operation)
Compatibility name for the shared POSIX failure check.
size_t size
Definition chdk_pipe.c:143
SessionLog(const fs::path &directory, const fs::path &filename="pangea.log")
Open an append-only session log and install console mirroring.
std::streambuf * oldErrors_
std::ofstream file_
~SessionLog()
Detach the tee buffers before their file and lock are destroyed.
std::streambuf * oldOutput_
std::mutex mutex_
Stream one message to both the original console and the session log.
TeeBuffer(std::streambuf *console, std::streambuf *file, std::mutex &mutex)
Bind two destinations to a caller-owned synchronization lock.
int overflow(int character) override
Mirror a single character not handled by a put area.
std::mutex & mutex_
std::streambuf * file_
std::streambuf * console_
std::streamsize xsputn(const char *data, std::streamsize size) override
Write a block to both outputs under the shared lock.
int sync() override
Synchronize the two outputs without interleaving other writes.
static pthread_mutex_t mutex
Definition pose_log.c:72