Skip to content

Commit

Permalink
more chain configuration option support
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Mar 18, 2024
1 parent b9bcc6f commit 5726ceb
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ endif()

if(STUMPLESS_CHAIN_TARGETS_SUPPORTED)
include(tools/cmake/chain.cmake)
else()
if(WIN32)
list(APPEND STUMPLESS_SOURCES "${PROJECT_SOURCE_DIR}/src/config/chain_unsupported.c")
endif(WIN32)
endif()


Expand Down
35 changes: 35 additions & 0 deletions include/private/config/wrapper/chain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* SPDX-License-Identifier: Apache-2.0 */

/*
* Copyright 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_CONFIG_WRAPPER_CHAIN_H
#define __STUMPLESS_PRIVATE_CONFIG_WRAPPER_CHAIN_H

#include <stumpless/config.h>

#ifdef STUMPLESS_CHAIN_TARGETS_SUPPORTED
# include <stumpless/target/chain.h>
# include "private/target/chain.h"
# define config_close_chain_and_contents stumpless_close_chain_and_contents
# define config_send_entry_to_chain_target send_entry_to_chain_target
#else
# include "private/target.h"
# define config_close_chain_and_contents close_unsupported_target
# define config_send_entry_to_chain_target send_entry_to_unsupported_target
#endif

#endif /* __STUMPLESS_PRIVATE_CONFIG_WRAPPER_CHAIN_H */
4 changes: 2 additions & 2 deletions include/private/target/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ new_chain_target( void );
* of the failing target is returned.
*/
int
sendto_chain( struct chain_target *chain,
const struct stumpless_entry *entry );
send_entry_to_chain_target( struct chain_target *chain,
const struct stumpless_entry *entry );

