From 4ed5f17fc06d8ac3c53377cbc6c1221222926276 Mon Sep 17 00:00:00 2001 From: Joel Date: Sun, 30 Jun 2024 21:37:41 -0400 Subject: [PATCH] merge priority and prival files The functions for working with priority values were spread across two source files name "priority" and "prival". This change consolidates them to the "prival" files so that they are located in a single file named consistently with existing struct members. --- CITATION.cff | 4 +- CMakeLists.txt | 13 +- ChangeLog.md | 5 + include/private/entry.h | 30 +--- include/private/prival.h | 55 +++++++ include/stumpless.h | 3 +- include/stumpless/priority.h | 66 -------- include/stumpless/prival.h | 38 ++++- src/entry.c | 7 +- src/priority.c | 114 -------------- src/prival.c | 104 ++++++++++++- src/target.c | 1 + test/function/priority.cpp | 144 ------------------ test/function/prival.cpp | 118 +++++++++++++- test/performance/{priority.cpp => prival.cpp} | 11 +- tools/check_headers/stumpless.yml | 5 +- tools/check_headers/stumpless_private.yml | 1 + tools/cmake/install_headers.cmake | 1 - 18 files changed, 334 insertions(+), 386 deletions(-) create mode 100644 include/private/prival.h delete mode 100644 include/stumpless/priority.h delete mode 100644 src/priority.c delete mode 100644 test/function/priority.cpp rename test/performance/{priority.cpp => prival.cpp} (80%) diff --git a/CITATION.cff b/CITATION.cff index 17ea01a05..5a523fdd0 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 title: Stumpless Logging Library license: Apache-2.0 repository-code: "https://github.com/goatshriek/stumpless" -version: 2.2.0 -date-released: 2024-05-26 +version: 3.0.0 +date-released: 2024-06-30 keywords: - journald - logging diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a7d9ac51..c485c5778 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ if(POLICY CMP0135) endif() project(stumpless - VERSION 2.2.0 + VERSION 3.0.0 ) set(CMAKE_PROJECT_HOMEPAGE_URL "https://goatshriek.github.io/stumpless/") @@ -226,6 +226,7 @@ set(STUMPLESS_SOURCES ${PROJECT_SOURCE_DIR}/src/log.c ${PROJECT_SOURCE_DIR}/src/memory.c ${PROJECT_SOURCE_DIR}/src/param.c + ${PROJECT_SOURCE_DIR}/src/prival.c ${PROJECT_SOURCE_DIR}/src/severity.c ${PROJECT_SOURCE_DIR}/src/strbuilder.c ${PROJECT_SOURCE_DIR}/src/strhelper.c @@ -236,8 +237,6 @@ set(STUMPLESS_SOURCES ${PROJECT_SOURCE_DIR}/src/target/stream.c ${PROJECT_SOURCE_DIR}/src/version.c ${PROJECT_SOURCE_DIR}/src/validate.c - ${PROJECT_SOURCE_DIR}/src/priority.c - ${PROJECT_SOURCE_DIR}/src/prival.c ) @@ -1081,10 +1080,6 @@ add_function_test(version SOURCES ${PROJECT_SOURCE_DIR}/test/function/version.cpp ) -add_function_test(priority - SOURCES ${PROJECT_SOURCE_DIR}/test/function/priority.cpp -) - add_function_test(prival SOURCES ${PROJECT_SOURCE_DIR}/test/function/prival.cpp ) @@ -1138,8 +1133,8 @@ add_performance_test(element SOURCES test/performance/element.cpp ) -add_performance_test(priority -SOURCES test/performance/priority.cpp +add_performance_test(prival + SOURCES "${PROJECT_SOURCE_DIR}/test/performance/prival.cpp" ) add_performance_test(entry diff --git a/ChangeLog.md b/ChangeLog.md index b2f8ec171..265ae8fdb 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -9,6 +9,11 @@ fixes, check out the [roadmap](https://github.com/goatshriek/stumpless/blob/master/docs/roadmap.md). +## [3.0.0] - 2024-06-30 +### Removed + - `stumpless/priority.h`, which was merged into `stumpless/prival.h`. + + ## [2.2.0] - 2024-05-26 ### Added - @since format check enforcement in CI pipeline. diff --git a/include/private/entry.h b/include/private/entry.h index a0bd5e6b6..262e23352 100644 --- a/include/private/entry.h +++ b/include/private/entry.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright 2018-2023 Joel E. Anderson + * Copyright 2018-2024 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,34 +46,6 @@ void entry_free_all( void ); -/** - * Gets the priority value from facility and severity parameters. - * - * Priority value is calculated by left shifting the facility value by 3 - * and adding it to the severity value which is according to the - * RFC 5424 Section 6.2.1. The shift operation is done prior to get_prival() - * function with macros in "facility.h" - * - * **Thread Safety: MT-Safe** - * This function is thread safe. - * - * **Async Signal Safety: AS-Safe ** - * This function must be safe to call from signal handlers - * - * **Async Cancel Safety: AC-Safe** - * This function must be safe to call from threads that may be asynchronously - * cancelled. - * - * @param facility Facility value. This should be a \c STUMPLESS_FACILITY value. - * - * @param severity Severity value. This should be a \c STUMPLESS_SEVERITY value - * - * @return Priority value - */ -int -get_prival( enum stumpless_facility facility, - enum stumpless_severity severity ); - /** * Locks the mutex within the entry. * diff --git a/include/private/prival.h b/include/private/prival.h new file mode 100644 index 000000000..4e35bbf30 --- /dev/null +++ b/include/private/prival.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* + * Copyright 2018-2024 Joel E. Anderson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __STUMPLESS_PRIVATE_PRIVAL_H +#define __STUMPLESS_PRIVATE_PRIVAL_H + +#include +#include + +/** + * Gets the priority value from facility and severity parameters. + * + * Priority value is calculated by left shifting the facility value by 3 + * and adding it to the severity value which is according to the + * RFC 5424 Section 6.2.1. The shift operation is done prior to get_prival() + * function with macros in "facility.h" + * + * Moved from "private/entry.h" to "private/prival.h" in release v3.0.0. + * + * **Thread Safety: MT-Safe** + * This function is thread safe. + * + * **Async Signal Safety: AS-Safe ** + * This function must be safe to call from signal handlers + * + * **Async Cancel Safety: AC-Safe** + * This function must be safe to call from threads that may be asynchronously + * cancelled. + * + * @param facility Facility value. This should be a \c STUMPLESS_FACILITY value. + * + * @param severity Severity value. This should be a \c STUMPLESS_SEVERITY value + * + * @return Priority value + */ +int +get_prival( enum stumpless_facility facility, + enum stumpless_severity severity ); + +#endif /* __STUMPLESS_PRIVATE_PRIVAL_H */ diff --git a/include/stumpless.h b/include/stumpless.h index dba9eed4b..3eb547ee1 100644 --- a/include/stumpless.h +++ b/include/stumpless.h @@ -196,6 +196,7 @@ #include #include #include +#include #include #include #include @@ -204,8 +205,6 @@ #include #include #include -#include -#include #ifdef STUMPLESS_CHAIN_TARGETS_SUPPORTED /** @example chain_example.c diff --git a/include/stumpless/priority.h b/include/stumpless/priority.h deleted file mode 100644 index d924bba86..000000000 --- a/include/stumpless/priority.h +++ /dev/null @@ -1,66 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ - -/* - * Copyright 2022-2024 Joel E. Anderson - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** @file - * Priority value (PRIVAL) represents both the Facility and Severity values. - * - * @since release v2.2.0 - */ - -#ifndef __STUMPLESS_PRIORITY_H -#define __STUMPLESS_PRIORITY_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * 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: - * facility first, then severity ("."). - * - * **Thread Safety: MT-Safe** - * This function is thread safe. A mutex is used to coordinate changes to the - * target while it is being read. - * - * **Async Signal Safety: AS-Unsafe lock** - * This function is not safe to call from signal handlers due to the use of a - * non-reentrant lock to coordinate the read of the target. - * - * **Async Cancel Safety: AC-Unsafe lock** - * This function is not safe to call from threads that may be asynchronously - * cancelled, due to the use of a lock that could be left locked.. - * - * @since release v2.2.0 - * - * @param string The string to extract the prival from. - * - * @return the PRIVAL number used for the severity and facility values of - * the logged entry, in the event of an error it returns -1. - */ -STUMPLESS_PUBLIC_FUNCTION -int -stumpless_prival_from_string( const char *string ); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* __STUMPLESS_PRIORITY_H */ diff --git a/include/stumpless/prival.h b/include/stumpless/prival.h index a68673e71..b369312ae 100644 --- a/include/stumpless/prival.h +++ b/include/stumpless/prival.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright 2024 Joel E. Anderson + * Copyright 2022-2024 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,8 @@ */ /** @file - * Functions for working with privals. + * Functions for working with privals, which contain both facility and + * severity values. * * @since release v2.2.0 */ @@ -30,6 +31,7 @@ #ifdef __cplusplus extern "C" { #endif + /** * Gets the string representation of the given prival. * @@ -57,6 +59,38 @@ STUMPLESS_PUBLIC_FUNCTION const char * stumpless_get_prival_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: + * facility first, then severity ("."). + * + * In release v2.2.0 this function was in a separate header + * "stumpless/priority.h". This header was consolidated into + * "stumpless/prival.h" in release v3.0.0. + * + * **Thread Safety: MT-Safe** + * This function is thread safe. A mutex is used to coordinate changes to the + * target while it is being read. + * + * **Async Signal Safety: AS-Unsafe lock** + * This function is not safe to call from signal handlers due to the use of a + * non-reentrant lock to coordinate the read of the target. + * + * **Async Cancel Safety: AC-Unsafe lock** + * This function is not safe to call from threads that may be asynchronously + * cancelled, due to the use of a lock that could be left locked.. + * + * @since release v2.2.0 + * + * @param string The string to extract the prival from. + * + * @return the PRIVAL number used for the severity and facility values of + * the logged entry, in the event of an error it returns -1. + */ +STUMPLESS_PUBLIC_FUNCTION +int +stumpless_prival_from_string( const char *string ); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/entry.c b/src/entry.c index 975cad181..db63bc6c1 100644 --- a/src/entry.c +++ b/src/entry.c @@ -39,6 +39,7 @@ #include "private/entry.h" #include "private/error.h" #include "private/facility.h" +#include "private/prival.h" #include "private/severity.h" #include "private/strbuilder.h" #include "private/strhelper.h" @@ -1220,12 +1221,6 @@ entry_free_all( void ) { entry_cache = NULL; } -int -get_prival( enum stumpless_facility facility, - enum stumpless_severity severity ) { - return facility | severity; -} - void lock_entry( const struct stumpless_entry *entry ) { config_lock_mutex( entry->mutex ); diff --git a/src/priority.c b/src/priority.c deleted file mode 100644 index 78e4f15dc..000000000 --- a/src/priority.c +++ /dev/null @@ -1,114 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -/* - * Copyright 2022-2024 Joel E. Anderson - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include "private/memory.h" -#include "private/entry.h" -#include "private/validate.h" -#include "private/config.h" -#include "private/strhelper.h" -#include "private/error.h" -#include "private/config/wrapper/locale.h" - -int -stumpless_prival_from_string( const char *string ) { - int prival; - int severity; - int facility; - char *param; - char *period; - char *sec_period; - size_t len; - size_t slen; - - VALIDATE_ARG_NOT_NULL_INT_RETURN( string ); - - if( unlikely( !string[0] ) ) { - raise_argument_empty( L10N_NULL_ARG_ERROR_MESSAGE( "string" ) ); - return -1; - } - - slen = strlen( string ); - - if( isdigit( string[0] ) ) { - prival = atoi( string ); - if( prival <= 191 && slen < 4 ) { - return prival; - } - } - - // find the first period character - period = strchr( string, '.' ); - if( !period ) { - raise_invalid_param( ); - return -1; - } - - // check there is no another period character - sec_period = strchr( period + 1, '.' ); - if( sec_period != NULL ) { - raise_invalid_param( ); - return -1; - } - - // Calculate the facility length, up to the first period character - len = period - string; - - // Copy the facility substring to the param buffer - param = copy_cstring_length( string, len ); - if( !param ) { - return -1; - } - - facility = stumpless_get_facility_enum( param ); - - free_mem( param ); - - if( facility < 0 ) { - raise_invalid_param( ); - return -1; - } - - // Calculate the severity length - len++; - len = slen - len; - - // Copy the severity substring to the param buffer - param = copy_cstring_length( ++period, len ); - if( !param ) { - return -1; - } - - severity = stumpless_get_severity_enum( param ); - - free_mem( param ); - - if( severity < 0 ) { - raise_invalid_param( ); - return -1; - } - - return get_prival( facility, severity ); -} - diff --git a/src/prival.c b/src/prival.c index ea2e45b15..4f36ce85a 100644 --- a/src/prival.c +++ b/src/prival.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 /* - * Copyright 2018-2022 Joel E. Anderson + * Copyright 2018-2024 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,23 @@ * limitations under the License. */ -#include +#include +#include #include +#include +#include #include #include #include -#include "private/severity.h" +#include "private/config.h" +#include "private/config/wrapper/locale.h" +#include "private/error.h" #include "private/facility.h" #include "private/memory.h" +#include "private/prival.h" +#include "private/severity.h" +#include "private/strhelper.h" +#include "private/validate.h" const char * stumpless_get_prival_string( int prival ) { @@ -43,3 +52,92 @@ stumpless_get_prival_string( int prival ) { return prival_string; } + +int +stumpless_prival_from_string( const char *string ) { + int prival; + int severity; + int facility; + const char *param; + const char *period; + const char *sec_period; + size_t len; + size_t slen; + + VALIDATE_ARG_NOT_NULL_INT_RETURN( string ); + + if( unlikely( !string[0] ) ) { + raise_argument_empty( L10N_NULL_ARG_ERROR_MESSAGE( "string" ) ); + return -1; + } + + slen = strlen( string ); + + if( isdigit( string[0] ) ) { + prival = atoi( string ); + if( prival <= 191 && slen < 4 ) { + return prival; + } + } + + // find the first period character + period = strchr( string, '.' ); + if( !period ) { + raise_invalid_param( ); + return -1; + } + + // check there is no another period character + sec_period = strchr( period + 1, '.' ); + if( sec_period != NULL ) { + raise_invalid_param( ); + return -1; + } + + // Calculate the facility length, up to the first period character + len = period - string; + + // Copy the facility substring to the param buffer + param = copy_cstring_length( string, len ); + if( !param ) { + return -1; + } + + facility = stumpless_get_facility_enum( param ); + + free_mem( param ); + + if( facility < 0 ) { + raise_invalid_param( ); + return -1; + } + + // Calculate the severity length + len++; + len = slen - len; + + // Copy the severity substring to the param buffer + param = copy_cstring_length( period + 1, len ); + if( !param ) { + return -1; + } + + severity = stumpless_get_severity_enum( param ); + + free_mem( param ); + + if( severity < 0 ) { + raise_invalid_param( ); + return -1; + } + + return get_prival( facility, severity ); +} + +/* private functions */ + +int +get_prival( enum stumpless_facility facility, + enum stumpless_severity severity ) { + return facility | severity; +} diff --git a/src/target.c b/src/target.c index d06c76791..49f2ee0dd 100644 --- a/src/target.c +++ b/src/target.c @@ -54,6 +54,7 @@ #include "private/inthelper.h" #include "private/memory.h" #include "private/param.h" +#include "private/prival.h" #include "private/severity.h" #include "private/strbuilder.h" #include "private/strhelper.h" diff --git a/test/function/priority.cpp b/test/function/priority.cpp deleted file mode 100644 index 10e13bc60..000000000 --- a/test/function/priority.cpp +++ /dev/null @@ -1,144 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -/* - * Copyright 2019-2024 Joel E. Anderson - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include "test/helper/assert.hpp" -#include "test/helper/memory_allocation.hpp" - -namespace { - - class PriorityTest : public::testing::Test { - }; - - TEST( GetPriorityValue, NumValidPriority ) { - int result; - - result = stumpless_prival_from_string( "119" ); - EXPECT_EQ( result, 119 ); - } - - TEST( GetPriorityValue, NumTooHighPriority ) { - int result; - - result = stumpless_prival_from_string( "192" ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); - } - - TEST( GetPriorityValue, NumInvalidPriority ) { - int result; - - result = stumpless_prival_from_string( "119aa" ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); - } - - TEST( GetPriorityValue, UpperValidPriority ) { - int result; - - result = stumpless_prival_from_string( "USER.INFO" ); - EXPECT_EQ( result, 14 ); - } - - TEST( GetPriorityValue, LowerValidPriority ) { - int result; - - result = stumpless_prival_from_string( "user.info" ); - EXPECT_EQ( result, 14 ); - } - - TEST( GetPriorityValue, EmptyPriority ) { - int result; - - result = stumpless_prival_from_string( "" ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY ); - } - - TEST( GetPriorityValue, NullPriority ) { - int result; - - result = stumpless_prival_from_string( NULL ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY ); - } - - TEST( GetPriorityValue, InvalidFacilityPriority ) { - int result; - - result = stumpless_prival_from_string( "umer.info" ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); - } - - TEST( GetPriorityValue, InvalidSeverityPriority ) { - int result; - - result = stumpless_prival_from_string( "user.imfo" ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); - } - - TEST( GetPriorityValue, InvalidNoPeriodPriority ) { - int result; - - result = stumpless_prival_from_string( "userinfo" ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); - } - - TEST( GetPriorityValue, InvalidMorePeriodPriority ) { - int result; - - result = stumpless_prival_from_string( "user.info." ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); - } - - TEST( GetPriorityValue, InvalidMemFacilityPriority ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_prival_from_string( "user.err" ); - EXPECT_EQ( result, -1 ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - - TEST( GetPriorityValue, InvalidMemSeverityPriority ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL_ON_SIZE( 4 ) ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_prival_from_string( "syslog.err" ); - EXPECT_EQ( result, -1 ); - - EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - -} diff --git a/test/function/prival.cpp b/test/function/prival.cpp index 4da6b92d0..f7943058e 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 /* - * Copyright 2024 Joel E. Anderson + * Copyright 2019-2024 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,130 @@ * limitations under the License. */ +#include #include #include #include +#include "test/helper/assert.hpp" +#include "test/helper/memory_allocation.hpp" namespace { class PrivalTest : public::testing::Test {}; + TEST( GetPrivalFromString, NumValidPriority ) { + int result; + + result = stumpless_prival_from_string( "119" ); + EXPECT_EQ( result, 119 ); + } + + TEST( GetPrivalFromString, NumTooHighPriority ) { + int result; + + result = stumpless_prival_from_string( "192" ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); + } + + TEST( GetPrivalFromString, NumInvalidPriority ) { + int result; + + result = stumpless_prival_from_string( "119aa" ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); + } + + TEST( GetPrivalFromString, UpperValidPriority ) { + int result; + + result = stumpless_prival_from_string( "USER.INFO" ); + EXPECT_EQ( result, 14 ); + } + + TEST( GetPrivalFromString, LowerValidPriority ) { + int result; + + result = stumpless_prival_from_string( "user.info" ); + EXPECT_EQ( result, 14 ); + } + + TEST( GetPrivalFromString, EmptyPriority ) { + int result; + + result = stumpless_prival_from_string( "" ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY ); + } + + TEST( GetPrivalFromString, NullPriority ) { + int result; + + result = stumpless_prival_from_string( NULL ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_ARGUMENT_EMPTY ); + } + + TEST( GetPrivalFromString, InvalidFacilityPriority ) { + int result; + + result = stumpless_prival_from_string( "umer.info" ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); + } + + TEST( GetPrivalFromString, InvalidSeverityPriority ) { + int result; + + result = stumpless_prival_from_string( "user.imfo" ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); + } + + TEST( GetPrivalFromString, InvalidNoPeriodPriority ) { + int result; + + result = stumpless_prival_from_string( "userinfo" ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); + } + + TEST( GetPrivalFromString, InvalidMorePeriodPriority ) { + int result; + + result = stumpless_prival_from_string( "user.info." ); + EXPECT_EQ( result, -1 ); + EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); + } + + TEST( GetPrivalFromString, InvalidMemFacilityPriority ) { + int result; + void * (*set_malloc_result)(size_t); + set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); + ASSERT_NOT_NULL( set_malloc_result ); + + result = stumpless_prival_from_string( "user.err" ); + EXPECT_EQ( result, -1 ); + + set_malloc_result = stumpless_set_malloc( malloc ); + EXPECT_TRUE( set_malloc_result == malloc ); + } + + TEST( GetPrivalFromString, InvalidMemSeverityPriority ) { + int result; + void * (*set_malloc_result)(size_t); + set_malloc_result = stumpless_set_malloc( MALLOC_FAIL_ON_SIZE( 4 ) ); + ASSERT_NOT_NULL( set_malloc_result ); + + result = stumpless_prival_from_string( "syslog.err" ); + EXPECT_EQ( result, -1 ); + + EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); + + set_malloc_result = stumpless_set_malloc( malloc ); + EXPECT_TRUE( set_malloc_result == malloc ); + } + TEST(GetPrivalString, ValidPrival) { int prival; const char *result; diff --git a/test/performance/priority.cpp b/test/performance/prival.cpp similarity index 80% rename from test/performance/priority.cpp rename to test/performance/prival.cpp index 523126344..c5f03b3f3 100644 --- a/test/performance/priority.cpp +++ b/test/performance/prival.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 /* - * Copyright 2020-2022 Joel E. Anderson + * Copyright 2020-2024 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,15 +17,18 @@ */ #include +#include #include #include "test/helper/memory_counter.hpp" -#include NEW_MEMORY_COUNTER( prival_from_string ) static void PrivalFromString(benchmark::State& state) { // Create a list of priorities. - std::vector priority_list = {"191", "user.emerg", "uucp.err", "local7.debug"}; + std::vector priority_list = { "191", + "user.emerg", + "uucp.err", + "local7.debug" }; INIT_MEMORY_COUNTER( prival_from_string ); @@ -41,4 +44,4 @@ static void PrivalFromString(benchmark::State& state) { SET_STATE_COUNTERS( state, prival_from_string ); } -BENCHMARK(PrivalFromString); \ No newline at end of file +BENCHMARK(PrivalFromString); diff --git a/tools/check_headers/stumpless.yml b/tools/check_headers/stumpless.yml index e2ae76e1a..b048d9db4 100644 --- a/tools/check_headers/stumpless.yml +++ b/tools/check_headers/stumpless.yml @@ -76,7 +76,6 @@ "get_facility": "private/facility.h" "get_paged_size": "private/memory.h" "get_priv_target": "private/target.h" -"get_prival": "private/entry.h" "get_severity": "private/severity.h" "gmtime_r_get_now": "private/config/have_gmtime_r.h" "HAVE_GMTIME_R": "private/config.h" @@ -638,5 +637,5 @@ "stumpless_get_target_type_string" : "stumpless/target.h" "STUMPLESS_FOREACH_TARGET_TYPE" : "stumpless/target.h" "STUMPLESS_FOREACH_SEVERITY" : "stumpless/severity.h" -"stumpless_prival_from_string" : "stumpless/priority.h" -"stumpless_get_prival_string" : "stumpless/prival.h" \ No newline at end of file +"stumpless_prival_from_string" : "stumpless/prival.h" +"stumpless_get_prival_string" : "stumpless/prival.h" diff --git a/tools/check_headers/stumpless_private.yml b/tools/check_headers/stumpless_private.yml index e2a2201b5..a7e62cfe5 100644 --- a/tools/check_headers/stumpless_private.yml +++ b/tools/check_headers/stumpless_private.yml @@ -69,6 +69,7 @@ "get_category": "private/config/wel_supported.h" "get_event_id": "private/config/wel_supported.h" "get_journald_field_name": "private/target/journald.h" +"get_prival": "private/prival.h" "get_type": "private/config/wel_supported.h" "getaddrinfo_int_connect": "private/config/have_getaddrinfo.h" "gethostbyname_int_connect": "private/config/gethostbyname_supported.h" diff --git a/tools/cmake/install_headers.cmake b/tools/cmake/install_headers.cmake index 9b5b7315a..66876017c 100644 --- a/tools/cmake/install_headers.cmake +++ b/tools/cmake/install_headers.cmake @@ -17,7 +17,6 @@ install( "${PROJECT_SOURCE_DIR}/include/stumpless/memory.h" "${PROJECT_SOURCE_DIR}/include/stumpless/option.h" "${PROJECT_SOURCE_DIR}/include/stumpless/param.h" - "${PROJECT_SOURCE_DIR}/include/stumpless/priority.h" "${PROJECT_SOURCE_DIR}/include/stumpless/prival.h" "${PROJECT_SOURCE_DIR}/include/stumpless/severity.h" "${PROJECT_SOURCE_DIR}/include/stumpless/target.h"