diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e6eacf15..94086577e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,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 @@ -950,6 +951,7 @@ install(FILES ${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/prival.h ${PROJECT_SOURCE_DIR}/include/stumpless/severity.h ${PROJECT_SOURCE_DIR}/include/stumpless/target.h ${PROJECT_SOURCE_DIR}/include/stumpless/version.h @@ -1218,6 +1220,10 @@ add_function_test(perror SOURCES test/function/startup/perror.cpp ) +add_function_test(prival + SOURCES test/function/prival.cpp +) + add_function_test(severity SOURCES test/function/severity.cpp ) diff --git a/include/stumpless.h b/include/stumpless.h index bb4c75378..21ad5d395 100644 --- a/include/stumpless.h +++ b/include/stumpless.h @@ -52,6 +52,7 @@ # include # include # include +# include # include # include # include diff --git a/include/stumpless/prival.h b/include/stumpless/prival.h new file mode 100644 index 000000000..8131d681a --- /dev/null +++ b/include/stumpless/prival.h @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* + * Copyright 2018-2022 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 + * Prival codes for function to create severity and facility string from prival int value. + */ + +#ifndef __STUMPLESS_PRIVAL_H +# define __STUMPLESS_PRIVAL_H + +# include + +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# include +# endif + +/** + * Gets the string corresponding to the given int prival value. + * + * **Thread Safety: MT-Safe** + * This function is thread safe. + * + * **Async Signal Safety: AS-Safe** + * This function is safe to call from signal handlers. + * + * **Async Cancel Safety: AC-Safe** + * This function is safe to call from threads that may be asynchronously + * cancelled. + * + * @since release v2.2.0. + * + * @param prival The prival name to get the string from. + * + * @return The string representation of the severity and facility from the given prival. +*/ +STUMPLESS_PUBLIC_FUNCTION +const char * +stumpless_get_prival_string( int prival ); + +# ifdef __cplusplus +} /* extern "C" */ +# endif + +#endif /* __STUMPLESS_PRIVAL_H */ diff --git a/src/prival.c b/src/prival.c new file mode 100644 index 000000000..65dac74d3 --- /dev/null +++ b/src/prival.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: Apache-2.0 + +/* + * Copyright 2018-2022 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 "private/facility.h" +#include "private/severity.h" +#include "private/memory.h" +#include "private/validate.h" + + +const char * stumpless_get_prival_string(int prival) { + + VALIDATE_ARG_NOT_NULL(prival); + + const char *prival_string; + int severity; + int facility; + + prival_string = alloc_mem(8); + severity = get_severity(prival); + facility = get_facility(prival); + + snprintf(&prival_string, 8, "%s | %s", stumpless_get_severity_string(severity), + stumpless_get_facility_string(facility)); + return prival_string; +} diff --git a/src/windows/stumpless.def b/src/windows/stumpless.def index 5397d84d8..5d5bb821e 100644 --- a/src/windows/stumpless.def +++ b/src/windows/stumpless.def @@ -222,3 +222,4 @@ EXPORTS stumpless_unload_entry_only @206 stumpless_unload_param @207 vstumpless_load_entry @208 + stumpless_get_prival_string @209 \ No newline at end of file diff --git a/test/function/prival.cpp b/test/function/prival.cpp new file mode 100644 index 000000000..083e85208 --- /dev/null +++ b/test/function/prival.cpp @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: Apache-2.0 + +/* + * Copyright 2019-2021 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 + +namespace { + + class PrivalTest : public::testing::Test { + }; + + TEST(GetPrivalString, ValidPrival) { + const char *result; + + result = stumpless_get_prival_string( 11 ); \ + EXPECT_STREQ( result, "3 | 8" ); + } +} \ No newline at end of file diff --git a/tools/check_headers/stumpless.yml b/tools/check_headers/stumpless.yml index 330987e3c..a74850836 100644 --- a/tools/check_headers/stumpless.yml +++ b/tools/check_headers/stumpless.yml @@ -632,6 +632,7 @@ "stumpless_get_severity_enum" : "stumpless/severity.h" "stumpless_get_facility_string" : "stumpless/facility.h" "stumpless_get_facility_enum" : "stumpless/facility.h" +"stumpless_get_prival_string" : "stumpless/prival.h" "stumpless_get_target_type_string" : "stumpless/target.h" "STUMPLESS_FOREACH_TARGET_TYPE" : "stumpless/target.h" "STUMPLESS_FOREACH_SEVERITY" : "stumpless/severity.h"