stumpless 2.2.0
Loading...
Searching...
No Matches
function_example.c

Demonstrates how to work with a function target.

Demonstrates how to work with a function target.

// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 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 <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stumpless.h>
#include <time.h>
int red_count = 0;
int green_count = 0;
int blue_count = 0;
// this function provides our color-counting logic
int
count_balls( const struct stumpless_target *target,
const struct stumpless_entry *entry ) {
const char *color;
int result;
color = stumpless_get_entry_param_value_by_name( entry, "ball", "color" );
if( strcmp( color, "red" ) == 0 ) {
result = ++red_count;
} else if( strcmp( color, "green" ) == 0 ) {
result = ++green_count;
} else if( strcmp( color, "blue" ) == 0 ) {
result = ++blue_count;
} else {
result = -1;
}
free( ( void * ) color );
return result;
}
int
main( void ) {
struct stumpless_target *target;
struct stumpless_entry *entry;
int i;
const char *ball_colors[] = {"red", "green" , "blue"};
const char *color;
// creating a target using our custom function
target = stumpless_open_function_target( "ball-counter", count_balls );
// creating an entry for ball processing events
entry = stumpless_new_entry( STUMPLESS_FACILITY_DAEMON,
STUMPLESS_SEVERITY_INFO,
"ball-processing-machine",
"processed-ball",
"a ball has finished processing" );
// processing a bunch of of fake balls
srand( time( NULL ) );
for( i = 0; i < 1024; i++ ) {
color = ball_colors[rand( ) % 3];
stumpless_set_entry_param_value_by_name( entry, "ball", "color", color );
// this line calls our custom function with the same target and entry
stumpless_add_entry( target, entry );
}
// printing out the final counts
printf( "red ball count: %d\n", red_count );
printf( "green ball count: %d\n", green_count );
printf( "blue ball count: %d\n", blue_count );
// some final cleanup before we finish
return EXIT_SUCCESS;
}
STUMPLESS_PUBLIC_FUNCTION void stumpless_destroy_entry_and_contents(const struct stumpless_entry *entry)
Destroys an entry as well as all elements and params that it contains, freeing any allocated memory.
STUMPLESS_PUBLIC_FUNCTION const char * stumpless_get_entry_param_value_by_name(const struct stumpless_entry *entry, const char *element_name, const char *param_name)
Gets the value of the first param from the element with the given name in an entry.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_new_entry(enum stumpless_facility facility, enum stumpless_severity severity, const char *app_name, const char *msgid, const char *message,...)
Creates a new entry with the given characteristics.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_set_entry_param_value_by_name(struct stumpless_entry *entry, const char *element_name, const char *param_name, const char *value)
Sets the value of the first param in the named element an entry.
STUMPLESS_PUBLIC_FUNCTION void stumpless_close_function_target(const struct stumpless_target *target)
Closes a function target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_function_target(const char *name, stumpless_log_func_t log_function)
Opens a function target.
STUMPLESS_PUBLIC_FUNCTION void stumpless_free_all(void)
Closes the default target if it has been opened, frees all memory allocated internally,...
A log entry.
Definition entry.h:60
A target that log entries can be sent to.
Definition target.h:140
The main header file for the stumpless logging library.
STUMPLESS_PUBLIC_FUNCTION int stumpless_add_entry(struct stumpless_target *target, const struct stumpless_entry *entry)
Adds an entry into a given target.