Skip to content

Commit

Permalink
change element to string format (#369)
Browse files Browse the repository at this point in the history
Changes the format of stumpless_element_to_string to be similar to
that of the logger command line tool:
`name=[param1="value1",param2="value2"]`.

This fixes #367, which can be referenced for more information.
  • Loading branch information
rmknan authored Sep 26, 2023
1 parent ce6689d commit 117ee5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions src/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ stumpless_element_to_string( const struct stumpless_element *element ) {

if( param_count != 0 ) {
// extra param list chars and commas
format_len += 6 + param_count - 1;
format_len += 4 + param_count ;
} else {
// no params, just name
format_len += 3;
Expand All @@ -229,10 +229,10 @@ stumpless_element_to_string( const struct stumpless_element *element ) {
goto fail;
}

memcpy( format + 1, name, name_len );
memcpy( format, name, name_len );

// build params list "param_1_to_string,param_2_to_string, ..."
size_t pos_offset = name_len + 4;
size_t pos_offset = name_len + 2;
for( size_t i = 0; i < param_count; i++) {
// replace '\0' with ',' at the end of each string
memcpy( format + pos_offset, params_format[i], strlen(params_format[i]));
Expand All @@ -246,17 +246,14 @@ stumpless_element_to_string( const struct stumpless_element *element ) {

unlock_element( element );

format[0] = '<';
format[name_len + 1] = '>';

if (param_count != 0 ) {
// <name>:[param_1_to_string,param_2_to_string,etc.] (with params)
format[name_len + 2] = ':';
format[name_len + 3] = '[';
// name=[param_1_to_string,param_2_to_string,etc.] (with params)
format[name_len ] = '=';
format[name_len + 1] = '[';
format[pos_offset] = ']';
} else {
// <name> (no params)
// pos_offset is name_len + 4 here
// name (no params)
// pos_offset is name_len + 2 here
pos_offset -= 3;
}

Expand Down
4 changes: 2 additions & 2 deletions test/function/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ namespace {
format = stumpless_element_to_string( element_with_params );
ASSERT_NOT_NULL( format );

EXPECT_STREQ( format, "<element-with-params>:[param-1=\"value-1\",param-2=\"value-2\"]" );
EXPECT_STREQ( format, "element-with-params=[param-1=\"value-1\",param-2=\"value-2\"]" );
EXPECT_NO_ERROR;

free( ( void * ) format );
Expand All @@ -820,7 +820,7 @@ namespace {
format = stumpless_element_to_string( basic_element );
ASSERT_NOT_NULL( format );

EXPECT_STREQ( format, "<basic-element>" );
EXPECT_STREQ( format, "basic-element" );
EXPECT_NO_ERROR;

free( ( void * ) format );
Expand Down

0 comments on commit 117ee5d

Please sign in to comment.