Skip to content

Commit

Permalink
make get message consistent with rfc
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Nov 16, 2023
1 parent b924533 commit a9b71ba
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
9 changes: 6 additions & 3 deletions include/stumpless/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ stumpless_get_entry_hostname( const struct stumpless_entry *entry );
* Note that if this message was originally set using format specifiers, the
* result will have them substituted, instead of the original placeholders.
*
* It is also important to note that the message may be NULL if the entry
* does not have one. This differs from other fields like the app id or msgid,
* which will be an RFC 5424 NILVALUE '-' if they have not been set.
*
* In versions prior to v2.0.0, the returned pointer was to the internal buffer
* used to store the name and was not to be modified by the caller. This
* behavior changed in v2.0.0 in order to avoid thread safety issues.
Expand All @@ -614,9 +618,8 @@ stumpless_get_entry_hostname( const struct stumpless_entry *entry );
*
* @param entry The entry to get the message of.
*
* @return The message of the entry if no error is encountered. If an error
* was encountered, then NULL is returned and an error code is set
* appropriately.
* @return The message of the entry (which may be NULL). If an error was
* encountered, then NULL is returned and an error code is set appropriately.
*/
STUMPLESS_PUBLIC_FUNCTION
const char *
Expand Down
9 changes: 2 additions & 7 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,8 @@ stumpless_get_entry_message( const struct stumpless_entry *entry ) {
VALIDATE_ARG_NOT_NULL( entry );

lock_entry( entry );
if( entry->message_length == 0 ) {
message_copy = alloc_mem( 2 );
if( !message_copy ) {
goto cleanup_and_return;
}
message_copy[0] = '-';
message_copy[1] = '\0';
if( !entry->message ) {
message_copy = NULL;
} else {
message_copy = alloc_mem( entry->message_length + 1 );
if( !message_copy ) {
Expand Down
6 changes: 3 additions & 3 deletions src/target/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ stumpless_sqlite3_prepare( const struct stumpless_entry *entry,
}

if( message_index != 0 ) {
if( entry->message_length == 1 && entry->message[0] == '-' ) {
sql_result = sqlite3_bind_null( insert_stmt, message_index );
} else {
if( entry->message ) {
sql_result = sqlite3_bind_text( insert_stmt, message_index, entry->message, entry->message_length, SQLITE_STATIC );
} else {
sql_result = sqlite3_bind_null( insert_stmt, message_index );
}
if( sql_result != SQLITE_OK ) {
raise_sqlite3_error( "could not bind the message to the statement", sql_result ); // TODO l10n
Expand Down
5 changes: 1 addition & 4 deletions test/function/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,8 @@ namespace {
const char *message;

message = stumpless_get_entry_message( nil_entry );
EXPECT_NOT_NULL( message );
EXPECT_NULL( message );
EXPECT_NO_ERROR;

EXPECT_STREQ( message, "-" );
free( ( void * ) message );
}

TEST_F( EntryTest, GetParamByIndex ) {
Expand Down
9 changes: 4 additions & 5 deletions test/function/target/sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,17 @@ TestEntryInDatabase( sqlite3 *db, std::string const &table_name, const struct st
"FROM " << table_name;

expected_message = stumpless_get_entry_message( entry );
EXPECT_NOT_NULL( expected_message );
if( strcmp( expected_message, "-" ) == 0 ) {
query_stream << " WHERE message IS NULL";
} else {
if( expected_message ) {
query_stream << " WHERE message = ?";
} else {
query_stream << " WHERE message IS NULL";
}
std::string result_query = query_stream.str();

sql_result = sqlite3_prepare_v2( db, result_query.c_str(), -1, &result_stmt, NULL );
EXPECT_EQ( sql_result, SQLITE_OK );

if( strcmp( expected_message, "-" ) != 0 ) {
if( expected_message ) {
sql_result = sqlite3_bind_text( result_stmt,
1,
expected_message,
Expand Down

0 comments on commit a9b71ba

Please sign in to comment.