logging: don't ask...

This commit is contained in:
Vasily Davydov 2023-05-12 13:59:49 +03:00
parent 398030afe8
commit 2c3e1a8dc8
13 changed files with 150 additions and 65 deletions

View File

@ -53,6 +53,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/user_interface}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/user_interface}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/threads/rotary}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/threads/rotary}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/threads/manager}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/threads/manager}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/threads/logging}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/peripherals}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/peripherals}&quot;"/>
</option> </option>
<option id="com.crt.advproject.cpp.misc.dialect.4036734" name="Language standard" superClass="com.crt.advproject.cpp.misc.dialect" useByScannerDiscovery="true" value="com.crt.advproject.misc.dialect.cpp17" valueType="enumerated"/> <option id="com.crt.advproject.cpp.misc.dialect.4036734" name="Language standard" superClass="com.crt.advproject.cpp.misc.dialect" useByScannerDiscovery="true" value="com.crt.advproject.misc.dialect.cpp17" valueType="enumerated"/>
@ -86,6 +87,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/master}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/master}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/user_interface}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/user_interface}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/peripherals}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/peripherals}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/src/threads/logging}&quot;"/>
</option> </option>
<option id="com.crt.advproject.c.misc.dialect.82852045" name="Language standard" superClass="com.crt.advproject.c.misc.dialect" useByScannerDiscovery="true" value="com.crt.advproject.misc.dialect.c17" valueType="enumerated"/> <option id="com.crt.advproject.c.misc.dialect.82852045" name="Language standard" superClass="com.crt.advproject.c.misc.dialect" useByScannerDiscovery="true" value="com.crt.advproject.misc.dialect.c17" valueType="enumerated"/>
<inputType id="com.crt.advproject.compiler.input.765511076" superClass="com.crt.advproject.compiler.input"/> <inputType id="com.crt.advproject.compiler.input.765511076" superClass="com.crt.advproject.compiler.input"/>

View File

