Skip to content

Commit

Permalink
added priority to string function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackb18b committed Oct 3, 2024
1 parent 1bd3333 commit a5d52c5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/stumpless/prival.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ STUMPLESS_PUBLIC_FUNCTION
const char *
stumpless_get_prival_string( int prival );

/**
* Gets the string representation of the given prival in the format
* "facility.level".
*
* The string returned must be freed by the caller when it is no longer
* needed to avoid memory leaks.
*
* **Thread Safety: MT-Safe**
* This function is thread safe.
*
* **Async Signal Safety: AS-Unsafe heap**
* This function is not safe to call from signal handlers due to the use of
* memory management functions.
*
* **Async Cancel Safety: AC-Unsafe heap**
* This function is not safe to call from threads that may be asynchronously
* cancelled due to the use of memory management functions.
*
* @param prival int to get the string from.
*
* @return The string representation of the given prival.
*
*/
STUMPLESS_PUBLIC_FUNCTION
const char *
stumpless_get_priority_string( int prival );

/**
* Extract PRIVAL number (Facility and Severity) from the given string with
* the direct number or with two names divided with a period in the order:
Expand Down
25 changes: 25 additions & 0 deletions src/prival.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ stumpless_get_prival_string( int prival ) {
return prival_string;
}

const char *
stumpless_get_priority_string( int prival ) {
const char *severity;
const char *facility;
size_t priority_string_size;
char* priority_string;

severity = stumpless_get_severity_string( get_severity( prival ) );
facility = stumpless_get_facility_string( get_facility( prival ) );

size_t len_severity = strlen(severity);
size_t len_facility = strlen(facility);

// +1 for '.' formatting, +1 for termination
priority_string_size = ( len_severity + len_facility + 2);
priority_string = alloc_mem( priority_string_size );

memcpy( priority_string, severity , len_severity);
memcpy( priority_string + len_severity, ".", 1);
memcpy( priority_string + len_severity + 1, facility, len_facility);
memcpy( priority_string + len_severity + 1 + len_facility, "\0", 1);

return priority_string;
}

int
stumpless_prival_from_string( const char *string ) {
int prival;
Expand Down
11 changes: 11 additions & 0 deletions test/function/prival.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,15 @@ namespace {

free( ( void * ) result );
}

TEST(GetPriorityString, ValidPrival) {
int prival;
const char *result;

prival = STUMPLESS_SEVERITY_ERR | STUMPLESS_FACILITY_USER;
result = stumpless_get_priority_string( prival );
EXPECT_STREQ( result, "STUMPLESS_SEVERITY_ERR.STUMPLESS_FACILITY_USER" );

free( ( void * ) result );
}
}

0 comments on commit a5d52c5

Please sign in to comment.