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

Demonstrates how to work with runtime filters on logging targets.

Demonstrates how to work with runtime filters on logging targets.

// 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 <stdbool.h>
#include <stdlib.h>
#include <stumpless.h>
// this is our custom filter that rejects entries with an element named 'ignore'
// in them, as well as honoring the default target mask behavior
bool
ignore_element_filter( const struct stumpless_target *target,
const struct stumpless_entry *entry ) {
return !stumpless_get_element_by_name( entry, "ignore" )
&& stumpless_mask_filter( target, entry );
}
int
main( int argc, char **argv ) {
struct stumpless_target *target;
int new_mask;
struct stumpless_entry *entry;
// opens a target to stdout
target = stumpless_open_stdout_target( "filter-example-target" );
// by default, all of these will be logged to the target
stump_em( "emergency!" );
stump_a( "alert!" );
stump_c( "critical!" );
stump_er( "error!" );
stump_w( "warning!" );
stump_n( "notice" );
stump_i( "informational" );
stump_d( "debug" );
stump_t( "trace" );
// the resulting output looks something like this:
// <8>1 2022-01-26T02:00:54.250910Z Angus - - - - emergency!
// <9>1 2022-01-26T02:00:54.251708Z Angus - - - - alert!
// <10>1 2022-01-26T02:00:54.251989Z Angus - - - - critical!
// <11>1 2022-01-26T02:00:54.252183Z Angus - - - - error!
// <12>1 2022-01-26T02:00:54.252845Z Angus - - - - warning!
// <13>1 2022-01-26T02:00:54.253226Z Angus - - - - notice
// <14>1 2022-01-26T02:00:54.253634Z Angus - - - - informational
// <15>1 2022-01-26T02:00:54.254015Z Angus - - - - debug
// <15>1 2022-01-26T02:00:54.254347Z Angus - - - [trace file="/mnt/c/users/reall/code/stumpless/docs/examples/filter/filter_example.c" line="52" function="main"] trace
// if we want to change the mask to be more complex, we can create our new
// mask and assign it to the target (or use the stumplog_set_mask shortcut
// if it happens to be the current target)
new_mask = STUMPLESS_SEVERITY_MASK( STUMPLESS_SEVERITY_DEBUG )
| STUMPLESS_SEVERITY_MASK_UPTO( STUMPLESS_SEVERITY_ERR );
stumplog_set_mask( new_mask );
//. now, we'll only see the first four of these and the debug one
stump_em( "emergency!" );
stump_a( "alert!" );
stump_c( "critical!" );
stump_er( "error!" );
stump_w( "warning!" );
stump_n( "notice" );
stump_i( "informational" );
stump_d( "debug" );
stump_t( "trace" );
// the mask filtered output:
// <8>1 2022-01-26T02:00:54.254749Z Angus - - - - emergency!
// <9>1 2022-01-26T02:00:54.255044Z Angus - - - - alert!
// <10>1 2022-01-26T02:00:54.255394Z Angus - - - - critical!
// <11>1 2022-01-26T02:00:54.255691Z Angus - - - - error!
// <15>1 2022-01-26T02:00:54.256015Z Angus - - - - debug
// <15>1 2022-01-26T02:00:54.256365Z Angus - - - [trace file="/mnt/c/users/reall/code/stumpless/docs/examples/filter/filter_example.c" line="84" function="main"] trace
// if we want to customize the filtering even more, then we can set the filter
// to our own custom function
stumpless_set_target_filter( target, ignore_element_filter );
// let's create an entry to send to our target
entry = stumpless_new_entry( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_ERR,
"filter-example",
"annoying-msg",
"hey! listen!" );
// this message will be allowed through
stumpless_add_entry( target, entry );
// message logged to the target:
// <11>1 2022-01-26T02:00:54.256697Z Angus filter-example - annoying-msg - hey! listen!
// now, let's add the special element and try again
stumpless_add_new_element( entry, "ignore" );
// this time, it'll be filtered out!
stumpless_add_entry( target, entry );
// there's no output to be seen!
// destroying all the resources before finishing up
return EXIT_SUCCESS;
}
#define stump_a(...)
Logs a message to the current target with alert severity.
Definition alert.h:97
#define stump_c(...)
Logs a message to the current target with crit severity.
Definition crit.h:97
#define stump_d(...)
Logs a message to the current target with debug severity.
Definition debug.h:97
#define stump_em(...)
Logs a message to the current target with emerg severity.
Definition emerg.h:97
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 struct stumpless_element * stumpless_get_element_by_name(const struct stumpless_entry *entry, const char *name)
Returns the element with the given name in this entry, if it is found.
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_add_new_element(struct stumpless_entry *entry, const char *name)
Creates a new element with the given name and adds it to this entry.
#define stump_er(...)
Logs a message to the current target with err severity.
Definition err.h:97
STUMPLESS_PUBLIC_FUNCTION bool stumpless_mask_filter(const struct stumpless_target *target, const struct stumpless_entry *entry)
Compares the severity of the entry to the current mask of the target, and only passes the entry if th...
#define stump_i(...)
Logs a message to the current target with info severity.
Definition info.h:97
STUMPLESS_PUBLIC_FUNCTION int stumplog_set_mask(int mask)
Sets the log mask of the current target.
STUMPLESS_PUBLIC_FUNCTION void stumpless_free_all(void)
Closes the default target if it has been opened, frees all memory allocated internally,...
#define stump_n(...)
Logs a message to the current target with notice severity.
Definition notice.h:97
#define STUMPLESS_SEVERITY_MASK(SEVERITY)
Creates a severity mask for the provided severity.
Definition severity.h:41
#define STUMPLESS_SEVERITY_MASK_UPTO(SEVERITY)
Creates a severity mask from EMERG up to the provided severity.
Definition severity.h:48
STUMPLESS_PUBLIC_FUNCTION void stumpless_close_stream_target(const struct stumpless_target *target)
Closes a stream target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_stdout_target(const char *name)
Opens a stream target for the stdout stream.
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 struct stumpless_target * stumpless_set_target_filter(struct stumpless_target *target, stumpless_filter_func_t filter)
Sets the filter used to determine whether entries should be logged by a given target.
STUMPLESS_PUBLIC_FUNCTION int stumpless_add_entry(struct stumpless_target *target, const struct stumpless_entry *entry)
Adds an entry into a given target.
#define stump_t(...)
Logs a message to the current target with debug severity, along with the file, line,...
Definition trace.h:106
#define stump_w(...)
Logs a message to the current target with warning severity.
Definition warning.h:97