perf: updated error checks
This commit is contained in:
1 parent
a39d9d40cf
commit
15a9db7342
3 files changed
+50
-36
No files matched your search
+33
-15
@@ -13,19 +13,22 @@
|
||||
|
||||
static const char *TAG = "zh_syslog";
|
||||
|
||||
static zh_syslog_init_config_t _init_config = {0}; /*!< Syslog client initialization configuration (internal copy) */
|
||||
static struct sockaddr_in _syslog_server = {0}; /*!< Pre-configured syslog server address */
|
||||
volatile static int _socket = 0; /*!< UDP socket file descriptor */
|
||||
volatile static bool _is_initialized = false; /*!< Initialization status flag */
|
||||
static struct sockaddr_in _syslog_server = {0};
|
||||
volatile static int _socket = -1;
|
||||
volatile static bool _is_initialized = false;
|
||||
|
||||
esp_err_t zh_syslog_init(const zh_syslog_init_config_t *config)
|
||||
{
|
||||
ESP_LOGI(TAG, "Syslog client initialization begin.");
|
||||
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument.");
|
||||
_init_config = *config;
|
||||
_syslog_server.sin_addr.s_addr = inet_addr(_init_config.syslog_ip);
|
||||
ZH_ERROR_CHECK(_is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "Syslog client initialization fail. Syslog client already initialized.");
|
||||
ZH_ERROR_CHECK(config->syslog_port != 0 && config->syslog_port <= 65535, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument.");
|
||||
ZH_ERROR_CHECK(memchr(config->syslog_ip, '\0', sizeof(config->syslog_ip)) != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid IP.");
|
||||
struct in_addr addr;
|
||||
ZH_ERROR_CHECK(inet_pton(AF_INET, config->syslog_ip, &addr) == 1, ESP_ERR_INVALID_ARG, NULL, "Syslog client initialization fail. Invalid argument.");
|
||||
_syslog_server.sin_addr = addr;
|
||||
_syslog_server.sin_family = AF_INET;
|
||||
_syslog_server.sin_port = htons(_init_config.syslog_port);
|
||||
_syslog_server.sin_port = htons((uint16_t)config->syslog_port);
|
||||
_socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
ZH_ERROR_CHECK(_socket >= 0, ESP_FAIL, NULL, "Syslog client initialization fail. Create socket error.");
|
||||
_is_initialized = true;
|
||||
@@ -37,24 +40,39 @@ esp_err_t zh_syslog_deinit(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Syslog client deinitialization begin.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client deinitialization fail. Syslog client not initialized.");
|
||||
shutdown(_socket, 0);
|
||||
close(_socket);
|
||||
_socket = -1;
|
||||
_is_initialized = false;
|
||||
ESP_LOGI(TAG, "Syslog client deinitialization success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_syslog_send(const zh_syslog_facility_code_t facility, const zh_syslog_severity_code_t severity, const char *hostname, const char *app_name, const char *message) // -V2008
|
||||
esp_err_t zh_syslog_send(const zh_syslog_facility_code_t facility, const zh_syslog_severity_code_t severity, const char *hostname, const char *app_name, const char *message)
|
||||
{
|
||||
ESP_LOGI(TAG, "Syslog client send begin.");
|
||||
ZH_ERROR_CHECK(hostname != NULL && app_name != NULL && message != NULL, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument.");
|
||||
ZH_ERROR_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Syslog client send fail. Syslog client not initialized.");
|
||||
char *payload = heap_caps_calloc(1, 1024, MALLOC_CAP_8BIT);
|
||||
ZH_ERROR_CHECK(payload != NULL, ESP_ERR_NO_MEM, NULL, "Syslog client send fail. Memory allocation fail or no free memory in the heap.");
|
||||
sprintf(payload, "<%d>1 - %s %s - - - %s", ((uint8_t)facility * 8) + (uint8_t)severity, hostname, app_name, message);
|
||||
int temp = sendto(_socket, payload, strlen(payload), 0, (struct sockaddr *)&_syslog_server, sizeof(_syslog_server)); // -V641
|
||||
ZH_ERROR_CHECK(temp >= 0, ESP_FAIL, heap_caps_free(payload), "Syslog client send fail. Socket error.");
|
||||
heap_caps_free(payload);
|
||||
ZH_ERROR_CHECK(facility >= ZH_KERN && facility <= ZH_LOCAL7 && severity >= ZH_EMERG && severity <= ZH_DEBUG, ESP_ERR_INVALID_ARG, NULL, "Syslog client send fail. Invalid argument.");
|
||||
const int pri = ((uint8_t)facility * 8) + (uint8_t)severity;
|
||||
char stack_buf[256];
|
||||
int len = snprintf(stack_buf, sizeof(stack_buf), "<%d>1 - %s %s - - - %s", pri, hostname, app_name, message);
|
||||
ZH_ERROR_CHECK(len >= 0, ESP_FAIL, NULL, "Syslog client send fail. Format error.");
|
||||
char *payload = stack_buf;
|
||||
bool heap_allocated = false;
|
||||
if (len >= sizeof(stack_buf))
|
||||
{
|
||||
payload = heap_caps_calloc(1, len + 1, MALLOC_CAP_8BIT);
|
||||
ZH_ERROR_CHECK(payload != NULL, ESP_ERR_NO_MEM, NULL, "Syslog client send fail. Memory allocation fail or no free memory in the heap.");
|
||||
int written = snprintf(payload, len + 1, "<%d>1 - %s %s - - - %s", pri, hostname, app_name, message);
|
||||
ZH_ERROR_CHECK(written == len, ESP_FAIL, heap_caps_free(payload), "Syslog client send fail. Format error.");
|
||||
heap_allocated = true;
|
||||
}
|
||||
int sent = sendto(_socket, payload, len, 0, (struct sockaddr *)&_syslog_server, sizeof(_syslog_server));
|
||||
if (heap_allocated == true)
|
||||
{
|
||||
heap_caps_free(payload);
|
||||
}
|
||||
ZH_ERROR_CHECK(sent == len, ESP_FAIL, NULL, "Syslog client send fail. Socket error.");
|
||||
ESP_LOGI(TAG, "Syslog client send success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in new issue
Block a user