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

Demonstrates how to work with a network target with a UDP network endpoint.

Demonstrates how to work with a network target with a UDP network endpoint.

// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2019 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 <stdlib.h>
#include <stumpless.h>
int
main( int argc, char **argv ) {
const char *log_server = "example.com"; // change this if you want to test
// with your own server
struct stumpless_target *udp_target;
struct stumpless_target *target_result;
struct stumpless_entry *basic_entry;
struct stumpless_element *element;
struct stumpless_element *element_result;
struct stumpless_param *param;
struct stumpless_entry *result;
int log_result;
// building the entry is done as usual
basic_entry = stumpless_new_entry( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_INFO,
"example-app-name",
"example-msgid",
"This is an example message, but it is"
" pretty long. It'll be truncated if the"
" maximum message size is set too low." );
if( !basic_entry ) {
stumpless_perror( "could not create a basic entry" );
return EXIT_FAILURE;
}
element = stumpless_new_element( "basic-element" );
result = stumpless_add_element( basic_entry, element );
if( !result ) {
stumpless_perror( "could not create and add an element to the entry" );
return EXIT_FAILURE;
}
param = stumpless_new_param( "basic-param-name", "basic-param-value" );
element_result = stumpless_add_param( element, param );
if( !element_result ) {
stumpless_perror( "could not create and add a param to the element" );
return EXIT_FAILURE;
}
// opening the target is straightforward
udp_target = stumpless_open_udp4_target( "udp4-example", log_server );
// if you wanted to use ipv6 instead, you would do this:
//udp_target = stumpless_open_udp6_target( "udp6-example", log_server );
if( !udp_target ) {
stumpless_perror( "couldn't open up the udp4 target" );
return EXIT_FAILURE;
}
// sending the entry is just like normal
log_result = stumpless_add_entry( udp_target, basic_entry );
if( log_result < 0 ) {
stumpless_perror( "could not log an entry" );
return EXIT_FAILURE;
}
// UDP messages will be truncated if they go over the maximum message size.
// This is set to 1472 by default, assuming an MTU of 1500 and a typical
// overhead of 28 bytes. However, you can tweak this setting if you need to.
// Messages that go over are simply truncated and sent.
target_result = stumpless_set_udp_max_message_size( udp_target, 200 );
if( !target_result ) {
stumpless_perror( "could not set the max message size of the target" );
return EXIT_FAILURE;
}
// this entry will be truncated to 200 bytes, meaning that the message will
// be cut off partways through
log_result = stumpless_add_entry( udp_target, basic_entry );
if( log_result < 0 ) {
stumpless_perror( "could not log an entry" );
return EXIT_FAILURE;
}
// closing the target is done as usual
// destroying all the other resources before finishing up:
return EXIT_SUCCESS;
}
STUMPLESS_PUBLIC_FUNCTION struct stumpless_element * stumpless_add_param(struct stumpless_element *element, struct stumpless_param *param)
Adds a param to an element.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_element * stumpless_new_element(const char *name)
Creates a new element with the given name.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_add_element(struct stumpless_entry *entry, struct stumpless_element *element)
Adds an element to an entry.
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_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 void stumpless_perror(const char *prefix)
Prints information about the current error to the error stream.
STUMPLESS_PUBLIC_FUNCTION void stumpless_free_all(void)
Closes the default target if it has been opened, frees all memory allocated internally,...
STUMPLESS_PUBLIC_FUNCTION void stumpless_close_network_target(const struct stumpless_target *target)
Closes a network target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_udp4_target(const char *name, const char *destination)
Opens a network target for remote logging over IPv4 and UDP.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_set_udp_max_message_size(struct stumpless_target *target, size_t max_msg_size)
Sets the maximum message size of a UDP network target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_param * stumpless_new_param(const char *name, const char *value)
Creates a new param with the given name and value.
An element of structured data.
Definition element.h:93
A log entry.
Definition entry.h:60
A parameter within a structured data element.
Definition param.h:93
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.