/**
* Releases the lock for the provided chain target.
Expand Down
52 changes: 52 additions & 0 deletions src/config/chain_unsupported.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: Apache-2.0

/*
* Copyright 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 <stddef.h>
#include <stumpless/target.h>
#include <stumpless/target/chain.h>
#include "private/config/locale/wrapper.h"
#include "private/error.h"

struct stumpless_target *
stumpless_add_target_to_chain( struct stumpless_target *chain,
struct stumpless_target *target ){
raise_target_unsupported( L10N_CHAIN_TARGETS_UNSUPPORTED );
return NULL;
}

void
stumpless_close_chain_and_contents( struct stumpless_target *chain ){
raise_target_unsupported( L10N_CHAIN_TARGETS_UNSUPPORTED );
}

void
stumpless_close_chain_only( struct stumpless_target *chain ){
raise_target_unsupported( L10N_CHAIN_TARGETS_UNSUPPORTED );
}

size_t
stumpless_get_chain_length( const struct stumpless_target *chain ){
raise_target_unsupported( L10N_CHAIN_TARGETS_UNSUPPORTED );
return 0;
}

struct stumpless_target *
stumpless_new_chain( const char *name ){
raise_target_unsupported( L10N_CHAIN_TARGETS_UNSUPPORTED );
return NULL;
}
7 changes: 3 additions & 4 deletions src/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
#include <stumpless/target.h>
#include <stumpless/error.h>
#include <stumpless/target/buffer.h>
#include <stumpless/target/chain.h>
#include <stumpless/target/file.h>
#include <stumpless/target/function.h>
#include <stumpless/target/stream.h>
#include "private/config.h"
#include "private/config/locale/wrapper.h"
#include "private/config/wrapper/chain.h"
#include "private/config/wrapper/open_default_target.h"
#include "private/config/wrapper/wel.h"
#include "private/config/wrapper/journald.h"
Expand All @@ -59,7 +59,6 @@
#include "private/strhelper.h"
#include "private/target.h"
#include "private/target/buffer.h"
#include "private/target/chain.h"
#include "private/target/file.h"
#include "private/target/function.h"
#include "private/target/stream.h"
Expand Down Expand Up @@ -212,7 +211,7 @@ stumpless_add_entry( struct stumpless_target *target,

// chain targets simply pass the entry on
if( target->type == STUMPLESS_CHAIN_TARGET ) {
result = sendto_chain( target->id, entry );
result = config_send_entry_to_chain_target( target->id, entry );
goto finish;
}

Expand Down Expand Up @@ -397,7 +396,7 @@ stumpless_close_target( struct stumpless_target *target ) {
break;

case STUMPLESS_CHAIN_TARGET:
stumpless_close_chain_and_contents( target );
config_close_chain_and_contents( target );
break;

case STUMPLESS_FILE_TARGET:
Expand Down
50 changes: 25 additions & 25 deletions src/target/chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

struct stumpless_target *
stumpless_add_target_to_chain( struct stumpless_target *chain,
struct stumpless_target *target ) {
struct stumpless_target *target ){
struct chain_target *internal_chain;
size_t overflow_count;
size_t old_size;
Expand All @@ -47,7 +47,7 @@ stumpless_add_target_to_chain( struct stumpless_target *chain,

lock_target( chain );

if( chain->type != STUMPLESS_CHAIN_TARGET ) {
if( chain->type != STUMPLESS_CHAIN_TARGET ){
raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
result = NULL;
goto finish;
Expand All @@ -56,12 +56,12 @@ stumpless_add_target_to_chain( struct stumpless_target *chain,
internal_chain = chain->id;
lock_chain_target( internal_chain );

if( unlikely( internal_chain->target_count >= CHAIN_TARGET_ARRAY_LENGTH ) ) {
if( unlikely( internal_chain->target_count >= CHAIN_TARGET_ARRAY_LENGTH ) ){
overflow_count = internal_chain->target_count - CHAIN_TARGET_ARRAY_LENGTH;
old_size = overflow_count * sizeof( target );
new_size = old_size + sizeof( target );
new_targets = realloc_mem( internal_chain->overflow_targets, new_size );
if( !new_targets ) {
if( !new_targets ){
result = NULL;
goto internal_finish;
}
Expand All @@ -82,21 +82,21 @@ stumpless_add_target_to_chain( struct stumpless_target *chain,
}

void
stumpless_close_chain_and_contents( struct stumpless_target *chain ) {
stumpless_close_chain_and_contents( struct stumpless_target *chain ){
struct chain_target *internal_target;
size_t i;
struct stumpless_target *curr;

VALIDATE_ARG_NOT_NULL_VOID_RETURN( chain );

if( unlikely( chain->type != STUMPLESS_CHAIN_TARGET ) ) {
if( unlikely( chain->type != STUMPLESS_CHAIN_TARGET ) ){
raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
return;
}

internal_target = chain->id;
for( i = 0; i < internal_target->target_count; i++ ) {
if( unlikely( i >= CHAIN_TARGET_ARRAY_LENGTH ) ) {
for( i = 0; i < internal_target->target_count; i++ ){
if( unlikely( i >= CHAIN_TARGET_ARRAY_LENGTH ) ){
curr = internal_target->overflow_targets[i - CHAIN_TARGET_ARRAY_LENGTH];
} else {
curr = internal_target->targets[i];
Expand All @@ -111,10 +111,10 @@ stumpless_close_chain_and_contents( struct stumpless_target *chain ) {
}

void
stumpless_close_chain_only( struct stumpless_target *chain ) {
stumpless_close_chain_only( struct stumpless_target *chain ){
VALIDATE_ARG_NOT_NULL_VOID_RETURN( chain );

if( unlikely( chain->type != STUMPLESS_CHAIN_TARGET ) ) {
if( unlikely( chain->type != STUMPLESS_CHAIN_TARGET ) ){
raise_target_incompatible( L10N_INVALID_TARGET_TYPE_ERROR_MESSAGE );
return;
}
Expand All @@ -125,13 +125,13 @@ stumpless_close_chain_only( struct stumpless_target *chain ) {
}

size_t
stumpless_get_chain_length( const struct stumpless_target *chain ) {
stumpless_get_chain_length( const struct stumpless_target *chain ){
struct chain_target *internal_chain;
size_t result;

VALIDATE_ARG_NOT_NULL_UNSIGNED_RETURN( chain );

if( unlikely( !chain->id ) ) {
if( unlikely( !chain->id ) ){
raise_invalid_id( );
return -1;
}
Expand All @@ -144,19 +144,19 @@ stumpless_get_chain_length( const struct stumpless_target *chain ) {
}

struct stumpless_target *
stumpless_new_chain( const char *name ) {
stumpless_new_chain( const char *name ){
struct stumpless_target *target;

VALIDATE_ARG_NOT_NULL( name );

target = new_target( STUMPLESS_CHAIN_TARGET, name );

if( unlikely( !target ) ) {
if( unlikely( !target ) ){
goto fail;
}

target->id = new_chain_target();
if( unlikely( !target->id ) ) {
if( unlikely( !target->id ) ){
goto fail_id;
}

Expand All @@ -172,23 +172,23 @@ stumpless_new_chain( const char *name ) {
/* private definitions */

