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

Demonstrates how to work with a journald target.

Demonstrates how to work with a journald target.

// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2021 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( void ) {
struct stumpless_target *my_journal;
struct stumpless_entry *entry;
// creating a target to send messages to the local journald service
my_journal = stumpless_open_journald_target( "daily-journal" );
// this is optional, but makes our messages easier to find later
stumpless_set_target_default_app_name( my_journal, "daily-journal" );
stumpless_add_message( my_journal, "rode my bike with no handlebars" );
stumpless_add_message( my_journal, "curled up with a book to read" );
stumpless_add_message( my_journal, "designed an engine with 64 mpg" );
// creating an entry with structured data
entry = stumpless_new_entry( STUMPLESS_FACILITY_DAEMON,
STUMPLESS_SEVERITY_INFO,
"daily-journal",
"detailed-status",
"alive and on top" );
stumpless_add_new_param_to_entry( entry, "my", "reach", "global" );
stumpless_add_new_param_to_entry( entry, "my", "tower", "secure" );
stumpless_add_new_param_to_entry( entry, "my", "cause", "noble" );
stumpless_add_new_param_to_entry( entry, "my", "power", "pure" );
// send our custom entry to the local journald service
stumpless_add_entry( my_journal, entry );
// some final cleanup before we finish
return EXIT_SUCCESS;
}
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_add_new_param_to_entry(struct stumpless_entry *entry, const char *element_name, const char *param_name, const char *param_value)
Creates a new param and adds it to the given element in the given entry.
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_target * stumpless_open_journald_target(const char *name)
Opens a journald target.
STUMPLESS_PUBLIC_FUNCTION void stumpless_close_journald_target(const struct stumpless_target *target)
Closes a journald target.
STUMPLESS_PUBLIC_FUNCTION void stumpless_free_all(void)
Closes the default target if it has been opened, frees all memory allocated internally,...
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_default_app_name(struct stumpless_target *target, const char *app_name)
Sets the default app name for a given target.
STUMPLESS_PUBLIC_FUNCTION int stumpless_add_message(struct stumpless_target *target, const char *message,...)
Adds a message to 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.