mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-10 12:59:25 +02:00
including mutex protection
This commit is contained in:
parent
65586d051f
commit
20a1d6598b
@ -29,6 +29,7 @@
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include <pthread.h>
|
||||
|
||||
static const char * TAG = "http_server";
|
||||
|
||||
@ -530,8 +531,12 @@ esp_err_t POST_OTA_update(httpd_req_t * req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
void log_to_websocket(const char * format, va_list args)
|
||||
{
|
||||
pthread_mutex_lock(&log_mutex);
|
||||
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
@ -540,9 +545,9 @@ void log_to_websocket(const char * format, va_list args)
|
||||
va_end(args_copy);
|
||||
|
||||
// Allocate the buffer dynamically
|
||||
char * log_buffer = (char *) malloc(needed_size);
|
||||
char * log_buffer = (char *) calloc(needed_size + 2, sizeof(char)); // +2 for potential \n and \0
|
||||
if (log_buffer == NULL) {
|
||||
// Handle allocation failure
|
||||
pthread_mutex_unlock(&log_mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -551,32 +556,36 @@ void log_to_websocket(const char * format, va_list args)
|
||||
vsnprintf(log_buffer, needed_size, format, args_copy);
|
||||
va_end(args_copy);
|
||||
|
||||
// Ensure the log message ends with a newline
|
||||
size_t len = strlen(log_buffer);
|
||||
if (len > 0 && log_buffer[len - 1] != '\n') {
|
||||
log_buffer[len] = '\n';
|
||||
log_buffer[len + 1] = '\0';
|
||||
len++;
|
||||
}
|
||||
|
||||
// Prepare the WebSocket frame
|
||||
httpd_ws_frame_t ws_pkt;
|
||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||
ws_pkt.payload = (uint8_t *) log_buffer;
|
||||
ws_pkt.len = strlen(log_buffer);
|
||||
ws_pkt.len = len;
|
||||
ws_pkt.type = HTTPD_WS_TYPE_TEXT;
|
||||
|
||||
// Print to standard output
|
||||
va_copy(args_copy, args);
|
||||
vprintf(format, args_copy);
|
||||
va_end(args_copy);
|
||||
printf("%s", log_buffer);
|
||||
|
||||
// Ensure server and fd are valid
|
||||
if (server == NULL || fd < 0) {
|
||||
// Handle invalid server or socket descriptor
|
||||
free(log_buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
// Send the WebSocket frame asynchronously
|
||||
if (httpd_ws_send_frame_async(server, fd, &ws_pkt) != ESP_OK) {
|
||||
esp_log_set_vprintf(vprintf);
|
||||
if (server != NULL && fd >= 0) {
|
||||
// Send the WebSocket frame asynchronously
|
||||
if (httpd_ws_send_frame_async(server, fd, &ws_pkt) != ESP_OK) {
|
||||
esp_log_set_vprintf(vprintf);
|
||||
}
|
||||
}
|
||||
|
||||
// Free the allocated buffer
|
||||
free(log_buffer);
|
||||
|
||||
pthread_mutex_unlock(&log_mutex);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user