Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #450: Adds new function for generating a shorter form prival string. #457

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, facility , len_facility);
priority_string[len_severity] = '.';
memcpy( priority_string + len_facility + 1, severity, len_severity);
priority_string[priority_string_size-1] = "\0";

return priority_string;
}

int
stumpless_prival_from_string( const char *string ) {
int prival;
Expand Down
1 change: 1 addition & 0 deletions src/windows/stumpless.def
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,4 @@ EXPORTS

stumpless_get_prival_string @230
stumpless_set_severity_color @231
stumpless_get_priority_string @232
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_FACILITY_USER.STUMPLESS_SEVERITY_ERR" );

free( ( void * ) result );
}
}
Loading