@ -7,50 +7,21 @@
#include "ThreadCommon.h" #include "ThreadCommon.h"
#include "Master.h" #include "Master.h"
#include "Rotary.h"
#include "Manager.h"
#include "UserInterface.h"
#include "Log.h"
int main(void) int main(void)
{ {
LOG_INFO("Starting system init");
SystemCoreClockUpdate(); SystemCoreClockUpdate();
Board_Init(); Board_Init();
retarget_init(); retarget_init();
LOG_INFO("System init done"); ThreadCommon::CommonManagers manager = {new ThreadCommon::ThreadManager,
new ThreadCommon::QueueManager};
ThreadCommon::ThreadManager* manager = new ThreadCommon::ThreadManager; manager.tm->createTask(thread_master, "master",
ThreadCommon::QueueManager* qmanager = new ThreadCommon::QueueManager;
LOG_INFO("Creating queues");
qmanager->createQueue(100,
sizeof(Event),
ThreadCommon::QueueManager::master_event_all);
qmanager->createQueue(20,
sizeof(Event),
ThreadCommon::QueueManager::manager_event_master);
qmanager->createQueue(20,
sizeof(UserInterface::InterfaceWithData),
ThreadCommon::QueueManager::ui_event_manager);
LOG_INFO("Creating tasks");
manager->createTask(thread_master, "master",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL, configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(qmanager)); static_cast<void*>(&manager));
manager->createTask(thread_manager, "manager",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(qmanager));
manager->createTask(thread_rotary, "rotary",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(qmanager));
manager->createTask(thread_user_interface, "user_interface",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(qmanager));
LOG_INFO("Start the real time kernel with preemption");
vTaskStartScheduler (); vTaskStartScheduler ();
return 1; return 1;

View File

@ -13,14 +13,11 @@ ThreadCommon::QueueManager::QueueManager() {}
bool ThreadCommon::QueueManager::createQueue(size_t queue_length, size_t item_size, Queue_id qid) bool ThreadCommon::QueueManager::createQueue(size_t queue_length, size_t item_size, Queue_id qid)
{ {
QueueHandle_t queue_to_create; QueueHandle_t queue_to_create;
LOG_DEBUG("Creating queue with id %d", qid);
if ((queue_to_create = xQueueCreate(queue_length, item_size))) if ((queue_to_create = xQueueCreate(queue_length, item_size)))
{ {
LOG_DEBUG("Queue with id %d has been created", qid);
queues.insert({qid, queue_to_create}); queues.insert({qid, queue_to_create});
return true; return true;
} }
LOG_ERROR("Failed to cerate queue with id %d", qid);
return false; return false;
} }

View File

@ -18,6 +18,7 @@
namespace ThreadCommon namespace ThreadCommon
{ {
enum RotaryAction enum RotaryAction
{ {
Right, Right,
@ -45,7 +46,8 @@ namespace ThreadCommon
master_event_all, master_event_all,
relay_event_master, relay_event_master,
manager_event_master, manager_event_master,
ui_event_manager ui_event_manager,
logging_message_all
}; };
QueueManager(); QueueManager();
~QueueManager() = default; ~QueueManager() = default;
@ -70,6 +72,12 @@ namespace ThreadCommon
std::map <Queue_id, QueueHandle_t> queues; std::map <Queue_id, QueueHandle_t> queues;
}; };
typedef struct _CommonManagers
{
ThreadManager * tm;
QueueManager * qm;
} CommonManagers;
/* global variables */ /* global variables */
/* 'receiver'_'what'_'sender'_q */ /* 'receiver'_'what'_'sender'_q */
/* /*

View File

@ -1,5 +1,4 @@
#include "ThreadCommon.h" #include "ThreadCommon.h"
#include "Log.h"
ThreadCommon::ThreadManager::ThreadManager(){} ThreadCommon::ThreadManager::ThreadManager(){}
@ -11,18 +10,12 @@ bool ThreadCommon::ThreadManager::createTask(void (*task_func)(void*),
void* parameters) void* parameters)
{ {
const char * t_name = name.c_str(); const char * t_name = name.c_str();
LOG_DEBUG("Creating task [name: %s, priority: %ld, stack: %ld]",
t_name, priority, stack_size);
BaseType_t taskCreated = xTaskCreate(task_func, BaseType_t taskCreated = xTaskCreate(task_func,
t_name, t_name,
stack_size, stack_size,
parameters, parameters,
priority, priority,
NULL); NULL);
if (!(taskCreated == pdPASS)) assert(taskCreated == pdPASS);
{
LOG_ERROR("Failed to create a task [name: %s, priority: %ld, stack: %ld]",
t_name, priority, stack_size)
}
return (taskCreated == pdPASS); return (taskCreated == pdPASS);
} }

View File

@ -5,13 +5,11 @@
#include "board.h" #include "board.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
/* extern QueueHandle_t logging_queue;
This simlpe logging framework is dependant
on std lib's multithread support, thus
if needed on different platforms, please
wrap it with mutexes.
*/
/* ================= Settings ================== */ /* ================= Settings ================== */
#define LOG_COLORED_OUTPUT #define LOG_COLORED_OUTPUT
@ -44,7 +42,9 @@
#define _LOG_STREAMOUT(message, message_length) \ #define _LOG_STREAMOUT(message, message_length) \
INT_ASSERT(message_length > 0); \ INT_ASSERT(message_length > 0); \
printf("%.*s\n", message_length, message); \ if (logging_queue) { \
xQueueSend(logging_queue, (void*)message, portMAX_DELAY); \
}
static void create_log_line(const char * _status, static void create_log_line(const char * _status,
const char * _location, const char * _location,
@ -69,17 +69,17 @@ static void create_log_line(const char * _status,
} }
#define LOG_INFO(fmt, ...) \ #define LOG_INFO(fmt, ...) \
create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#define LOG_WARNING(fmt, ...) \ #define LOG_WARNING(fmt, ...) \
create_log_line(C_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ create_log_line(C_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#define LOG_ERROR(fmt, ...) \ #define LOG_ERROR(fmt, ...) \
create_log_line(C_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ create_log_line(C_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#if LOG_DEBUG_MESSAGES #if LOG_DEBUG_MESSAGES
#define LOG_DEBUG(fmt, ...) \ #define LOG_DEBUG(fmt, ...) \
create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ create_log_line(C_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__);
#else #else
#define LOG_DEBUG(fmt, ...) #define LOG_DEBUG(fmt, ...)
#endif #endif

View File

@ -0,0 +1,35 @@
/*
* Logging.cpp
*
* Created on: 12 May 2023
* Author: tylen
*/
#include "Logging.h"
#include "ThreadCommon.h"
#include "Log.h"
#include "queue.h"
Logging::Logging(ThreadCommon::QueueManager * qm) : _qm(qm) {
q = _qm->getQueue(ThreadCommon::QueueManager::logging_message_all);
}
Logging::~Logging() {
// TODO Auto-generated destructor stub
}
void Logging::taskFunction()
{
char data[LOG_BUFFER_MAX_CAP] = {0};
xQueueReceive(q, static_cast<char*>(data), portMAX_DELAY);
mutex.lock();
printf("%s\n", data);
mutex.unlock();
}
void thread_logging(void* pvParams)
{
ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
Logging log(manager->qm);
log.taskFunction();
}

View File

@ -0,0 +1,27 @@
/*
* Logging.h
*
* Created on: 12 May 2023
* Author: tylen
*/
#ifndef THREADS_LOGGING_LOGGING_H_
#define THREADS_LOGGING_LOGGING_H_
#include "Fmutex.h"
#include "ThreadCommon.h"
class Logging {
public:
Logging(ThreadCommon::QueueManager * qm);
virtual ~Logging();
void taskFunction();
private:
ThreadCommon::QueueManager * _qm;
QueueHandle_t q;
Fmutex mutex;
};
void thread_logging(void* pvParams);
#endif /* THREADS_LOGGING_LOGGING_H_ */

View File

@ -52,6 +52,7 @@ void Manager::taskFunction()
void thread_manager(void* pvParams) void thread_manager(void* pvParams)
{ {
Manager m(static_cast<ThreadCommon::QueueManager*>(pvParams)); ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
Manager m(manager->qm);
m.taskFunction(); m.taskFunction();
} }

View File

@ -7,6 +7,13 @@
#include "Master.h" #include "Master.h"
#include "Log.h" #include "Log.h"
#include "ThreadCommon.h"
#include "Rotary.h"
#include "Manager.h"
#include "Logging.h"
#include "UserInterface.h"
#include "queue.h"
#include "Logging.h"
static const char* rotary_direction[] = static const char* rotary_direction[] =
{ {
@ -16,6 +23,8 @@ static const char* rotary_direction[] =
"Idle" "Idle"
}; };
QueueHandle_t logging_queue;
Master::Master(ThreadCommon::QueueManager* qm) : _qm(qm) Master::Master(ThreadCommon::QueueManager* qm) : _qm(qm)
{ {
LOG_DEBUG("Creating Master"); LOG_DEBUG("Creating Master");
@ -75,6 +84,44 @@ void Master::taskFunction() {
void thread_master(void* pvParams) { void thread_master(void* pvParams) {
Master m(static_cast<ThreadCommon::QueueManager*>(pvParams)); ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
manager->qm->createQueue(50,
LOG_BUFFER_MAX_CAP,
ThreadCommon::QueueManager::logging_message_all);
logging_queue = manager->qm->getQueue(ThreadCommon::QueueManager::logging_message_all);
manager->tm->createTask(thread_logging, "logging",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager));
LOG_INFO("Logging Active");
LOG_INFO("Started the real time kernel with preemption");
LOG_INFO("Master Started");
Master m(manager->qm);
LOG_INFO("Master is creating queues");
manager->qm->createQueue(100,
sizeof(Event),
ThreadCommon::QueueManager::master_event_all);
manager->qm->createQueue(20,
sizeof(Event),
ThreadCommon::QueueManager::manager_event_master);
manager->qm->createQueue(20,
sizeof(UserInterface::InterfaceWithData),
ThreadCommon::QueueManager::ui_event_manager);
LOG_INFO("Master is creating tasks");
manager->tm->createTask(thread_master, "master",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager));
manager->tm->createTask(thread_manager, "manager",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager));
manager->tm->createTask(thread_rotary, "rotary",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager));
manager->tm->createTask(thread_user_interface, "user_interface",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager));
m.taskFunction(); m.taskFunction();
} }

View File

@ -10,6 +10,8 @@
#include "chip.h" #include "chip.h"
#include "board.h" #include "board.h"
#include "FreeRTOS.h"
#include "task.h"
#include "ThreadCommon.h" #include "ThreadCommon.h"
#include "Event.h" #include "Event.h"
#include "task.h" #include "task.h"

View File

@ -86,6 +86,7 @@ void thread_rotary(void* pvParams)
QueueHandle_t rotary_isr_q = xQueueCreate(15, sizeof(char)); QueueHandle_t rotary_isr_q = xQueueCreate(15, sizeof(char));
p_rotary_isr_q = &rotary_isr_q; p_rotary_isr_q = &rotary_isr_q;
Rotary r(static_cast<ThreadCommon::QueueManager*>(pvParams)); ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
Rotary r(manager->qm);
r.taskFunction(); r.taskFunction();
} }

View File

@ -60,11 +60,11 @@ void UserInterface::handleEvent(InterfaceWithData* ui_data)
void UserInterface::handleLCD(LiquidCrystal *lcd, const char *str) void UserInterface::handleLCD(LiquidCrystal *lcd, const char *str)
{ {
//Interpret empty string as clear. //Interpret empty string as clear.
if(!strlen(str)) if(!strlen(str)){
lcd->clear(); lcd->clear();
LOG_INFO("Clear up LCD"); LOG_INFO("Clear up LCD");
//Print the text otherwise. //Print the text otherwise.
else }else
{ {
lcd->setCursor(0, 0); lcd->setCursor(0, 0);
lcd->print(str); lcd->print(str);
@ -97,6 +97,7 @@ void UserInterface::initLCD1()
void thread_user_interface(void* pvParams) void thread_user_interface(void* pvParams)
{ {
UserInterface ui(static_cast<ThreadCommon::QueueManager*>(pvParams)); ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
UserInterface ui(manager->qm);
ui.taskFunction(); ui.taskFunction();
} }