Suppression du cache pour les messages de log, desactivation du PANIC dans malloc

This commit is contained in:
Samuel 2023-07-11 12:34:18 +02:00
parent 4f2445911d
commit 3284db23b6
2 changed files with 24 additions and 49 deletions

View File

@ -48,6 +48,7 @@ spi_nb.c)
pico_generate_pio_header(test ${CMAKE_CURRENT_LIST_DIR}/quadrature_encoder.pio)
add_definitions(-DGYRO_ADXRS453)
add_definitions(-DPICO_MALLOC_PANIC=0)
pico_enable_stdio_usb(test 1)
pico_enable_stdio_uart(test 1)
pico_add_extra_outputs(test)

72
Log.c
View File

@ -3,19 +3,13 @@
#include <stdio.h>
#include <stdlib.h>
#define LOG_CACHE_SIZE 10
struct log_message_storage{
struct Log_message_data message;
struct log_message_storage * next;
} * log_message_storage_envoi, log_message_storage_premier, *log_message_storage_courant;
struct Log_message_data log_cache[LOG_CACHE_SIZE];
uint log_cache_index_entry = 0;
uint log_cache_index_stored = 0;
uint log_error = 0;
void increment_cache_index(uint * index);
int store_new_message(struct Log_message_data message, struct log_message_storage * stockage);
void envoi_message(struct Log_message_data message);
@ -35,64 +29,44 @@ void Log_init(void){
/// @param message : string, without '\n' at the end.
/// @param log_level : can be in TELEPLOT, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
void Log_message(char * message, enum Log_level log_level){
uint temps0, temps1;
temps0 = time_us_32();
/// Création de la structure de données
struct Log_message_data message_container;
if(strlen(message) > LOG_MAX_MESSAGE_SIZE){
strcpy(log_cache[log_cache_index_entry].message, "MSG TOO LONG");
strcpy(message_container.message, "MSG TOO LONG");
}else{
strcpy(log_cache[log_cache_index_entry].message, message);
strcpy(message_container.message, message);
}
log_cache[log_cache_index_entry].log_level = log_level;
log_cache[log_cache_index_entry].timestamp = time_us_32() / 1000;
message_container.log_level = log_level;
message_container.timestamp = time_us_32() / 1000;
increment_cache_index(&log_cache_index_entry);
temps1 = time_us_32();
printf("temps tableau : %u us\n", temps1 - temps0);
// Cache overflow - est-ce pertinent ? Ne faudrait-il pas réaliser ce test avant d'incrémenter cache_index_entry ?
/*if(log_cache_index_entry == log_cache_index_stored){
log_error |= LOG_ERROR_CACHE_OVERFLOW;
}*/
/// Insertion de la structure dans la liste chaînée
struct log_message_storage* tmp_message_storage;
tmp_message_storage = (struct log_message_storage*) malloc(sizeof(struct log_message_storage));
if(tmp_message_storage != NULL){
tmp_message_storage->message = message_container;
tmp_message_storage->next = NULL;
log_message_storage_courant->next = tmp_message_storage;
log_message_storage_courant = log_message_storage_courant->next;
}else{
log_error |= LOG_ERROR_MEMORY_FULL;
}
}
/// @brief Read messages in cache, store them and send them through the serial connection
void Log_gestion(){
// Store all message from the cache
while(log_cache_index_entry != log_cache_index_stored){
store_new_message(log_cache[log_cache_index_stored], log_message_storage_courant);
increment_cache_index(&log_cache_index_stored);
log_message_storage_courant = log_message_storage_courant->next;
}
// Envoi 1 message par la liaison série
if(log_message_storage_envoi->next != NULL){
log_message_storage_envoi = log_message_storage_envoi->next;
envoi_message(log_message_storage_envoi->message);
// Si on est à la fin des messages, en envoie le statut de la fonction Log
if(log_message_storage_envoi->next == NULL){
if(log_error & LOG_ERROR_MEMORY_FULL){
printf("LOG ERROR : Memoire pleine !\n");
}
}
}
}
void increment_cache_index(uint * index){
*index = *index +1;
if(*index >= LOG_CACHE_SIZE){
*index = 0;
}
}
/// @brief Alloue l'espace pour stocker un nouveau message
/// @return 0 en cas de succès, 1 si la mémoire est pleine
int store_new_message(struct Log_message_data message, struct log_message_storage * stockage){
stockage->next = (struct log_message_storage*) malloc(sizeof(struct log_message_storage));
if(stockage->next != NULL){
stockage->next->message = message;
stockage->next->next = NULL;
return 0;
}
return 1;
}
void envoi_message(struct Log_message_data message){
printf("%u ms:%s\n", message.timestamp, message.message);
}