1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01: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

View File

@ -80,6 +80,18 @@ void Worker::Process(size_t jobs)
}
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)
{
std::unique_ptr< BaseJob > job;
@ -133,6 +145,8 @@ void Worker::Start()
}
// Create the JS state
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
sq_setprintfunc(m_VM, PrintFunc, ErrorFunc);
// 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
va_list args;
va_start(args, msg);
// Forward the message to the logger
std::vprintf(msg, args);
std::puts("");
// Initialize an empty log message
std::unique_ptr< Log > ptr(new Log{uint8_t{LOGL_USR}, String{}});
// 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
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
va_list args;
va_start(args, msg);
// Tell the logger to display debugging information
std::vprintf(msg, args);
std::puts("");
// Initialize an empty log message
std::unique_ptr< Log > ptr(new Log{uint8_t{LOGL_ERR}, String{}});
// 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
va_end(args);
}

View File

@ -4,6 +4,7 @@
#include "Base/Shared.hpp"
#include "Base/VecMap.hpp"
#include "Library/Worker/Job.hpp"
#include "Logger.hpp"
// ------------------------------------------------------------------------------------------------
#include <thread>
@ -162,13 +163,25 @@ private:
*/
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;
/* --------------------------------------------------------------------------------------------
* BaseJob queue.
* Finished job queue.
*/
Jobs m_FinishedJobs;
/* --------------------------------------------------------------------------------------------
* Log message queue.
*/
Logs m_Logs;
/* --------------------------------------------------------------------------------------------
* Loop state.
*/