void
destroy_chain_target( const struct chain_target *chain ) {
destroy_chain_target( const struct chain_target *chain ){
config_destroy_mutex( &chain->chain_mutex );
free_mem( chain->overflow_targets );
free_mem( chain );
}

void
lock_chain_target( struct chain_target *chain ) {
lock_chain_target( struct chain_target *chain ){
config_lock_mutex( &chain->chain_mutex );
}

struct chain_target *
new_chain_target( void ) {
new_chain_target( void ){
struct chain_target *target;

target = alloc_mem( sizeof( *target ) );
if( unlikely( !target ) ) {
if( unlikely( !target ) ){
return NULL;
}

Expand All @@ -200,24 +200,24 @@ new_chain_target( void ) {
}

int
sendto_chain( struct chain_target *chain,
const struct stumpless_entry *entry ) {
send_entry_to_chain_target( struct chain_target *chain,
const struct stumpless_entry *entry ){
size_t i;
int result;
int final_result = 1;
struct stumpless_target *sub_target;

lock_chain_target( chain );

for( i = 0; i < chain->target_count; i++ ) {
if( i < CHAIN_TARGET_ARRAY_LENGTH ) {
for( i = 0; i < chain->target_count; i++ ){
if( i < CHAIN_TARGET_ARRAY_LENGTH ){
sub_target = chain->targets[i];
} else {
sub_target = chain->overflow_targets[i - CHAIN_TARGET_ARRAY_LENGTH];
}

result = stumpless_add_entry( sub_target, entry );
if( unlikely( result < 0 ) ) {
if( unlikely( result < 0 ) ){
final_result = result;
}
}
Expand All @@ -227,6 +227,6 @@ sendto_chain( struct chain_target *chain,
}

void
unlock_chain_target( struct chain_target *chain ) {
unlock_chain_target( struct chain_target *chain ){
config_unlock_mutex( &chain->chain_mutex );
}
4 changes: 3 additions & 1 deletion tools/check_headers/stumpless_private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"config_atomic_bool_true": "private/config/wrapper/thread_safety.h"
"config_atomic_ptr_t": "private/config/wrapper/thread_safety.h"
"config_check_mutex_valid": "private/config/wrapper/thread_safety.h"
"config_close_chain_and_contents": "private/config/wrapper/chain.h"
"config_close_journald_target": "private/config/wrapper/journald.h"
"config_close_socket_target": "private/config/wrapper/socket.h"
"config_close_sqlite3_target_and_db": "private/config/wrapper/sqlite3.h"
Expand Down Expand Up @@ -37,6 +38,7 @@
"config_network_provider_free_all": "private/config/wrapper/network_supported.h"
"config_read_flag": "private/config/wrapper/thread_safety.h"
"config_read_ptr": "private/config/wrapper/thread_safety.h"
"config_send_entry_to_chain_target": "private/config/wrapper/chain.h"
"config_send_entry_to_journald_target": "private/config/wrapper/journald.h"
"config_send_entry_to_sqlite3_target": "private/config/wrapper/sqlite3.h"
"config_sendto_socket_target": "private/config/wrapper/socket.h"
Expand Down Expand Up @@ -146,12 +148,12 @@
"RFC_5424_TIMESTAMP_REGEX_STRING": "test/helper/rfc5424.hpp"
"RFC_5424_WHOLE_TIME_BUFFER_SIZE": "private/formatter.h"
"send_entry_and_msg_to_unsupported_target": "private/target.h"
"send_entry_to_chain_target": "private/target/chain.h"
"send_entry_to_function_target": "private/target/function.h"
"send_entry_to_journald_target": "private/target/journald.h"
"send_entry_to_sqlite3_target": "private/target/sqlite3.h"
"send_entry_to_unsupported_target": "private/target.h"
"sendto_buffer_target": "private/target/buffer.h"
"sendto_chain": "private/target/chain.h"
"sendto_file_target": "private/target/file.h"
"sendto_network_target": "private/target/network.h"
"sendto_socket_target": "private/target/socket.h"
Expand Down

0 comments on commit 5726ceb

Please sign in to comment.