Skip to content

Commit

Permalink
[C]: make distinct error log default to stderr logging on allocaiton …
Browse files Browse the repository at this point in the history
…failure or full log.
  • Loading branch information
tmontgomery committed Jul 24, 2017
1 parent bfbea69 commit 33a8e24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 14 additions & 6 deletions aeron-driver/src/main/c/concurrent/aeron_distinct_error_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdatomic.h>
#include <errno.h>
#include "util/aeron_error.h"
#include "util/aeron_strutil.h"
#include "aeron_alloc.h"
#include "aeron_distinct_error_log.h"
#include "aeron_atomic.h"
Expand Down Expand Up @@ -77,8 +78,6 @@ void aeron_distinct_error_log_close(aeron_distinct_error_log_t *log)
aeron_free(log->observations_pimpl);
}

/* TODO: pre-populate OOM in distinct_error_log so that it never needs to allocate if OOMed */

static aeron_distinct_observation_t *aeron_distinct_error_log_find_observation(
aeron_distinct_observation_t *observations,
size_t num_observations,
Expand All @@ -88,7 +87,7 @@ static aeron_distinct_observation_t *aeron_distinct_error_log_find_observation(
for (size_t i = 0; i < num_observations; i++)
{
if (observations[i].error_code == error_code &&
strncmp(observations[i].description, description, AERON_MAX_PATH) == 0)
strncmp(observations[i].description, description, observations[i].description_length) == 0)
{
return &observations[i];
}
Expand Down Expand Up @@ -116,14 +115,17 @@ static aeron_distinct_observation_t *aeron_distinct_error_log_new_observation(

snprintf(encoded_error, sizeof(encoded_error) - 1, "%d: %s %s", error_code, description, message);

size_t description_length = strlen(description);
size_t encoded_error_length = strlen(encoded_error);
size_t length = AERON_ERROR_LOG_HEADER_LENGTH + encoded_error_length;
aeron_distinct_observation_t *new_array = NULL;
char *new_description = NULL;
size_t offset = log->next_offset;
aeron_error_log_entry_t *entry = (aeron_error_log_entry_t *)(log->buffer + offset);

if ((offset + length) > log->buffer_capacity ||
aeron_alloc((void **)&new_array, sizeof(aeron_distinct_observation_t) * (num_observations + 1)) < 0)
aeron_alloc((void **)&new_array, sizeof(aeron_distinct_observation_t) * (num_observations + 1)) < 0 ||
aeron_alloc((void **)&new_description, description_length + 1) < 0)
{
return NULL;
}
Expand All @@ -135,7 +137,9 @@ static aeron_distinct_observation_t *aeron_distinct_error_log_new_observation(
log->next_offset = AERON_ALIGN(offset + length, AERON_ERROR_LOG_RECORD_ALIGNMENT);

new_array[0].error_code = error_code;
new_array[0].description = strndup(description, AERON_MAX_PATH);
new_array[0].description = new_description;
strncpy(new_description, description, description_length + 1);
new_array[0].description_length = description_length;
new_array[0].offset = offset;
memcpy(&new_array[1], observations, sizeof(aeron_distinct_observation_t) * num_observations);

Expand Down Expand Up @@ -182,7 +186,11 @@ int aeron_distinct_error_log_record(

if (NULL == observation)
{
aeron_set_err(ENOMEM, "%s", strerror(ENOMEM));
char buffer[AERON_MAX_PATH];

aeron_format_date(buffer, sizeof(buffer), timestamp);
fprintf(stderr, "%s - unrecordable error %d: %s %s\n", buffer, error_code, description, message);
errno = ENOMEM;
return -1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ typedef void (*aeron_resource_linger_func_t)(uint8_t *resource);
typedef struct aeron_distinct_observation_stct
{
const char *description;
size_t offset;
int error_code;
size_t offset;
size_t description_length;
}
aeron_distinct_observation_t;

Expand Down

0 comments on commit 33a8e24

Please sign in to comment.