logging via shm

This commit is contained in:
pigeatgarlic 2025-08-15 15:00:00 +07:00
parent 08cdd9cff7
commit f834902305
6 changed files with 48 additions and 29 deletions

View File

@ -1,3 +1,5 @@
#ifndef __SMEMORY__
#define __SMEMORY__
#define OUT_QUEUE_SIZE 8
#define IN_QUEUE_SIZE 8
@ -38,7 +40,10 @@ typedef struct _Memory {
DisplayQueue video[MAX_DISPLAY];
Queue audio;
Queue data;
Queue logging;
char worker_info[TAG_SIZE];
}Memory;
}Memory;
#endif

View File

@ -201,3 +201,9 @@ HANDLE IVSHMEM::getHandle()
{
return m_handle;
}
void
copy_to_packet(Packet* packet,void* data, size_t size) {
memcpy(packet->data+packet->size,data,size);
packet->size += size;
}

View File

@ -22,6 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <windows.h>
#include <stdbool.h>
#include <initguid.h>
#include <smemory.h>
DEFINE_GUID (GUID_DEVINTERFACE_IVSHMEM,
0xdf576976,0x569d,0x4672,0x95,0xa0,0xf5,0x7e,0x4e,0xa0,0xb2,0x10);
@ -123,3 +124,7 @@ private:
UINT64 m_size ; bool m_gotSize ;
void * m_memory ; bool m_gotMemory;
};
void
copy_to_packet(Packet* packet,void* data, size_t size);

View File

@ -17,6 +17,7 @@
// local includes
#include "logging.h"
#include "interprocess.h"
extern "C" {
#include <libavutil/log.h>
@ -71,7 +72,7 @@ namespace logging {
* ```
*/
[[nodiscard]] std::unique_ptr<deinit_t>
init(int min_log_level) {
init(int min_log_level, Queue* queue) {
if (sink) {
// Deinitialize the logging system before reinitializing it. This can probably only ever be hit in tests.
deinit();
@ -85,7 +86,7 @@ namespace logging {
sink->locked_backend()->add_stream(stream);
sink->set_filter(severity >= min_log_level);
sink->set_formatter([](const bl::record_view &view, bl::formatting_ostream &os) {
sink->set_formatter([queue](const bl::record_view &view, bl::formatting_ostream &os) {
constexpr const char *message = "Message";
constexpr const char *severity = "Severity";
constexpr int DATE_BUFFER_SIZE = 21 + 2 + 1; // Full string plus ": \0"
@ -115,6 +116,14 @@ namespace logging {
};
os << log_type << view.attribute_values()[message].extract<std::string>();
auto updated = queue->inindex + 1;
if (updated >= IN_QUEUE_SIZE)
updated = 0;
copy_to_packet(&queue->incoming[updated],(void*)log_type.cbegin(),log_type.size());
auto msg = view.attribute_values()[message].extract<std::string>();
copy_to_packet(&queue->incoming[updated],(void*)msg->c_str(),msg->size());
queue->inindex = updated;
});
// Flush after each log record to ensure log file contents on disk isn't stale.

View File

@ -9,6 +9,7 @@
// lib includes
#include <boost/log/common.hpp>
#include <boost/log/sinks.hpp>
#include <smemory.h>
using text_sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
@ -28,7 +29,7 @@ namespace logging {
void
deinit();
[[nodiscard]] std::unique_ptr<deinit_t>
init(int min_log_level);
init(int min_log_level,Queue* queue);
void
setup_av_logging(int min_log_level);
void

View File

@ -79,11 +79,6 @@ split (std::string s, char delim) {
}
void
copy_to_packet(Packet* packet,void* data, size_t size) {
memcpy(packet->data+packet->size,data,size);
packet->size += size;
}
int
main(void) {
@ -99,17 +94,29 @@ main(void) {
#pragma GCC diagnostic pop
mail::man = std::make_shared<safe::mail_raw_t>();
auto ivshmem = new IVSHMEM();
ivshmem->Initialize();
Memory* memory = NULL;
if (ivshmem->GetSize() < (UINT64)sizeof(Memory)) {
BOOST_LOG(error) << "Invalid ivshmem size: "sv << ivshmem->GetSize();
BOOST_LOG(error) << "Expected ivshmem size: "sv << sizeof(Memory);
memory = (Memory*)malloc(sizeof(Memory));
} else {
BOOST_LOG(info) << "Found ivshmem shared memory"sv;
memory = (Memory*)ivshmem->GetMemory();
}
auto log_deinit_guard = logging::init(config::sunshine.min_log_level);
if (memory == NULL) {
BOOST_LOG(error) << "Failed to allocate shared memory"sv;
return StatusCode::INVALID_IVSHMEM;
}
auto log_deinit_guard = logging::init(config::sunshine.min_log_level,&memory->logging);
if (!log_deinit_guard) {
BOOST_LOG(error) << "Logging failed to initialize"sv;
}
task_pool.start(1);
auto ivshmem = new IVSHMEM();
ivshmem->Initialize();
// Create signal handler after logging has been initialized
auto process_shutdown_event = mail::man->event<bool>(mail::shutdown);
@ -153,20 +160,6 @@ main(void) {
auto platf_deinit_guard = platf::init();
Memory* memory = NULL;
if (ivshmem->GetSize() < (UINT64)sizeof(Memory)) {
BOOST_LOG(error) << "Invalid ivshmem size: "sv << ivshmem->GetSize();
BOOST_LOG(error) << "Expected ivshmem size: "sv << sizeof(Memory);
memory = (Memory*)malloc(sizeof(Memory));
} else {
BOOST_LOG(info) << "Found ivshmem shared memory"sv;
memory = (Memory*)ivshmem->GetMemory();
}
if (memory == NULL) {
BOOST_LOG(error) << "Failed to allocate shared memory"sv;
return StatusCode::INVALID_IVSHMEM;
}
if (!platf_deinit_guard) {
BOOST_LOG(error) << "Platform failed to initialize"sv;
@ -201,7 +194,7 @@ main(void) {
auto expected_index = 0;
auto last_bitrate = 6;
char buffer[512] = {0};
char buffer[PACKET_SIZE] = {0};
while (!process_shutdown_event->peek() && !local_shutdown->peek()) {
while (expected_index == queue->outindex)
timer->sleep_for(1ms);