1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-04-18 10:17:14 +02:00

Sync console output during prototyping.

This commit is contained in:
Sandu Liviu Catalin 2020-09-06 23:55:15 +03:00
parent 425fd46bcf
commit 054a7983a5
2 changed files with 45 additions and 10 deletions
module/Library

@ -80,6 +80,18 @@ void Worker::Process(size_t jobs)
} }
for (auto & t : workers) for (auto & t : workers)
{ {
const size_t logs = std::min(t->m_Logs.size_approx(), size_t{32});
// Flush log messages
for (size_t n = 0; n < logs; ++n)
{
std::unique_ptr< Log > log;
// Try to get a log from the queue
if (t->m_Logs.try_dequeue(log))
{
// Send it to the logger
Logger::Get().Send(log->first, false, log->second.data(), log->second.size());
}
}
for (size_t n = 0; n < jobs; ++n) for (size_t n = 0; n < jobs; ++n)
{ {
std::unique_ptr< BaseJob > job; std::unique_ptr< BaseJob > job;
@ -133,6 +145,8 @@ void Worker::Start()
} }
// Create the JS state // Create the JS state
m_VM = sq_open(m_StackSize); m_VM = sq_open(m_StackSize);
// Associate with this VM
sq_setforeignptr(m_VM, this);
// Tell the VM to use these functions to output user on error messages // Tell the VM to use these functions to output user on error messages
sq_setprintfunc(m_VM, PrintFunc, ErrorFunc); sq_setprintfunc(m_VM, PrintFunc, ErrorFunc);
// This is now running // This is now running
@ -174,27 +188,35 @@ void Worker::Work()
} }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Worker::PrintFunc(HSQUIRRELVM /*vm*/, CSStr msg, ...) void Worker::PrintFunc(HSQUIRRELVM vm, CSStr msg, ...)
{ {
auto worker = reinterpret_cast< Worker * >(sq_getforeignptr(vm));
// Initialize the variable argument list // Initialize the variable argument list
va_list args; va_list args;
va_start(args, msg); va_start(args, msg);
// Forward the message to the logger // Initialize an empty log message
std::vprintf(msg, args); std::unique_ptr< Log > ptr(new Log{uint8_t{LOGL_USR}, String{}});
std::puts(""); // Attempt to generate the message
PrintToStrFv(ptr->second, msg, args);
// Let the main thread deal with it
worker->m_Logs.enqueue(std::move(ptr));
// Finalize the variable argument list // Finalize the variable argument list
va_end(args); va_end(args);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Worker::ErrorFunc(HSQUIRRELVM /*vm*/, CSStr msg, ...) void Worker::ErrorFunc(HSQUIRRELVM vm, CSStr msg, ...)
{ {
auto worker = reinterpret_cast< Worker * >(sq_getforeignptr(vm));
// Initialize the variable argument list // Initialize the variable argument list
va_list args; va_list args;
va_start(args, msg); va_start(args, msg);
// Tell the logger to display debugging information // Initialize an empty log message
std::vprintf(msg, args); std::unique_ptr< Log > ptr(new Log{uint8_t{LOGL_ERR}, String{}});
std::puts(""); // Attempt to generate the message
PrintToStrFv(ptr->second, msg, args);
// Let the main thread deal with it
worker->m_Logs.enqueue(std::move(ptr));
// Finalize the variable argument list // Finalize the variable argument list
va_end(args); va_end(args);
} }

@ -4,6 +4,7 @@
#include "Base/Shared.hpp" #include "Base/Shared.hpp"
#include "Base/VecMap.hpp" #include "Base/VecMap.hpp"
#include "Library/Worker/Job.hpp" #include "Library/Worker/Job.hpp"
#include "Logger.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <thread> #include <thread>
@ -162,13 +163,25 @@ private:
*/ */
using Jobs = ConcurrentQueue< std::unique_ptr< BaseJob > >; using Jobs = ConcurrentQueue< std::unique_ptr< BaseJob > >;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* BaseJob queue. *
*/
using Log = std::pair< uint8_t, String >;
/* --------------------------------------------------------------------------------------------
*
*/
using Logs = ConcurrentQueue< std::unique_ptr< Log > >;
/* --------------------------------------------------------------------------------------------
* Pending job queue.
*/ */
Jobs m_PendingJobs; Jobs m_PendingJobs;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* BaseJob queue. * Finished job queue.
*/ */
Jobs m_FinishedJobs; Jobs m_FinishedJobs;
/* --------------------------------------------------------------------------------------------
* Log message queue.
*/
Logs m_Logs;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Loop state. * Loop state.
*/ */