From 64e0951dce63141966f7010e113d38c3356ddc44 Mon Sep 17 00:00:00 2001 From: Raghav Sharma Date: Mon, 9 Jun 2025 08:58:01 +0530 Subject: [PATCH 1/5] feat: Add facility function tests and fix missing test targets --- include/stumpless/facility.h | 492 ++++++++++++++++++----------------- src/facility.c | 62 +++-- test/function/facility.cpp | 133 ++++++---- 3 files changed, 372 insertions(+), 315 deletions(-) diff --git a/include/stumpless/facility.h b/include/stumpless/facility.h index 256269e50..2775ad9be 100644 --- a/include/stumpless/facility.h +++ b/include/stumpless/facility.h @@ -26,59 +26,59 @@ */ #ifndef __STUMPLESS_FACILITY_H -# define __STUMPLESS_FACILITY_H +#define __STUMPLESS_FACILITY_H -# include -# include -# include +#include +#include +#include -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# include -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#include +#endif /** * Kernel message facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_KERN_VALUE LOG_KERN -# else -# define STUMPLESS_FACILITY_KERN_VALUE 0 -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_KERN_VALUE LOG_KERN +#else +#define STUMPLESS_FACILITY_KERN_VALUE 0 +#endif /** * User-level message facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_USER_VALUE LOG_USER -# else -# define STUMPLESS_FACILITY_USER_VALUE ( 1 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_USER_VALUE LOG_USER +#else +#define STUMPLESS_FACILITY_USER_VALUE (1 << 3) +#endif /** * Mail system facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_MAIL_VALUE LOG_MAIL -# else -# define STUMPLESS_FACILITY_MAIL_VALUE ( 2 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_MAIL_VALUE LOG_MAIL +#else +#define STUMPLESS_FACILITY_MAIL_VALUE (2 << 3) +#endif /** * System daemons facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_DAEMON_VALUE LOG_DAEMON -# else -# define STUMPLESS_FACILITY_DAEMON_VALUE ( 3 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_DAEMON_VALUE LOG_DAEMON +#else +#define STUMPLESS_FACILITY_DAEMON_VALUE (3 << 3) +#endif /** * @@ -86,11 +86,11 @@ * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_AUTH_VALUE LOG_AUTH -# else -# define STUMPLESS_FACILITY_AUTH_VALUE ( 4 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_AUTH_VALUE LOG_AUTH +#else +#define STUMPLESS_FACILITY_AUTH_VALUE (4 << 3) +#endif /** * Facility code value for messages generated internally by the logging daemon @@ -98,181 +98,181 @@ * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_SYSLOG_VALUE ( 5 << 3 ) +#define STUMPLESS_FACILITY_SYSLOG_VALUE (5 << 3) /** * Line printer subsystem facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LPR_VALUE LOG_LPR -# else -# define STUMPLESS_FACILITY_LPR_VALUE ( 6 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LPR_VALUE LOG_LPR +#else +#define STUMPLESS_FACILITY_LPR_VALUE (6 << 3) +#endif /** * Network news subsystem facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_NEWS_VALUE LOG_NEWS -# else -# define STUMPLESS_FACILITY_NEWS_VALUE ( 7 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_NEWS_VALUE LOG_NEWS +#else +#define STUMPLESS_FACILITY_NEWS_VALUE (7 << 3) +#endif /** * UUCP subsystem facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_UUCP_VALUE LOG_UUCP -# else -# define STUMPLESS_FACILITY_UUCP_VALUE ( 8 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_UUCP_VALUE LOG_UUCP +#else +#define STUMPLESS_FACILITY_UUCP_VALUE (8 << 3) +#endif /** * Clock daemon facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_CRON_VALUE LOG_CRON -# else -# define STUMPLESS_FACILITY_CRON_VALUE ( 9 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_CRON_VALUE LOG_CRON +#else +#define STUMPLESS_FACILITY_CRON_VALUE (9 << 3) +#endif /** * Security/authorization messages facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_AUTH2_VALUE ( 10 << 3 ) +#define STUMPLESS_FACILITY_AUTH2_VALUE (10 << 3) /** * FTP daemon facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_FTP_VALUE ( 11 << 3 ) +#define STUMPLESS_FACILITY_FTP_VALUE (11 << 3) /** * NTP subsystem facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_NTP_VALUE ( 12 << 3 ) +#define STUMPLESS_FACILITY_NTP_VALUE (12 << 3) /** * Log audit facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_AUDIT_VALUE ( 13 << 3 ) +#define STUMPLESS_FACILITY_AUDIT_VALUE (13 << 3) /** * Log alert facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_ALERT_VALUE ( 14 << 3 ) +#define STUMPLESS_FACILITY_ALERT_VALUE (14 << 3) /** * Clock daemon facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -# define STUMPLESS_FACILITY_CRON2_VALUE ( 15 << 3 ) +#define STUMPLESS_FACILITY_CRON2_VALUE (15 << 3) /** * Local use 0 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL0_VALUE LOG_LOCAL0 -# else -# define STUMPLESS_FACILITY_LOCAL0_VALUE ( 16 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL0_VALUE LOG_LOCAL0 +#else +#define STUMPLESS_FACILITY_LOCAL0_VALUE (16 << 3) +#endif /** * Local use 1 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL1_VALUE LOG_LOCAL1 -# else -# define STUMPLESS_FACILITY_LOCAL1_VALUE ( 17 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL1_VALUE LOG_LOCAL1 +#else +#define STUMPLESS_FACILITY_LOCAL1_VALUE (17 << 3) +#endif /** * Local use 2 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL2_VALUE LOG_LOCAL2 -# else -# define STUMPLESS_FACILITY_LOCAL2_VALUE ( 18 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL2_VALUE LOG_LOCAL2 +#else +#define STUMPLESS_FACILITY_LOCAL2_VALUE (18 << 3) +#endif /** * Local use 3 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL3_VALUE LOG_LOCAL3 -# else -# define STUMPLESS_FACILITY_LOCAL3_VALUE ( 19 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL3_VALUE LOG_LOCAL3 +#else +#define STUMPLESS_FACILITY_LOCAL3_VALUE (19 << 3) +#endif /** * Local use 4 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL4_VALUE LOG_LOCAL4 -# else -# define STUMPLESS_FACILITY_LOCAL4_VALUE ( 20 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL4_VALUE LOG_LOCAL4 +#else +#define STUMPLESS_FACILITY_LOCAL4_VALUE (20 << 3) +#endif /** * Local use 5 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL5_VALUE LOG_LOCAL5 -# else -# define STUMPLESS_FACILITY_LOCAL5_VALUE ( 21 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL5_VALUE LOG_LOCAL5 +#else +#define STUMPLESS_FACILITY_LOCAL5_VALUE (21 << 3) +#endif /** * Local use 6 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL6_VALUE LOG_LOCAL6 -# else -# define STUMPLESS_FACILITY_LOCAL6_VALUE ( 22 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL6_VALUE LOG_LOCAL6 +#else +#define STUMPLESS_FACILITY_LOCAL6_VALUE (22 << 3) +#endif /** * Local use 7 facility code value. * * @since release v2.0.0. */ -# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -# define STUMPLESS_FACILITY_LOCAL7_VALUE LOG_LOCAL7 -# else -# define STUMPLESS_FACILITY_LOCAL7_VALUE ( 23 << 3 ) -# endif +#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +#define STUMPLESS_FACILITY_LOCAL7_VALUE LOG_LOCAL7 +#else +#define STUMPLESS_FACILITY_LOCAL7_VALUE (23 << 3) +#endif /** * A macro function that runs the provided action once for each facility, @@ -282,149 +282,157 @@ * * @since release v2.0.0. */ -# define STUMPLESS_FOREACH_FACILITY( ACTION ) \ -/** Kernel messages. */ \ -ACTION( STUMPLESS_FACILITY_KERN, STUMPLESS_FACILITY_KERN_VALUE ) \ -/** User-level messages. */ \ -ACTION( STUMPLESS_FACILITY_USER, STUMPLESS_FACILITY_USER_VALUE ) \ -/** Mail system facility code value as defined by RFC 5424. */ \ -ACTION( STUMPLESS_FACILITY_MAIL, STUMPLESS_FACILITY_MAIL_VALUE ) \ -/** System daemons. */ \ -ACTION( STUMPLESS_FACILITY_DAEMON, STUMPLESS_FACILITY_DAEMON_VALUE ) \ -/** Security/authorization messages. */ \ -ACTION( STUMPLESS_FACILITY_AUTH, STUMPLESS_FACILITY_AUTH_VALUE ) \ -/** Message generated internally by the logging daemon. */ \ -ACTION( STUMPLESS_FACILITY_SYSLOG, STUMPLESS_FACILITY_SYSLOG_VALUE ) \ -/** Line printer subsystem. */ \ -ACTION( STUMPLESS_FACILITY_LPR, STUMPLESS_FACILITY_LPR_VALUE ) \ -/** Network news. */ \ -ACTION( STUMPLESS_FACILITY_NEWS, STUMPLESS_FACILITY_NEWS_VALUE ) \ -/** UUCP subsystem. */ \ -ACTION( STUMPLESS_FACILITY_UUCP, STUMPLESS_FACILITY_UUCP_VALUE ) \ -/** Clock daemon. */ \ -ACTION( STUMPLESS_FACILITY_CRON, STUMPLESS_FACILITY_CRON_VALUE ) \ -/** Security/authorization messages. */ \ -ACTION( STUMPLESS_FACILITY_AUTH2, STUMPLESS_FACILITY_AUTH2_VALUE ) \ -/** FTP daemon. */ \ -ACTION( STUMPLESS_FACILITY_FTP, STUMPLESS_FACILITY_FTP_VALUE ) \ -/** NTP subsystem. */ \ -ACTION( STUMPLESS_FACILITY_NTP, STUMPLESS_FACILITY_NTP_VALUE ) \ -/** Log audit. */ \ -ACTION( STUMPLESS_FACILITY_AUDIT, STUMPLESS_FACILITY_AUDIT_VALUE ) \ -/** Log alert. */ \ -ACTION( STUMPLESS_FACILITY_ALERT, STUMPLESS_FACILITY_ALERT_VALUE ) \ -/** Clock daemon. */ \ -ACTION( STUMPLESS_FACILITY_CRON2, STUMPLESS_FACILITY_CRON2_VALUE ) \ -/** Local use 0. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL0, STUMPLESS_FACILITY_LOCAL0_VALUE ) \ -/** Local use 1. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL1, STUMPLESS_FACILITY_LOCAL1_VALUE ) \ -/** Local use 2. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL2, STUMPLESS_FACILITY_LOCAL2_VALUE ) \ -/** Local use 3. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL3, STUMPLESS_FACILITY_LOCAL3_VALUE ) \ -/** Local use 4. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL4, STUMPLESS_FACILITY_LOCAL4_VALUE ) \ -/** Local use 5. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL5, STUMPLESS_FACILITY_LOCAL5_VALUE ) \ -/** Local use 6. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL6, STUMPLESS_FACILITY_LOCAL6_VALUE ) \ -/** Local use 7. */ \ -ACTION( STUMPLESS_FACILITY_LOCAL7, STUMPLESS_FACILITY_LOCAL7_VALUE ) - -# ifdef __cplusplus -extern "C" { -# endif - -/** - * All possible facility codes available to log entries. - * - * In versions prior to 2.0.0, these values were simply \#define symbols. They - * have been changed to an enum to clearly convey proper usage. - * - * @since release v2.0.0. - */ -enum stumpless_facility { - STUMPLESS_FOREACH_FACILITY( STUMPLESS_GENERATE_ENUM ) -}; - -/** - * Gets the string representation of the given facility. - * - * This is a string literal that should not be modified or freed by the caller. - * - * **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.1.0. - * - * @param facility The facility to get the string from. - * - * @return The string representation of the given facility. - */ -STUMPLESS_PUBLIC_FUNCTION -const char * -stumpless_get_facility_string( enum stumpless_facility facility ); - -/** - * Gets the enum value corresponding to the given facility string. - * - * **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.1.0. - * - * @param facility_string The facility name to get the enum from. - * - * @return The enum integer corresponding to the given facility or -1 if - * the string is not a valid facility name. - */ -STUMPLESS_PUBLIC_FUNCTION -enum stumpless_facility -stumpless_get_facility_enum( const char *facility_string ); - -/** - * Gets the enum value corresponding to the given facility string. - * - * **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 facility_string The facility name to get the enum from. - * - * @param facility_buffer_length The length of the buffer - * - * @return The enum integer corresponding to the given facility or -1 if - * the string is not a valid facility name. - */ -STUMPLESS_PUBLIC_FUNCTION -enum stumpless_facility -stumpless_get_facility_enum_from_buffer( const char *facility_string, size_t facility_buffer_length ); - -# ifdef __cplusplus +#define STUMPLESS_FOREACH_FACILITY(ACTION) \ + /** Kernel messages. */ \ + ACTION(STUMPLESS_FACILITY_KERN, STUMPLESS_FACILITY_KERN_VALUE) \ + /** User-level messages. */ \ + ACTION(STUMPLESS_FACILITY_USER, STUMPLESS_FACILITY_USER_VALUE) \ + /** Mail system facility code value as defined by RFC 5424. */ \ + ACTION(STUMPLESS_FACILITY_MAIL, STUMPLESS_FACILITY_MAIL_VALUE) \ + /** System daemons. */ \ + ACTION(STUMPLESS_FACILITY_DAEMON, STUMPLESS_FACILITY_DAEMON_VALUE) \ + /** Security/authorization messages. */ \ + ACTION(STUMPLESS_FACILITY_AUTH, STUMPLESS_FACILITY_AUTH_VALUE) \ + /** Message generated internally by the logging daemon. */ \ + ACTION(STUMPLESS_FACILITY_SYSLOG, STUMPLESS_FACILITY_SYSLOG_VALUE) \ + /** Line printer subsystem. */ \ + ACTION(STUMPLESS_FACILITY_LPR, STUMPLESS_FACILITY_LPR_VALUE) \ + /** Network news. */ \ + ACTION(STUMPLESS_FACILITY_NEWS, STUMPLESS_FACILITY_NEWS_VALUE) \ + /** UUCP subsystem. */ \ + ACTION(STUMPLESS_FACILITY_UUCP, STUMPLESS_FACILITY_UUCP_VALUE) \ + /** Clock daemon. */ \ + ACTION(STUMPLESS_FACILITY_CRON, STUMPLESS_FACILITY_CRON_VALUE) \ + /** Security/authorization messages. */ \ + ACTION(STUMPLESS_FACILITY_AUTH2, STUMPLESS_FACILITY_AUTH2_VALUE) \ + /** FTP daemon. */ \ + ACTION(STUMPLESS_FACILITY_FTP, STUMPLESS_FACILITY_FTP_VALUE) \ + /** NTP subsystem. */ \ + ACTION(STUMPLESS_FACILITY_NTP, STUMPLESS_FACILITY_NTP_VALUE) \ + /** Log audit. */ \ + ACTION(STUMPLESS_FACILITY_AUDIT, STUMPLESS_FACILITY_AUDIT_VALUE) \ + /** Log alert. */ \ + ACTION(STUMPLESS_FACILITY_ALERT, STUMPLESS_FACILITY_ALERT_VALUE) \ + /** Clock daemon. */ \ + ACTION(STUMPLESS_FACILITY_CRON2, STUMPLESS_FACILITY_CRON2_VALUE) \ + /** Local use 0. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL0, STUMPLESS_FACILITY_LOCAL0_VALUE) \ + /** Local use 1. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL1, STUMPLESS_FACILITY_LOCAL1_VALUE) \ + /** Local use 2. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL2, STUMPLESS_FACILITY_LOCAL2_VALUE) \ + /** Local use 3. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL3, STUMPLESS_FACILITY_LOCAL3_VALUE) \ + /** Local use 4. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL4, STUMPLESS_FACILITY_LOCAL4_VALUE) \ + /** Local use 5. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL5, STUMPLESS_FACILITY_LOCAL5_VALUE) \ + /** Local use 6. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL6, STUMPLESS_FACILITY_LOCAL6_VALUE) \ + /** Local use 7. */ \ + ACTION(STUMPLESS_FACILITY_LOCAL7, STUMPLESS_FACILITY_LOCAL7_VALUE) + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * All possible facility codes available to log entries. + * + * In versions prior to 2.0.0, these values were simply \#define symbols. They + * have been changed to an enum to clearly convey proper usage. + * + * @since release v2.0.0. + */ + enum stumpless_facility + { + STUMPLESS_FOREACH_FACILITY(STUMPLESS_GENERATE_ENUM) + }; + + /** + * Gets the string representation of the given facility. + * + * This is a string literal that should not be modified or freed by the caller. + * + * **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.1.0. + * + * @param facility The facility to get the string from. + * + * @return The string representation of the given facility. + */ + STUMPLESS_PUBLIC_FUNCTION + const char * + stumpless_get_facility_string(enum stumpless_facility facility); + + /** + * Gets the enum value corresponding to the given facility string. + * + * **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.1.0. + * + * @param facility_string The facility name to get the enum from. + * + * @error Clears previous error on success. + * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. + * + * @return The enum integer corresponding to the given facility or -1 if + * the string is not a valid facility name. + */ + STUMPLESS_PUBLIC_FUNCTION + enum stumpless_facility + stumpless_get_facility_enum(const char *facility_string); + + /** + * Gets the enum value corresponding to the given facility string. + * + * **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 facility_string The facility name to get the enum from. + * + * @param facility_buffer_length The length of the buffer + * + * @error Clears previous error on success. + * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. + * + * @return The enum integer corresponding to the given facility or -1 if + * the string is not a valid facility name. + */ + STUMPLESS_PUBLIC_FUNCTION + enum stumpless_facility + stumpless_get_facility_enum_from_buffer(const char *facility_string, size_t facility_buffer_length); + +#ifdef __cplusplus } /* extern "C" */ -# endif +#endif #endif /* __STUMPLESS_FACILITY_H */ diff --git a/src/facility.c b/src/facility.c index dff55c6d6..bf9c76297 100644 --- a/src/facility.c +++ b/src/facility.c @@ -21,6 +21,8 @@ #include #include "private/facility.h" #include "private/strhelper.h" +#include + static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) @@ -29,44 +31,60 @@ static char *facility_enum_to_string[] = { const char * stumpless_get_facility_string( enum stumpless_facility facility ) { if ( !facility_is_invalid( facility ) ) { + clear_error(); return facility_enum_to_string[facility >> 3]; } - return "NO_SUCH_FACILITY"; -} -enum stumpless_facility -stumpless_get_facility_enum( const char *facility_string ) { - return stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string)); + raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility enum" ); + return "NO_SUCH_FACILITY"; } enum stumpless_facility -stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { - size_t facility_bound; - size_t i; - const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" +stumpless_get_facility_enum_from_buffer( const char *facility_buffer, + size_t facility_buffer_length ) { + size_t facility_bound; + size_t i; + const int str_offset = 19; - facility_bound = sizeof( facility_enum_to_string ) / - sizeof( facility_enum_to_string[0] ); + facility_bound = sizeof( facility_enum_to_string ) / + sizeof( facility_enum_to_string[0] ); - for( i = 0; i < facility_bound; i++ ) { - if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { - return i << 3; + for( i = 0; i < facility_bound; i++ ) { + if( strncasecmp_custom( facility_buffer, + facility_enum_to_string[i] + str_offset, + facility_buffer_length ) == 0 ) { + clear_error(); + return i << 3; + } } - } - // exeption, for 'security' return 'auth' enum value if( strncasecmp_custom( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) { - return STUMPLESS_FACILITY_AUTH_VALUE; - } + clear_error(); + return STUMPLESS_FACILITY_AUTH_VALUE; + } - // exeption, for 'authpriv' not presented in enum list if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { - return STUMPLESS_FACILITY_AUTH2_VALUE; - } + clear_error(); + return STUMPLESS_FACILITY_AUTH2_VALUE; + } - return -1; + raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility string" ); + return -1; } +enum stumpless_facility +stumpless_get_facility_enum( const char *facility_string ) { + enum stumpless_facility result = stumpless_get_facility_enum_from_buffer( + facility_string, strlen( facility_string ) ); + + if( result != -1 ) { + clear_error(); + } + + return result; +} + + /* private functions */ int diff --git a/test/function/facility.cpp b/test/function/facility.cpp index b3292f1c2..e5a2ef359 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -20,100 +20,131 @@ #include #include "test/helper/assert.hpp" -namespace { +namespace +{ - class FacilityTest : public::testing::Test { + class FacilityTest : public ::testing::Test + { }; - TEST( GetFacilityString, EachValidFacility ) { + TEST(GetFacilityString, EachValidFacility) + { int facility_count = 0; const char *result; - #define CHECK_FACILITY( STRING, ENUM ) \ - result = stumpless_get_facility_string( STRING ); \ - EXPECT_STREQ( result, #STRING ); - STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY ) +#define CHECK_FACILITY(STRING, ENUM) \ + result = stumpless_get_facility_string(STRING); \ + EXPECT_STREQ(result, #STRING); + STUMPLESS_FOREACH_FACILITY(CHECK_FACILITY) } - TEST( GetFacilityString, NoSuchFacility ) { + TEST(GetFacilityString, NoSuchFacility) + { int facility_count = 0; const char *result; - #define COUNT_FACILITY( STRING, ENUM ) ++facility_count; - STUMPLESS_FOREACH_FACILITY( COUNT_FACILITY ) +#define COUNT_FACILITY(STRING, ENUM) ++facility_count; + STUMPLESS_FOREACH_FACILITY(COUNT_FACILITY) stumpless_facility wrong_facility = static_cast(facility_count + 1); - result = stumpless_get_facility_string( wrong_facility ); - EXPECT_STREQ( result, "NO_SUCH_FACILITY" ); + result = stumpless_get_facility_string(wrong_facility); + EXPECT_STREQ(result, "NO_SUCH_FACILITY"); } - TEST( GetFacilityEnum, EachValidFacility ) { + TEST(GetFacilityEnum, EachValidFacility) + { int facility_count = 0; int result; - #define CHECK_FACILITY_ENUM( STRING, ENUM ) \ - result = stumpless_get_facility_enum( #STRING + 19 ); \ - EXPECT_EQ( result, ENUM ); - STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY_ENUM ) +#define CHECK_FACILITY_ENUM(STRING, ENUM) \ + result = stumpless_get_facility_enum(#STRING + 19); \ + EXPECT_EQ(result, ENUM); + STUMPLESS_FOREACH_FACILITY(CHECK_FACILITY_ENUM) } - TEST( GetFacilityEnum, LowercaseFacility ) { + TEST(GetFacilityEnum, LowercaseFacility) + { int result; - result = stumpless_get_facility_enum( "user" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_USER ); + result = stumpless_get_facility_enum("user"); + EXPECT_EQ(result, STUMPLESS_FACILITY_USER); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "mail" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_MAIL ); + result = stumpless_get_facility_enum("mail"); + EXPECT_EQ(result, STUMPLESS_FACILITY_MAIL); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "daemon" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_DAEMON ); + result = stumpless_get_facility_enum("daemon"); + EXPECT_EQ(result, STUMPLESS_FACILITY_DAEMON); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "auth" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_AUTH ); + result = stumpless_get_facility_enum("auth"); + EXPECT_EQ(result, STUMPLESS_FACILITY_AUTH); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "security" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_AUTH ); + result = stumpless_get_facility_enum("security"); + EXPECT_EQ(result, STUMPLESS_FACILITY_AUTH); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "syslog" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_SYSLOG ); + result = stumpless_get_facility_enum("syslog"); + EXPECT_EQ(result, STUMPLESS_FACILITY_SYSLOG); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "lpr" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_LPR ); + result = stumpless_get_facility_enum("lpr"); + EXPECT_EQ(result, STUMPLESS_FACILITY_LPR); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "news" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_NEWS ); + result = stumpless_get_facility_enum("news"); + EXPECT_EQ(result, STUMPLESS_FACILITY_NEWS); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "uucp" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_UUCP ); + result = stumpless_get_facility_enum("uucp"); + EXPECT_EQ(result, STUMPLESS_FACILITY_UUCP); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "cron" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_CRON ); + result = stumpless_get_facility_enum("cron"); + EXPECT_EQ(result, STUMPLESS_FACILITY_CRON); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "authpriv" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_AUTH2 ); + result = stumpless_get_facility_enum("authpriv"); + EXPECT_EQ(result, STUMPLESS_FACILITY_AUTH2); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "ftp" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_FTP ); + result = stumpless_get_facility_enum("ftp"); + EXPECT_EQ(result, STUMPLESS_FACILITY_FTP); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "ntp" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_NTP ); + result = stumpless_get_facility_enum("ntp"); + EXPECT_EQ(result, STUMPLESS_FACILITY_NTP); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "audit" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_AUDIT ); + result = stumpless_get_facility_enum("audit"); + EXPECT_EQ(result, STUMPLESS_FACILITY_AUDIT); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum( "alert" ); - EXPECT_EQ( result, STUMPLESS_FACILITY_ALERT ); + result = stumpless_get_facility_enum("alert"); + EXPECT_EQ(result, STUMPLESS_FACILITY_ALERT); EXPECT_NO_ERROR; } - TEST( GetFacilityEnum, NoSuchFacility ) { + TEST(GetFacilityEnum, NoSuchFacility) + { int result; - result = stumpless_get_facility_enum( "an_invalid_facility" ); - EXPECT_EQ( result, -1 ); + result = stumpless_get_facility_enum("an_invalid_facility"); + EXPECT_EQ(result, -1); } } + +TEST(FacilityErrorHandling, ClearsPreviousErrorOnSuccess) +{ + stumpless_version_to_string(NULL); + EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY); + + const char *result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER); + EXPECT_NO_ERROR; + EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER"); +} + +TEST(FacilityErrorHandling, InvalidFacilityEnumRaisesError) +{ + const char *result = stumpless_get_facility_string(static_cast(200)); + EXPECT_STREQ(result, "NO_SUCH_FACILITY"); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); +} + +TEST(FacilityErrorHandling, InvalidFacilityStringRaisesError) +{ + int result = stumpless_get_facility_enum("not_a_real_facility"); + EXPECT_EQ(result, -1); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); +} From 8ef62c2e454d882e1094c2429d9c5cde92a03817 Mon Sep 17 00:00:00 2001 From: Raghav Sharma Date: Mon, 9 Jun 2025 19:45:06 +0530 Subject: [PATCH 2/5] Revert "feat: Add facility function tests and fix missing test targets" This reverts commit 64e0951dce63141966f7010e113d38c3356ddc44. --- include/stumpless/facility.h | 492 +++++++++++++++++------------------ src/facility.c | 62 ++--- test/function/facility.cpp | 133 ++++------ 3 files changed, 315 insertions(+), 372 deletions(-) diff --git a/include/stumpless/facility.h b/include/stumpless/facility.h index 2775ad9be..256269e50 100644 --- a/include/stumpless/facility.h +++ b/include/stumpless/facility.h @@ -26,59 +26,59 @@ */ #ifndef __STUMPLESS_FACILITY_H -#define __STUMPLESS_FACILITY_H +# define __STUMPLESS_FACILITY_H -#include -#include -#include +# include +# include +# include -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#include -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# include +# endif /** * Kernel message facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_KERN_VALUE LOG_KERN -#else -#define STUMPLESS_FACILITY_KERN_VALUE 0 -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_KERN_VALUE LOG_KERN +# else +# define STUMPLESS_FACILITY_KERN_VALUE 0 +# endif /** * User-level message facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_USER_VALUE LOG_USER -#else -#define STUMPLESS_FACILITY_USER_VALUE (1 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_USER_VALUE LOG_USER +# else +# define STUMPLESS_FACILITY_USER_VALUE ( 1 << 3 ) +# endif /** * Mail system facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_MAIL_VALUE LOG_MAIL -#else -#define STUMPLESS_FACILITY_MAIL_VALUE (2 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_MAIL_VALUE LOG_MAIL +# else +# define STUMPLESS_FACILITY_MAIL_VALUE ( 2 << 3 ) +# endif /** * System daemons facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_DAEMON_VALUE LOG_DAEMON -#else -#define STUMPLESS_FACILITY_DAEMON_VALUE (3 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_DAEMON_VALUE LOG_DAEMON +# else +# define STUMPLESS_FACILITY_DAEMON_VALUE ( 3 << 3 ) +# endif /** * @@ -86,11 +86,11 @@ * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_AUTH_VALUE LOG_AUTH -#else -#define STUMPLESS_FACILITY_AUTH_VALUE (4 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_AUTH_VALUE LOG_AUTH +# else +# define STUMPLESS_FACILITY_AUTH_VALUE ( 4 << 3 ) +# endif /** * Facility code value for messages generated internally by the logging daemon @@ -98,181 +98,181 @@ * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_SYSLOG_VALUE (5 << 3) +# define STUMPLESS_FACILITY_SYSLOG_VALUE ( 5 << 3 ) /** * Line printer subsystem facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LPR_VALUE LOG_LPR -#else -#define STUMPLESS_FACILITY_LPR_VALUE (6 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LPR_VALUE LOG_LPR +# else +# define STUMPLESS_FACILITY_LPR_VALUE ( 6 << 3 ) +# endif /** * Network news subsystem facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_NEWS_VALUE LOG_NEWS -#else -#define STUMPLESS_FACILITY_NEWS_VALUE (7 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_NEWS_VALUE LOG_NEWS +# else +# define STUMPLESS_FACILITY_NEWS_VALUE ( 7 << 3 ) +# endif /** * UUCP subsystem facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_UUCP_VALUE LOG_UUCP -#else -#define STUMPLESS_FACILITY_UUCP_VALUE (8 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_UUCP_VALUE LOG_UUCP +# else +# define STUMPLESS_FACILITY_UUCP_VALUE ( 8 << 3 ) +# endif /** * Clock daemon facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_CRON_VALUE LOG_CRON -#else -#define STUMPLESS_FACILITY_CRON_VALUE (9 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_CRON_VALUE LOG_CRON +# else +# define STUMPLESS_FACILITY_CRON_VALUE ( 9 << 3 ) +# endif /** * Security/authorization messages facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_AUTH2_VALUE (10 << 3) +# define STUMPLESS_FACILITY_AUTH2_VALUE ( 10 << 3 ) /** * FTP daemon facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_FTP_VALUE (11 << 3) +# define STUMPLESS_FACILITY_FTP_VALUE ( 11 << 3 ) /** * NTP subsystem facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_NTP_VALUE (12 << 3) +# define STUMPLESS_FACILITY_NTP_VALUE ( 12 << 3 ) /** * Log audit facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_AUDIT_VALUE (13 << 3) +# define STUMPLESS_FACILITY_AUDIT_VALUE ( 13 << 3 ) /** * Log alert facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_ALERT_VALUE (14 << 3) +# define STUMPLESS_FACILITY_ALERT_VALUE ( 14 << 3 ) /** * Clock daemon facility code value as defined by RFC 5424. * * @since release v2.0.0. */ -#define STUMPLESS_FACILITY_CRON2_VALUE (15 << 3) +# define STUMPLESS_FACILITY_CRON2_VALUE ( 15 << 3 ) /** * Local use 0 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL0_VALUE LOG_LOCAL0 -#else -#define STUMPLESS_FACILITY_LOCAL0_VALUE (16 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL0_VALUE LOG_LOCAL0 +# else +# define STUMPLESS_FACILITY_LOCAL0_VALUE ( 16 << 3 ) +# endif /** * Local use 1 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL1_VALUE LOG_LOCAL1 -#else -#define STUMPLESS_FACILITY_LOCAL1_VALUE (17 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL1_VALUE LOG_LOCAL1 +# else +# define STUMPLESS_FACILITY_LOCAL1_VALUE ( 17 << 3 ) +# endif /** * Local use 2 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL2_VALUE LOG_LOCAL2 -#else -#define STUMPLESS_FACILITY_LOCAL2_VALUE (18 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL2_VALUE LOG_LOCAL2 +# else +# define STUMPLESS_FACILITY_LOCAL2_VALUE ( 18 << 3 ) +# endif /** * Local use 3 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL3_VALUE LOG_LOCAL3 -#else -#define STUMPLESS_FACILITY_LOCAL3_VALUE (19 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL3_VALUE LOG_LOCAL3 +# else +# define STUMPLESS_FACILITY_LOCAL3_VALUE ( 19 << 3 ) +# endif /** * Local use 4 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL4_VALUE LOG_LOCAL4 -#else -#define STUMPLESS_FACILITY_LOCAL4_VALUE (20 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL4_VALUE LOG_LOCAL4 +# else +# define STUMPLESS_FACILITY_LOCAL4_VALUE ( 20 << 3 ) +# endif /** * Local use 5 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL5_VALUE LOG_LOCAL5 -#else -#define STUMPLESS_FACILITY_LOCAL5_VALUE (21 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL5_VALUE LOG_LOCAL5 +# else +# define STUMPLESS_FACILITY_LOCAL5_VALUE ( 21 << 3 ) +# endif /** * Local use 6 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL6_VALUE LOG_LOCAL6 -#else -#define STUMPLESS_FACILITY_LOCAL6_VALUE (22 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL6_VALUE LOG_LOCAL6 +# else +# define STUMPLESS_FACILITY_LOCAL6_VALUE ( 22 << 3 ) +# endif /** * Local use 7 facility code value. * * @since release v2.0.0. */ -#ifdef STUMPLESS_SYSLOG_H_COMPATIBLE -#define STUMPLESS_FACILITY_LOCAL7_VALUE LOG_LOCAL7 -#else -#define STUMPLESS_FACILITY_LOCAL7_VALUE (23 << 3) -#endif +# ifdef STUMPLESS_SYSLOG_H_COMPATIBLE +# define STUMPLESS_FACILITY_LOCAL7_VALUE LOG_LOCAL7 +# else +# define STUMPLESS_FACILITY_LOCAL7_VALUE ( 23 << 3 ) +# endif /** * A macro function that runs the provided action once for each facility, @@ -282,157 +282,149 @@ * * @since release v2.0.0. */ -#define STUMPLESS_FOREACH_FACILITY(ACTION) \ - /** Kernel messages. */ \ - ACTION(STUMPLESS_FACILITY_KERN, STUMPLESS_FACILITY_KERN_VALUE) \ - /** User-level messages. */ \ - ACTION(STUMPLESS_FACILITY_USER, STUMPLESS_FACILITY_USER_VALUE) \ - /** Mail system facility code value as defined by RFC 5424. */ \ - ACTION(STUMPLESS_FACILITY_MAIL, STUMPLESS_FACILITY_MAIL_VALUE) \ - /** System daemons. */ \ - ACTION(STUMPLESS_FACILITY_DAEMON, STUMPLESS_FACILITY_DAEMON_VALUE) \ - /** Security/authorization messages. */ \ - ACTION(STUMPLESS_FACILITY_AUTH, STUMPLESS_FACILITY_AUTH_VALUE) \ - /** Message generated internally by the logging daemon. */ \ - ACTION(STUMPLESS_FACILITY_SYSLOG, STUMPLESS_FACILITY_SYSLOG_VALUE) \ - /** Line printer subsystem. */ \ - ACTION(STUMPLESS_FACILITY_LPR, STUMPLESS_FACILITY_LPR_VALUE) \ - /** Network news. */ \ - ACTION(STUMPLESS_FACILITY_NEWS, STUMPLESS_FACILITY_NEWS_VALUE) \ - /** UUCP subsystem. */ \ - ACTION(STUMPLESS_FACILITY_UUCP, STUMPLESS_FACILITY_UUCP_VALUE) \ - /** Clock daemon. */ \ - ACTION(STUMPLESS_FACILITY_CRON, STUMPLESS_FACILITY_CRON_VALUE) \ - /** Security/authorization messages. */ \ - ACTION(STUMPLESS_FACILITY_AUTH2, STUMPLESS_FACILITY_AUTH2_VALUE) \ - /** FTP daemon. */ \ - ACTION(STUMPLESS_FACILITY_FTP, STUMPLESS_FACILITY_FTP_VALUE) \ - /** NTP subsystem. */ \ - ACTION(STUMPLESS_FACILITY_NTP, STUMPLESS_FACILITY_NTP_VALUE) \ - /** Log audit. */ \ - ACTION(STUMPLESS_FACILITY_AUDIT, STUMPLESS_FACILITY_AUDIT_VALUE) \ - /** Log alert. */ \ - ACTION(STUMPLESS_FACILITY_ALERT, STUMPLESS_FACILITY_ALERT_VALUE) \ - /** Clock daemon. */ \ - ACTION(STUMPLESS_FACILITY_CRON2, STUMPLESS_FACILITY_CRON2_VALUE) \ - /** Local use 0. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL0, STUMPLESS_FACILITY_LOCAL0_VALUE) \ - /** Local use 1. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL1, STUMPLESS_FACILITY_LOCAL1_VALUE) \ - /** Local use 2. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL2, STUMPLESS_FACILITY_LOCAL2_VALUE) \ - /** Local use 3. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL3, STUMPLESS_FACILITY_LOCAL3_VALUE) \ - /** Local use 4. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL4, STUMPLESS_FACILITY_LOCAL4_VALUE) \ - /** Local use 5. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL5, STUMPLESS_FACILITY_LOCAL5_VALUE) \ - /** Local use 6. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL6, STUMPLESS_FACILITY_LOCAL6_VALUE) \ - /** Local use 7. */ \ - ACTION(STUMPLESS_FACILITY_LOCAL7, STUMPLESS_FACILITY_LOCAL7_VALUE) - -#ifdef __cplusplus -extern "C" -{ -#endif - - /** - * All possible facility codes available to log entries. - * - * In versions prior to 2.0.0, these values were simply \#define symbols. They - * have been changed to an enum to clearly convey proper usage. - * - * @since release v2.0.0. - */ - enum stumpless_facility - { - STUMPLESS_FOREACH_FACILITY(STUMPLESS_GENERATE_ENUM) - }; - - /** - * Gets the string representation of the given facility. - * - * This is a string literal that should not be modified or freed by the caller. - * - * **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.1.0. - * - * @param facility The facility to get the string from. - * - * @return The string representation of the given facility. - */ - STUMPLESS_PUBLIC_FUNCTION - const char * - stumpless_get_facility_string(enum stumpless_facility facility); - - /** - * Gets the enum value corresponding to the given facility string. - * - * **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.1.0. - * - * @param facility_string The facility name to get the enum from. - * - * @error Clears previous error on success. - * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. - * - * @return The enum integer corresponding to the given facility or -1 if - * the string is not a valid facility name. - */ - STUMPLESS_PUBLIC_FUNCTION - enum stumpless_facility - stumpless_get_facility_enum(const char *facility_string); - - /** - * Gets the enum value corresponding to the given facility string. - * - * **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 facility_string The facility name to get the enum from. - * - * @param facility_buffer_length The length of the buffer - * - * @error Clears previous error on success. - * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. - * - * @return The enum integer corresponding to the given facility or -1 if - * the string is not a valid facility name. - */ - STUMPLESS_PUBLIC_FUNCTION - enum stumpless_facility - stumpless_get_facility_enum_from_buffer(const char *facility_string, size_t facility_buffer_length); - -#ifdef __cplusplus +# define STUMPLESS_FOREACH_FACILITY( ACTION ) \ +/** Kernel messages. */ \ +ACTION( STUMPLESS_FACILITY_KERN, STUMPLESS_FACILITY_KERN_VALUE ) \ +/** User-level messages. */ \ +ACTION( STUMPLESS_FACILITY_USER, STUMPLESS_FACILITY_USER_VALUE ) \ +/** Mail system facility code value as defined by RFC 5424. */ \ +ACTION( STUMPLESS_FACILITY_MAIL, STUMPLESS_FACILITY_MAIL_VALUE ) \ +/** System daemons. */ \ +ACTION( STUMPLESS_FACILITY_DAEMON, STUMPLESS_FACILITY_DAEMON_VALUE ) \ +/** Security/authorization messages. */ \ +ACTION( STUMPLESS_FACILITY_AUTH, STUMPLESS_FACILITY_AUTH_VALUE ) \ +/** Message generated internally by the logging daemon. */ \ +ACTION( STUMPLESS_FACILITY_SYSLOG, STUMPLESS_FACILITY_SYSLOG_VALUE ) \ +/** Line printer subsystem. */ \ +ACTION( STUMPLESS_FACILITY_LPR, STUMPLESS_FACILITY_LPR_VALUE ) \ +/** Network news. */ \ +ACTION( STUMPLESS_FACILITY_NEWS, STUMPLESS_FACILITY_NEWS_VALUE ) \ +/** UUCP subsystem. */ \ +ACTION( STUMPLESS_FACILITY_UUCP, STUMPLESS_FACILITY_UUCP_VALUE ) \ +/** Clock daemon. */ \ +ACTION( STUMPLESS_FACILITY_CRON, STUMPLESS_FACILITY_CRON_VALUE ) \ +/** Security/authorization messages. */ \ +ACTION( STUMPLESS_FACILITY_AUTH2, STUMPLESS_FACILITY_AUTH2_VALUE ) \ +/** FTP daemon. */ \ +ACTION( STUMPLESS_FACILITY_FTP, STUMPLESS_FACILITY_FTP_VALUE ) \ +/** NTP subsystem. */ \ +ACTION( STUMPLESS_FACILITY_NTP, STUMPLESS_FACILITY_NTP_VALUE ) \ +/** Log audit. */ \ +ACTION( STUMPLESS_FACILITY_AUDIT, STUMPLESS_FACILITY_AUDIT_VALUE ) \ +/** Log alert. */ \ +ACTION( STUMPLESS_FACILITY_ALERT, STUMPLESS_FACILITY_ALERT_VALUE ) \ +/** Clock daemon. */ \ +ACTION( STUMPLESS_FACILITY_CRON2, STUMPLESS_FACILITY_CRON2_VALUE ) \ +/** Local use 0. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL0, STUMPLESS_FACILITY_LOCAL0_VALUE ) \ +/** Local use 1. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL1, STUMPLESS_FACILITY_LOCAL1_VALUE ) \ +/** Local use 2. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL2, STUMPLESS_FACILITY_LOCAL2_VALUE ) \ +/** Local use 3. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL3, STUMPLESS_FACILITY_LOCAL3_VALUE ) \ +/** Local use 4. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL4, STUMPLESS_FACILITY_LOCAL4_VALUE ) \ +/** Local use 5. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL5, STUMPLESS_FACILITY_LOCAL5_VALUE ) \ +/** Local use 6. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL6, STUMPLESS_FACILITY_LOCAL6_VALUE ) \ +/** Local use 7. */ \ +ACTION( STUMPLESS_FACILITY_LOCAL7, STUMPLESS_FACILITY_LOCAL7_VALUE ) + +# ifdef __cplusplus +extern "C" { +# endif + +/** + * All possible facility codes available to log entries. + * + * In versions prior to 2.0.0, these values were simply \#define symbols. They + * have been changed to an enum to clearly convey proper usage. + * + * @since release v2.0.0. + */ +enum stumpless_facility { + STUMPLESS_FOREACH_FACILITY( STUMPLESS_GENERATE_ENUM ) +}; + +/** + * Gets the string representation of the given facility. + * + * This is a string literal that should not be modified or freed by the caller. + * + * **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.1.0. + * + * @param facility The facility to get the string from. + * + * @return The string representation of the given facility. + */ +STUMPLESS_PUBLIC_FUNCTION +const char * +stumpless_get_facility_string( enum stumpless_facility facility ); + +/** + * Gets the enum value corresponding to the given facility string. + * + * **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.1.0. + * + * @param facility_string The facility name to get the enum from. + * + * @return The enum integer corresponding to the given facility or -1 if + * the string is not a valid facility name. + */ +STUMPLESS_PUBLIC_FUNCTION +enum stumpless_facility +stumpless_get_facility_enum( const char *facility_string ); + +/** + * Gets the enum value corresponding to the given facility string. + * + * **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 facility_string The facility name to get the enum from. + * + * @param facility_buffer_length The length of the buffer + * + * @return The enum integer corresponding to the given facility or -1 if + * the string is not a valid facility name. + */ +STUMPLESS_PUBLIC_FUNCTION +enum stumpless_facility +stumpless_get_facility_enum_from_buffer( const char *facility_string, size_t facility_buffer_length ); + +# ifdef __cplusplus } /* extern "C" */ -#endif +# endif #endif /* __STUMPLESS_FACILITY_H */ diff --git a/src/facility.c b/src/facility.c index bf9c76297..dff55c6d6 100644 --- a/src/facility.c +++ b/src/facility.c @@ -21,8 +21,6 @@ #include #include "private/facility.h" #include "private/strhelper.h" -#include - static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) @@ -31,60 +29,44 @@ static char *facility_enum_to_string[] = { const char * stumpless_get_facility_string( enum stumpless_facility facility ) { if ( !facility_is_invalid( facility ) ) { - clear_error(); return facility_enum_to_string[facility >> 3]; } - - raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility enum" ); return "NO_SUCH_FACILITY"; } enum stumpless_facility -stumpless_get_facility_enum_from_buffer( const char *facility_buffer, - size_t facility_buffer_length ) { - size_t facility_bound; - size_t i; - const int str_offset = 19; +stumpless_get_facility_enum( const char *facility_string ) { + return stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string)); +} + +enum stumpless_facility +stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { + size_t facility_bound; + size_t i; + const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" - facility_bound = sizeof( facility_enum_to_string ) / - sizeof( facility_enum_to_string[0] ); + facility_bound = sizeof( facility_enum_to_string ) / + sizeof( facility_enum_to_string[0] ); - for( i = 0; i < facility_bound; i++ ) { - if( strncasecmp_custom( facility_buffer, - facility_enum_to_string[i] + str_offset, - facility_buffer_length ) == 0 ) { - clear_error(); - return i << 3; - } + for( i = 0; i < facility_bound; i++ ) { + if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { + return i << 3; } + } + // exeption, for 'security' return 'auth' enum value if( strncasecmp_custom( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) { - clear_error(); - return STUMPLESS_FACILITY_AUTH_VALUE; - } + return STUMPLESS_FACILITY_AUTH_VALUE; + } + // exeption, for 'authpriv' not presented in enum list if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { - clear_error(); - return STUMPLESS_FACILITY_AUTH2_VALUE; - } + return STUMPLESS_FACILITY_AUTH2_VALUE; + } - raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility string" ); - return -1; + return -1; } -enum stumpless_facility -stumpless_get_facility_enum( const char *facility_string ) { - enum stumpless_facility result = stumpless_get_facility_enum_from_buffer( - facility_string, strlen( facility_string ) ); - - if( result != -1 ) { - clear_error(); - } - - return result; -} - - /* private functions */ int diff --git a/test/function/facility.cpp b/test/function/facility.cpp index e5a2ef359..b3292f1c2 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -20,131 +20,100 @@ #include #include "test/helper/assert.hpp" -namespace -{ +namespace { - class FacilityTest : public ::testing::Test - { + class FacilityTest : public::testing::Test { }; - TEST(GetFacilityString, EachValidFacility) - { + TEST( GetFacilityString, EachValidFacility ) { int facility_count = 0; const char *result; -#define CHECK_FACILITY(STRING, ENUM) \ - result = stumpless_get_facility_string(STRING); \ - EXPECT_STREQ(result, #STRING); - STUMPLESS_FOREACH_FACILITY(CHECK_FACILITY) + #define CHECK_FACILITY( STRING, ENUM ) \ + result = stumpless_get_facility_string( STRING ); \ + EXPECT_STREQ( result, #STRING ); + STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY ) } - TEST(GetFacilityString, NoSuchFacility) - { + TEST( GetFacilityString, NoSuchFacility ) { int facility_count = 0; const char *result; -#define COUNT_FACILITY(STRING, ENUM) ++facility_count; - STUMPLESS_FOREACH_FACILITY(COUNT_FACILITY) + #define COUNT_FACILITY( STRING, ENUM ) ++facility_count; + STUMPLESS_FOREACH_FACILITY( COUNT_FACILITY ) stumpless_facility wrong_facility = static_cast(facility_count + 1); - result = stumpless_get_facility_string(wrong_facility); - EXPECT_STREQ(result, "NO_SUCH_FACILITY"); + result = stumpless_get_facility_string( wrong_facility ); + EXPECT_STREQ( result, "NO_SUCH_FACILITY" ); } - TEST(GetFacilityEnum, EachValidFacility) - { + TEST( GetFacilityEnum, EachValidFacility ) { int facility_count = 0; int result; -#define CHECK_FACILITY_ENUM(STRING, ENUM) \ - result = stumpless_get_facility_enum(#STRING + 19); \ - EXPECT_EQ(result, ENUM); - STUMPLESS_FOREACH_FACILITY(CHECK_FACILITY_ENUM) + #define CHECK_FACILITY_ENUM( STRING, ENUM ) \ + result = stumpless_get_facility_enum( #STRING + 19 ); \ + EXPECT_EQ( result, ENUM ); + STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY_ENUM ) } - TEST(GetFacilityEnum, LowercaseFacility) - { + TEST( GetFacilityEnum, LowercaseFacility ) { int result; - result = stumpless_get_facility_enum("user"); - EXPECT_EQ(result, STUMPLESS_FACILITY_USER); + result = stumpless_get_facility_enum( "user" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_USER ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("mail"); - EXPECT_EQ(result, STUMPLESS_FACILITY_MAIL); + result = stumpless_get_facility_enum( "mail" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_MAIL ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("daemon"); - EXPECT_EQ(result, STUMPLESS_FACILITY_DAEMON); + result = stumpless_get_facility_enum( "daemon" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_DAEMON ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("auth"); - EXPECT_EQ(result, STUMPLESS_FACILITY_AUTH); + result = stumpless_get_facility_enum( "auth" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_AUTH ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("security"); - EXPECT_EQ(result, STUMPLESS_FACILITY_AUTH); + result = stumpless_get_facility_enum( "security" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_AUTH ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("syslog"); - EXPECT_EQ(result, STUMPLESS_FACILITY_SYSLOG); + result = stumpless_get_facility_enum( "syslog" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_SYSLOG ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("lpr"); - EXPECT_EQ(result, STUMPLESS_FACILITY_LPR); + result = stumpless_get_facility_enum( "lpr" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_LPR ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("news"); - EXPECT_EQ(result, STUMPLESS_FACILITY_NEWS); + result = stumpless_get_facility_enum( "news" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_NEWS ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("uucp"); - EXPECT_EQ(result, STUMPLESS_FACILITY_UUCP); + result = stumpless_get_facility_enum( "uucp" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_UUCP ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("cron"); - EXPECT_EQ(result, STUMPLESS_FACILITY_CRON); + result = stumpless_get_facility_enum( "cron" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_CRON ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("authpriv"); - EXPECT_EQ(result, STUMPLESS_FACILITY_AUTH2); + result = stumpless_get_facility_enum( "authpriv" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_AUTH2 ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("ftp"); - EXPECT_EQ(result, STUMPLESS_FACILITY_FTP); + result = stumpless_get_facility_enum( "ftp" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_FTP ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("ntp"); - EXPECT_EQ(result, STUMPLESS_FACILITY_NTP); + result = stumpless_get_facility_enum( "ntp" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_NTP ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("audit"); - EXPECT_EQ(result, STUMPLESS_FACILITY_AUDIT); + result = stumpless_get_facility_enum( "audit" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_AUDIT ); EXPECT_NO_ERROR; - result = stumpless_get_facility_enum("alert"); - EXPECT_EQ(result, STUMPLESS_FACILITY_ALERT); + result = stumpless_get_facility_enum( "alert" ); + EXPECT_EQ( result, STUMPLESS_FACILITY_ALERT ); EXPECT_NO_ERROR; } - TEST(GetFacilityEnum, NoSuchFacility) - { + TEST( GetFacilityEnum, NoSuchFacility ) { int result; - result = stumpless_get_facility_enum("an_invalid_facility"); - EXPECT_EQ(result, -1); + result = stumpless_get_facility_enum( "an_invalid_facility" ); + EXPECT_EQ( result, -1 ); } } - -TEST(FacilityErrorHandling, ClearsPreviousErrorOnSuccess) -{ - stumpless_version_to_string(NULL); - EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY); - - const char *result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER); - EXPECT_NO_ERROR; - EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER"); -} - -TEST(FacilityErrorHandling, InvalidFacilityEnumRaisesError) -{ - const char *result = stumpless_get_facility_string(static_cast(200)); - EXPECT_STREQ(result, "NO_SUCH_FACILITY"); - EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); -} - -TEST(FacilityErrorHandling, InvalidFacilityStringRaisesError) -{ - int result = stumpless_get_facility_enum("not_a_real_facility"); - EXPECT_EQ(result, -1); - EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); -} From a050775b0b05a2e0a7dcdcd33cdf74acffdd6e96 Mon Sep 17 00:00:00 2001 From: Raghav Sharma Date: Mon, 9 Jun 2025 20:29:08 +0530 Subject: [PATCH 3/5] feat: Add facility function tests and fix missing test targets (without white space changes and fixed header) --- include/stumpless/facility.h | 6 ++++ src/facility.c | 59 ++++++++++++++++++++++-------------- test/function/facility.cpp | 22 ++++++++++++++ 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/include/stumpless/facility.h b/include/stumpless/facility.h index 256269e50..484714625 100644 --- a/include/stumpless/facility.h +++ b/include/stumpless/facility.h @@ -389,6 +389,9 @@ stumpless_get_facility_string( enum stumpless_facility facility ); * @since release v2.1.0. * * @param facility_string The facility name to get the enum from. + * + * @error Clears previous error on success. + * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. * * @return The enum integer corresponding to the given facility or -1 if * the string is not a valid facility name. @@ -415,6 +418,9 @@ stumpless_get_facility_enum( const char *facility_string ); * @param facility_string The facility name to get the enum from. * * @param facility_buffer_length The length of the buffer + * + * @error Clears previous error on success. + * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. * * @return The enum integer corresponding to the given facility or -1 if * the string is not a valid facility name. diff --git a/src/facility.c b/src/facility.c index dff55c6d6..166b41291 100644 --- a/src/facility.c +++ b/src/facility.c @@ -21,6 +21,7 @@ #include #include "private/facility.h" #include "private/strhelper.h" +#include "private/error.h" static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) @@ -29,44 +30,58 @@ static char *facility_enum_to_string[] = { const char * stumpless_get_facility_string( enum stumpless_facility facility ) { if ( !facility_is_invalid( facility ) ) { + clear_error(); return facility_enum_to_string[facility >> 3]; } - return "NO_SUCH_FACILITY"; -} -enum stumpless_facility -stumpless_get_facility_enum( const char *facility_string ) { - return stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string)); + raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility enum" , 0, NULL); + return "NO_SUCH_FACILITY"; } enum stumpless_facility -stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { - size_t facility_bound; - size_t i; - const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" +stumpless_get_facility_enum_from_buffer( const char *facility_buffer, size_t facility_buffer_length ) { + size_t facility_bound; + size_t i; + const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" - facility_bound = sizeof( facility_enum_to_string ) / - sizeof( facility_enum_to_string[0] ); + facility_bound = sizeof( facility_enum_to_string ) / + sizeof( facility_enum_to_string[0] ); - for( i = 0; i < facility_bound; i++ ) { - if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { - return i << 3; + for( i = 0; i < facility_bound; i++ ) { + if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { + clear_error(); + return i << 3; + } } - } - // exeption, for 'security' return 'auth' enum value if( strncasecmp_custom( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) { - return STUMPLESS_FACILITY_AUTH_VALUE; - } + clear_error(); + return STUMPLESS_FACILITY_AUTH_VALUE; + } - // exeption, for 'authpriv' not presented in enum list if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { - return STUMPLESS_FACILITY_AUTH2_VALUE; - } + clear_error(); + return STUMPLESS_FACILITY_AUTH2_VALUE; + } + + raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility string", 0, NULL ); + return -1; +} + +enum stumpless_facility +stumpless_get_facility_enum( const char *facility_string ) { + enum stumpless_facility result = stumpless_get_facility_enum_from_buffer( + facility_string, strlen( facility_string ) ); - return -1; + if( result != -1 ) { + clear_error(); + } + + return result; } + + /* private functions */ int diff --git a/test/function/facility.cpp b/test/function/facility.cpp index b3292f1c2..b92227f1a 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -116,4 +116,26 @@ namespace { EXPECT_EQ( result, -1 ); } + TEST( FacilityErrorHandling, ClearsPreviousErrorOnSuccess ) { + stumpless_version_to_string(NULL); + EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY); + + const char* result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER); + EXPECT_NO_ERROR; + EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER"); + } + + TEST( FacilityErrorHandling, InvalidFacilityEnumRaisesError ) { + const char* result = stumpless_get_facility_string(static_cast(200)); + EXPECT_STREQ(result, "NO_SUCH_FACILITY"); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); + } + + TEST( FacilityErrorHandling, InvalidFacilityStringRaisesError ) { + int result = stumpless_get_facility_enum("not_a_real_facility"); + EXPECT_EQ(result, -1); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); + } + + } From 15ace65d0cc088796d55a549ba928af4298b0fd8 Mon Sep 17 00:00:00 2001 From: Raghav Sharma Date: Tue, 10 Jun 2025 01:46:10 +0530 Subject: [PATCH 4/5] fix: Added stumpless/error.h header file --- src/facility.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/facility.c b/src/facility.c index 166b41291..d2bd03087 100644 --- a/src/facility.c +++ b/src/facility.c @@ -22,6 +22,7 @@ #include "private/facility.h" #include "private/strhelper.h" #include "private/error.h" +#include static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) From f9b5fcff56fd001a874ede2eaa6cf1cc1a8a0ccb Mon Sep 17 00:00:00 2001 From: Raghav Sharma Date: Tue, 10 Jun 2025 02:40:00 +0530 Subject: [PATCH 5/5] fix: fixed header and formatting --- include/stumpless/facility.h | 10 ++++++---- src/facility.c | 3 +-- test/function/facility.cpp | 23 ++++++++++++----------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/stumpless/facility.h b/include/stumpless/facility.h index 484714625..77da68862 100644 --- a/include/stumpless/facility.h +++ b/include/stumpless/facility.h @@ -390,8 +390,9 @@ stumpless_get_facility_string( enum stumpless_facility facility ); * * @param facility_string The facility name to get the enum from. * - * @error Clears previous error on success. - * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. + * Clears previous error on success. + * + * Raises STUMPLESS_INVALID_FACILITY on invalid input. * * @return The enum integer corresponding to the given facility or -1 if * the string is not a valid facility name. @@ -419,8 +420,9 @@ stumpless_get_facility_enum( const char *facility_string ); * * @param facility_buffer_length The length of the buffer * - * @error Clears previous error on success. - * @error Raises STUMPLESS_INVALID_FACILITY on invalid input. + * Clears previous error on success. + * + * Raises STUMPLESS_INVALID_FACILITY on invalid input. * * @return The enum integer corresponding to the given facility or -1 if * the string is not a valid facility name. diff --git a/src/facility.c b/src/facility.c index d2bd03087..ecb9091af 100644 --- a/src/facility.c +++ b/src/facility.c @@ -22,6 +22,7 @@ #include "private/facility.h" #include "private/strhelper.h" #include "private/error.h" +#include #include static char *facility_enum_to_string[] = { @@ -81,8 +82,6 @@ stumpless_get_facility_enum( const char *facility_string ) { return result; } - - /* private functions */ int diff --git a/test/function/facility.cpp b/test/function/facility.cpp index b92227f1a..9ef8ebc7f 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -19,6 +19,7 @@ #include #include #include "test/helper/assert.hpp" +#include namespace { @@ -117,24 +118,24 @@ namespace { } TEST( FacilityErrorHandling, ClearsPreviousErrorOnSuccess ) { - stumpless_version_to_string(NULL); - EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY); + stumpless_version_to_string(NULL); + EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY); - const char* result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER); - EXPECT_NO_ERROR; - EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER"); + const char* result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER); + EXPECT_NO_ERROR; + EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER"); } TEST( FacilityErrorHandling, InvalidFacilityEnumRaisesError ) { - const char* result = stumpless_get_facility_string(static_cast(200)); - EXPECT_STREQ(result, "NO_SUCH_FACILITY"); - EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); + const char* result = stumpless_get_facility_string(static_cast(200)); + EXPECT_STREQ(result, "NO_SUCH_FACILITY"); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); } TEST( FacilityErrorHandling, InvalidFacilityStringRaisesError ) { - int result = stumpless_get_facility_enum("not_a_real_facility"); - EXPECT_EQ(result, -1); - EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); + int result = stumpless_get_facility_enum("not_a_real_facility"); + EXPECT_EQ(result, -1); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); }