Skip to content

Commit c934e2b

Browse files
committed
add map function to targets
1 parent d2f2193 commit c934e2b

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

include/private/entry.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* SPDX-License-Identifier: Apache-2.0 */
22

33
/*
4-
* Copyright 2018-2024 Joel E. Anderson
4+
* Copyright 2018-2025 Joel E. Anderson
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -385,7 +385,8 @@ strbuilder_append_structured_data( struct strbuilder *builder,
385385
* Not safe in the context of asynchronous cancellation due to potential resource
386386
* cleanup issues.
387387
*
388-
* @since release v1.5.0lock_entry
388+
* @since release v1.5.0
389+
*
389390
* @param entry A pointer to the entry to be destroyed. Must not be NULL.
390391
*/
391392
void

include/stumpless/filter.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* SPDX-License-Identifier: Apache-2.0 */
22

33
/*
4-
* Copyright 2022 Joel E. Anderson
4+
* Copyright 2022-2025 Joel E. Anderson
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -24,16 +24,16 @@
2424
*/
2525

2626
#ifndef __STUMPLESS_FILTER_H
27-
# define __STUMPLESS_FILTER_H
27+
#define __STUMPLESS_FILTER_H
2828

29-
# include <stdbool.h>
30-
# include <stumpless/config.h>
31-
# include <stumpless/entry.h>
32-
# include <stumpless/target.h>
29+
#include <stdbool.h>
30+
#include <stumpless/config.h>
31+
#include <stumpless/entry.h>
32+
#include <stumpless/target.h>
3333

34-
# ifdef __cplusplus
34+
#ifdef __cplusplus
3535
extern "C" {
36-
# endif
36+
#endif
3737

3838
/**
3939
* Compares the severity of the entry to the current mask of the target, and
@@ -49,24 +49,27 @@ extern "C" {
4949
*
5050
* **Async Cancel Safety: AC-Unsafe lock**
5151
* This function is not safe to call from threads that may be asynchronously
52-
* cancelled, due to the use of a lock that could be left locked..
52+
* cancelled, due to the use of a lock that could be left locked.
5353
*
5454
* @since release v2.1.0
5555
*
5656
* @param target The target that the entry will be sent to if it passes.
5757
*
5858
* @param entry The entry that is being submitted to the target.
5959
*
60+
* @param data Unused.
61+
*
6062
* @return true if the severity of the entry is set in the target's mask,
6163
* false otherwise.
6264
*/
6365
STUMPLESS_PUBLIC_FUNCTION
6466
bool
6567
stumpless_mask_filter( const struct stumpless_target *target,
66-
const struct stumpless_entry *entry );
68+
const struct stumpless_entry *entry,
69+
void *data );
6770

68-
# ifdef __cplusplus
71+
#ifdef __cplusplus
6972
} /* extern "C" */
70-
# endif
73+
#endif
7174

7275
#endif /* __STUMPLESS_FILTER_H */

include/stumpless/target.h

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,41 @@ struct stumpless_target;
128128
* @param entry The entry that is being submitted to the target. Will not be
129129
* NULL when called during logging.
130130
*
131+
* @param data A pointer to data that may hold anything that the filter function
132+
* needs to use in addition to the target and entry.
133+
*
131134
* @return true if the entry should be sent to the target, false if not.
132135
*/
133136
typedef bool ( *stumpless_filter_func_t )(
134137
const struct stumpless_target *target,
135-
const struct stumpless_entry *entry );
138+
const struct stumpless_entry *entry,
139+
void *data );
140+
141+
/**
142+
* A function that maps a given entry to another.
143+
*
144+
* Map functions are useful for adding, removing, or otherwise modifying an
145+
* entry before it is logged to a target. They return a pointer to the mapped
146+
* entry.
147+
*
148+
* Map functions should not modify the entry they operate on. If no modification
149+
* is needed, then it is better to simply not specify a map function.
150+
*
151+
* @since release v3.0.0
152+
*
153+
* @param target The target that the map function was called as part of.
154+
*
155+
* @param entry The entry that this map function will base its output on.
156+
*
157+
* @param data A pointer to data that may hold anything that the map function
158+
* needs to use in addition to the target and entry.
159+
*
160+
* @return A pointer to the mapped entry.
161+
*/
162+
typedef struct stumpless_entry * ( *stumpless_map_func_t )(
163+
const struct stumpless_target *target,
164+
const struct stumpless_entry *entry,
165+
void *data );
136166

137167
/**
138168
* A target that log entries can be sent to.
@@ -188,6 +218,30 @@ struct stumpless_target {
188218
* @since release v2.1.0
189219
*/
190220
stumpless_filter_func_t filter;
221+
/**
222+
* A pointer to data which may be used by the filter function.
223+
*
224+
* @since release v3.0.0
225+
*/
226+
void *filter_data;
227+
/**
228+
* A mapping function which will map the entry the target recieved to one that
229+
* will be logged. This supports things including redacting specific items,
230+
* adding new elements automatically, or mapping element or parameter values to
231+
* other entry fields.
232+
*
233+
* If present, the map function is called after the filter, before the entry is
234+
* logged to the target.
235+
*
236+
* @since release v3.0.0
237+
*/
238+
stumpless_map_func_t map;
239+
/**
240+
* A pointer to data which may be used by the map function.
241+
*
242+
* @since release v3.0.0
243+
*/
244+
void *map_data;
191245
#ifdef STUMPLESS_THREAD_SAFETY_SUPPORTED
192246
/**
193247
* A pointer to a mutex which protects all target fields. The exact type of

src/filter.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
/*
4-
* Copyright 2022 Joel E. Anderson
4+
* Copyright 2022-2025 Joel E. Anderson
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -23,7 +23,10 @@
2323

2424
bool
2525
stumpless_mask_filter( const struct stumpless_target *target,
26-
const struct stumpless_entry *entry ) {
26+
const struct stumpless_entry *entry,
27+
void *data ) {
28+
(void) data;
29+
2730
return STUMPLESS_SEVERITY_MASK( stumpless_get_entry_severity( entry ) )
2831
& stumpless_get_target_mask( target );
2932
}

src/target.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ stumpless_add_entry( struct stumpless_target *target,
210210
}
211211

212212
filter = stumpless_get_target_filter( target );
213-
if( filter && !filter( target, entry ) ) {
213+
if( filter && !filter( target, entry, target->filter_data ) ) {
214214
return 0;
215215
}
216216

@@ -1174,6 +1174,9 @@ new_target( enum stumpless_target_type type, const char *name ) {
11741174
target->default_msgid_length = 1;
11751175
target->mask = STUMPLESS_SEVERITY_MASK_UPTO( STUMPLESS_SEVERITY_DEBUG_VALUE );
11761176
target->filter = stumpless_mask_filter;
1177+
target->filter_data = NULL;
1178+
target->map = NULL;
1179+
target->map_data = NULL;
11771180

11781181
return target;
11791182

0 commit comments

Comments
 (0)