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

Demonstrates how to work with a chain target.

Demonstrates how to work with a chain target.

// 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 <stdio.h>
#include <stdlib.h>
#include <stumpless.h>
int
main( void ){
struct stumpless_target *simple_chain;
struct stumpless_target *file_target;
struct stumpless_target *stdout_target;
int mask;
char error_buffer[4096];
struct stumpless_target *stderr_target;
struct stumpless_target *error_buffer_target;
struct stumpless_target *error_chain;
struct stumpless_target *main_chain;
char message_buffer[4096];
size_t read_size;
// a file target to hold all of our logs in one file
file_target = stumpless_open_file_target( "chain-example.log" );
// set up a target to log to stdout
stdout_target = stumpless_open_stdout_target( "chain-example-stdout" );
// only send "normal" messages to stdout though: from info to warning
mask = STUMPLESS_SEVERITY_MASK_UPTO( STUMPLESS_SEVERITY_INFO )
& ~STUMPLESS_SEVERITY_MASK_UPTO( STUMPLESS_SEVERITY_ERR );
stumpless_set_target_mask( stdout_target, mask );
// create a new chain
simple_chain = stumpless_new_chain( "example-simple-chain" );
// add our targets to the chain
stumpless_add_target_to_chain( simple_chain, stdout_target );
stumpless_add_target_to_chain( simple_chain, file_target );
// send a test message to both stdout and chain-example.log
stumpless_add_message( simple_chain, "simple chain message" );
// set up a target to handle errors
stderr_target = stumpless_open_stderr_target( "chain-example-stderr" );
error_buffer_target = stumpless_open_buffer_target( "chain-example-buffer",
error_buffer,
sizeof( error_buffer ) );
error_chain = stumpless_new_chain( "example-error-chain" );
stumpless_add_target_to_chain( error_chain, stderr_target );
stumpless_add_target_to_chain( error_chain, error_buffer_target );
mask = STUMPLESS_SEVERITY_MASK_UPTO( STUMPLESS_SEVERITY_ERR );
// make a top-level chain for all messages to go to
main_chain = stumpless_new_chain( "example-main-chain" );
stumpless_add_target_to_chain( main_chain, error_chain );
stumpless_add_target_to_chain( main_chain, simple_chain );
// since the main chain was the last target opened,
// we can still just send messages to the default 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" );
// print out the entries in the error buffer
printf( "\nerrors logged in memory:\n" );
do {
read_size = stumpless_read_buffer( error_buffer_target,
message_buffer,
sizeof( message_buffer ) );
printf( "%s\n", message_buffer );
} while( read_size > 1 );
// this closes all targets in the chain
// some final cleanup
return EXIT_SUCCESS;
}
#define stump_a(...)
Logs a message to the current target with alert severity.
Definition alert.h:97
STUMPLESS_PUBLIC_FUNCTION size_t stumpless_read_buffer(struct stumpless_target *target, char *buffer, size_t max_length)
Reads the next message from the provided buffer target and writes it into the given buffer.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_buffer_target(const char *name, char *buffer, size_t size)
Creates a buffer target for the given buffer.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_new_chain(const char *name)
Creates a new target chain.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_add_target_to_chain(struct stumpless_target *chain, struct stumpless_target *target)
Adds a target to an existing chain target.
STUMPLESS_PUBLIC_FUNCTION void stumpless_close_chain_and_contents(struct stumpless_target *chain)
Closes a chain of targets.
#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
#define stump_er(...)
Logs a message to the current target with err severity.
Definition err.h:97
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_file_target(const char *name)
Opens a file target.
#define stump_i(...)
Logs a message to the current target with info severity.
Definition info.h:97
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_UPTO(SEVERITY)
Creates a severity mask from EMERG up to the provided severity.
Definition severity.h:48
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_stdout_target(const char *name)
Opens a stream target for the stdout stream.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_stderr_target(const char *name)
Opens a stream target for the stderr stream.
A target that log entries can be sent to.
Definition target.h:140
int mask
The log mask for the target.
Definition target.h:181
The main header file for the stumpless logging library.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_set_target_mask(struct stumpless_target *target, int mask)
Sets the log mask of a target.
STUMPLESS_PUBLIC_FUNCTION int stumpless_add_message(struct stumpless_target *target, const char *message,...)
Adds a message to 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