stumpless  2.1.0
target.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 /*
4  * Copyright 2018-2022 Joel E. Anderson
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
27 #ifndef __STUMPLESS_TARGET_H
28 # define __STUMPLESS_TARGET_H
29 
30 # include <stdarg.h>
31 # include <stdbool.h>
32 # include <stddef.h>
33 # include <stdio.h>
34 # include <stumpless/config.h>
35 # include <stumpless/entry.h>
36 # include <stumpless/id.h>
37 # include <stumpless/generator.h>
38 
40 # define STUMPLESS_DEFAULT_FILE "stumpless-default.log"
41 
43 # define STUMPLESS_DEFAULT_TARGET_NAME "stumpless-default"
44 
45 # ifdef __cplusplus
46 extern "C" {
47 # endif
48 
50 # define STUMPLESS_BUFFER_TARGET_VALUE 0
51 
53 # define STUMPLESS_FILE_TARGET_VALUE 1
54 
56 # define STUMPLESS_FUNCTION_TARGET_VALUE 2
57 
59 # define STUMPLESS_JOURNALD_TARGET_VALUE 3
60 
62 # define STUMPLESS_NETWORK_TARGET_VALUE 4
63 
65 # define STUMPLESS_SOCKET_TARGET_VALUE 5
66 
68 # define STUMPLESS_STREAM_TARGET_VALUE 6
69 
71 # define STUMPLESS_WINDOWS_EVENT_LOG_TARGET_VALUE 7
72 
79 # define STUMPLESS_FOREACH_TARGET_TYPE( ACTION )\
80 \
81 ACTION( STUMPLESS_BUFFER_TARGET, STUMPLESS_BUFFER_TARGET_VALUE )\
82 \
83 ACTION( STUMPLESS_FILE_TARGET, STUMPLESS_FILE_TARGET_VALUE )\
84 \
85 ACTION( STUMPLESS_FUNCTION_TARGET, STUMPLESS_FUNCTION_TARGET_VALUE )\
86 \
87 ACTION( STUMPLESS_JOURNALD_TARGET, STUMPLESS_JOURNALD_TARGET_VALUE )\
88 \
89 ACTION( STUMPLESS_NETWORK_TARGET, STUMPLESS_NETWORK_TARGET_VALUE )\
90 \
91 ACTION( STUMPLESS_SOCKET_TARGET, STUMPLESS_SOCKET_TARGET_VALUE )\
92 \
93 ACTION( STUMPLESS_STREAM_TARGET, STUMPLESS_STREAM_TARGET_VALUE )\
94 \
95 ACTION( STUMPLESS_WINDOWS_EVENT_LOG_TARGET, STUMPLESS_WINDOWS_EVENT_LOG_TARGET_VALUE )
96 
100 };
101 
102 // needed so that we can define the filter function type before targets
103 struct stumpless_target;
104 
130 typedef bool ( *stumpless_filter_func_t )(
131  const struct stumpless_target *target,
132  const struct stumpless_entry *entry );
133 
137 struct stumpless_target {
154  char *name;
156  size_t name_length;
158  int options;
160  int default_prival;
176  size_t default_msgid_length;
178  int mask;
187 # ifdef STUMPLESS_THREAD_SAFETY_SUPPORTED
188 
192  void *mutex;
193 # endif
194 };
195 
227 int
228 stumpless_add_entry( struct stumpless_target *target,
229  const struct stumpless_entry *entry );
230 
278 int
279 stumpless_add_log( struct stumpless_target *target,
280  int priority,
281  const char *message,
282  ... );
283 
322 int
324  int priority,
325  const char *message );
326 
371 int
373  const char *message,
374  ... );
375 
412 int
414  const char *message );
415 
442 void
443 stumpless_close_target( struct stumpless_target *target );
444 
445 /*
446  * Gets the current console stream where logs are written to.
447  *
448  * **Thread Safety: MT-Safe**
449  * This function is thread safe. Atomic variables are used to store and
450  * retrieve the console stream.
451  *
452  * **Async Signal Safety: AS-Safe**
453  * This function is safe to call from signal handlers.
454  *
455  * **Async Cancel Safety: AC-Safe**
456  * This function is safe to call from threads that may be ansynchronously
457  * cancelled.
458  *
459  * @since v2.1.0
460  *
461  * @return The current stream where messages are logged to on setting the
462  * the CONS option.
463  */
465 FILE *
467 
501 
523 int
524 stumpless_get_default_facility( const struct stumpless_target *target );
525 
572 
600 int
601 stumpless_get_option( const struct stumpless_target *target, int option );
602 
630 const char *
632 
660 const char *
662 
692 stumpless_get_target_filter( const struct stumpless_target *target );
693 
722 int
723 stumpless_get_target_mask( const struct stumpless_target *target );
724 
751 const char *
752 stumpless_get_target_name( const struct stumpless_target *target );
753 
785 stumpless_open_target( struct stumpless_target *target );
786 
810 void
811 stumpless_set_cons_stream( FILE *stream );
812 
836 void
838 
865  int default_facility );
866 
892 stumpless_set_option( struct stumpless_target *target, int option );
893 
923  const char *app_name );
924 
955  const char *msgid );
956 
987 
1019 stumpless_set_target_mask( struct stumpless_target *target, int mask );
1020 
1050 const struct stumpless_target *
1051 stumpless_target_is_open( const struct stumpless_target *target );
1052 
1095 int
1096 stumpless_trace_entry( struct stumpless_target *target,
1097  struct stumpless_entry *entry,
1098  const char *file,
1099  int line,
1100  const char *func );
1101 
1160 int
1161 stumpless_trace_log( struct stumpless_target *target,
1162  int priority,
1163  const char *file,
1164  int line,
1165  const char *func,
1166  const char *message,
1167  ... );
1168 
1218 int
1220  int priority,
1221  const char *file,
1222  int line,
1223  const char *func,
1224  const char *message );
1225 
1281 int
1283  const char *file,
1284  int line,
1285  const char *func,
1286  const char *message,
1287  ... );
1288 
1336 int
1338  const char *file,
1339  int line,
1340  const char *func,
1341  const char *message );
1342 
1368 stumpless_unset_option( struct stumpless_target *target, int option );
1369 
1412 int
1413 vstumpless_add_log( struct stumpless_target *target,
1414  int priority,
1415  const char *message,
1416  va_list subs );
1417 
1457 int
1459  const char *message,
1460  va_list subs );
1461 
1514 int
1515 vstumpless_trace_log( struct stumpless_target *target,
1516  int priority,
1517  const char *file,
1518  int line,
1519  const char *func,
1520  const char *message,
1521  va_list subs );
1522 
1572 int
1574  const char *file,
1575  int line,
1576  const char *func,
1577  const char *message,
1578  va_list subs );
1579 
1602 const char *
1604 
1605 
1606 # ifdef __cplusplus
1607 } /* extern "C" */
1608 # endif
1609 
1610 #endif /* __STUMPLESS_TARGET_H */
stumpless_set_target_default_app_name
struct stumpless_target * stumpless_set_target_default_app_name(struct stumpless_target *target, const char *app_name)
Sets the default app name for a given target.
stumpless_get_target_default_msgid
const char * stumpless_get_target_default_msgid(const struct stumpless_target *target)
Returns the default msgid of the given target.
stumpless_get_option
int stumpless_get_option(const struct stumpless_target *target, int option)
Gets a given option of a target.
stumpless_set_cons_stream
void stumpless_set_cons_stream(FILE *stream)
Sets the console stream to write logs to.
stumpless_set_target_default_msgid
struct stumpless_target * stumpless_set_target_default_msgid(struct stumpless_target *target, const char *msgid)
Sets the default msgid for a given target.
stumpless_trace_message
int stumpless_trace_message(struct stumpless_target *target, const char *file, int line, const char *func, const char *message,...)
Adds a message to a given target, along with the file, line, and function information specified in a ...
stumpless_target_type
stumpless_target_type
Types of targets that may be created.
Definition: target.h:98
stumpless_target::filter
stumpless_filter_func_t filter
A filter function used to determine if a given entry should be processed by this target or ignored.
Definition: target.h:185
stumpless_add_log_str
int stumpless_add_log_str(struct stumpless_target *target, int priority, const char *message)
Adds a log message with a priority to a given target.
stumpless_target::default_prival
int default_prival
The prival used for messages without a severity or facility provided.
Definition: target.h:159
id.h
vstumpless_trace_message
int vstumpless_trace_message(struct stumpless_target *target, const char *file, int line, const char *func, const char *message, va_list subs)
Adds a message to a given target, along with the file, line, and function information specified in a ...
stumpless_target::name
char * name
The name of this target.
Definition: target.h:153
stumpless_set_target_filter
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_GENERATE_ENUM
#define STUMPLESS_GENERATE_ENUM(NAME, VALUE)
Returns a definition of a symbol with the given name and value fit for use in an enum definition.
Definition: generator.h:33
stumpless_get_target_filter
stumpless_filter_func_t stumpless_get_target_filter(const struct stumpless_target *target)
Returns the current filter used by the given target to determine if entries should be allowed through...
stumpless_target::id
stumpless_id_t id
A unique identifier of this target.
Definition: target.h:138
vstumpless_add_message
int vstumpless_add_message(struct stumpless_target *target, const char *message, va_list subs)
Adds a message to a given target.
stumpless_trace_log_str
int stumpless_trace_log_str(struct stumpless_target *target, int priority, const char *file, int line, const char *func, const char *message)
Adds a log message with a priority to a given target, along with the file, line, and function informa...
stumpless_get_target_default_app_name
const char * stumpless_get_target_default_app_name(const struct stumpless_target *target)
Returns the default app name of the given target.
vstumpless_add_log
int vstumpless_add_log(struct stumpless_target *target, int priority, const char *message, va_list subs)
Adds a log message with a priority to a given target.
stumpless_target
A target that log entries can be sent to.
Definition: target.h:136
stumpless_add_message_str
int stumpless_add_message_str(struct stumpless_target *target, const char *message)
Adds a string message to a given target.
stumpless_target::type
enum stumpless_target_type type
The type of this target.
Definition: target.h:143
entry.h
stumpless_add_log
int stumpless_add_log(struct stumpless_target *target, int priority, const char *message,...)
Adds a log message with a priority to a given target.
vstumpless_trace_log
int vstumpless_trace_log(struct stumpless_target *target, int priority, const char *file, int line, const char *func, const char *message, va_list subs)
Adds a log message with a priority to a given target, along with the file, line, and function informa...
stumpless_get_current_target
struct stumpless_target * stumpless_get_current_target(void)
Gets the current target.
STUMPLESS_MAX_APP_NAME_LENGTH
#define STUMPLESS_MAX_APP_NAME_LENGTH
The maximum length of an app name, as specified by RFC 5424.
Definition: entry.h:43
stumpless_target::default_msgid
char default_msgid[32]
The msgid used for messages without one provided.
Definition: target.h:173
stumpless_set_current_target
void stumpless_set_current_target(struct stumpless_target *target)
Sets the target used when one is not provided.
stumpless_get_target_name
const char * stumpless_get_target_name(const struct stumpless_target *target)
Returns the name of the given target.
stumpless_filter_func_t
bool(* stumpless_filter_func_t)(const struct stumpless_target *target, const struct stumpless_entry *entry)
A function that determines whether a given entry should be sent to a given target.
Definition: target.h:129
stumpless_get_default_target
struct stumpless_target * stumpless_get_default_target(void)
Gets the default target.
stumpless_target::default_msgid_length
size_t default_msgid_length
The number of characters in the default msgid.
Definition: target.h:175
stumpless_add_entry
int stumpless_add_entry(struct stumpless_target *target, const struct stumpless_entry *entry)
Adds an entry into a given target.
stumpless_close_target
void stumpless_close_target(struct stumpless_target *target)
Closes a target.
stumpless_target::name_length
size_t name_length
The number of characters in the name.
Definition: target.h:155
stumpless_target_is_open
const struct stumpless_target * stumpless_target_is_open(const struct stumpless_target *target)
Checks to see if the given target is open.
stumpless_get_cons_stream
FILE * stumpless_get_cons_stream(void)
stumpless_set_default_facility
struct stumpless_target * stumpless_set_default_facility(struct stumpless_target *target, int default_facility)
Sets the default facility of a target.
stumpless_get_target_mask
int stumpless_get_target_mask(const struct stumpless_target *target)
Gets the log mask of a target.
stumpless_unset_option
struct stumpless_target * stumpless_unset_option(struct stumpless_target *target, int option)
Unsets an option on a target.
stumpless_set_target_mask
struct stumpless_target * stumpless_set_target_mask(struct stumpless_target *target, int mask)
Sets the log mask of a target.
stumpless_open_target
struct stumpless_target * stumpless_open_target(struct stumpless_target *target)
Opens a target that has already been created and configured.
stumpless_id_t
void * stumpless_id_t
Definition: id.h:30
stumpless_trace_message_str
int stumpless_trace_message_str(struct stumpless_target *target, const char *file, int line, const char *func, const char *message)
Adds a message to a given target, along with the file, line, and function information specified in a ...
STUMPLESS_PUBLIC_FUNCTION
#define STUMPLESS_PUBLIC_FUNCTION
Marks functions that are provided for use by the library.
Definition: config.h:31
stumpless_set_option
struct stumpless_target * stumpless_set_option(struct stumpless_target *target, int option)
Sets an option on a target.
stumpless_target::mask
int mask
The log mask for the target.
Definition: target.h:177
config.h
stumpless_get_target_type_string
const char * stumpless_get_target_type_string(enum stumpless_target_type target)
Gets the string representation of the given target_type.
stumpless_add_message
int stumpless_add_message(struct stumpless_target *target, const char *message,...)
Adds a message to a given target.
stumpless_trace_entry
int stumpless_trace_entry(struct stumpless_target *target, struct stumpless_entry *entry, const char *file, int line, const char *func)
Adds an entry into a given target.
stumpless_target::mutex
void * mutex
A pointer to a mutex which protects all target fields.
Definition: target.h:191
stumpless_entry
A log entry.
Definition: entry.h:63
stumpless_target::options
int options
A bitwise or of all options set on the target.
Definition: target.h:157
STUMPLESS_FOREACH_TARGET_TYPE
#define STUMPLESS_FOREACH_TARGET_TYPE(ACTION)
A macro function that runs the provided action once for each target_type, providing the symbol and va...
Definition: target.h:79
STUMPLESS_MAX_MSGID_LENGTH
#define STUMPLESS_MAX_MSGID_LENGTH
The maximum length of a msgid, as specified by RFC 5424.
Definition: entry.h:46
stumpless_get_default_facility
int stumpless_get_default_facility(const struct stumpless_target *target)
Gets the default facility of a target.
stumpless_target::default_app_name_length
size_t default_app_name_length
The number of characters in the default app name.
Definition: target.h:167
generator.h
stumpless_trace_log
int stumpless_trace_log(struct stumpless_target *target, int priority, const char *file, int line, const char *func, const char *message,...)
Adds a log message with a priority to a given target, along with the file, line, and function informa...
stumpless_target::default_app_name
char default_app_name[48]
The app name used for messages without one provided.
Definition: target.h:165