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

Demonstrates how to work with a Windows Event Log target.

Demonstrates how to work with a Windows Event Log target.

// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2018-2022 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>
#include <windows.h>
#include "docs/examples/wel/example_events.h"
int
main( int argc, char **argv ) {
struct stumpless_entry *basic_entry;
DWORD register_result;
struct stumpless_target *wel_target;
struct stumpless_entry *entry_with_params;
struct stumpless_entry *result;
struct stumpless_param *child_name;
struct stumpless_param *tree_type;
int log_result;
// creating a basic entry
basic_entry = stumpless_new_entry( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_INFO,
"tree-identifier-app",
"tree-identified-by-child",
"a child found a tree!" );
if( !basic_entry ) {
stumpless_perror( "could not create the basic entry" );
return EXIT_FAILURE;
}
// attempting to register the default event source
if( register_result != ERROR_SUCCESS ) {
stumpless_perror( "WARNING - couldn't register the default source" );
// we wont fail our example if this happens, just warn the user
}
// opening an explicit Windows Event Log target to the default source
wel_target = stumpless_open_local_wel_target( "Stumpless" );
if( !wel_target ) {
stumpless_perror( "could not open the default Windows Event Log" );
return EXIT_FAILURE;
}
// sending an entry with default values to the default target
// if the registration function succeeded, then this entry will be visible
// in the Event Viewer under the Stumpless application
log_result = stumpless_add_entry( wel_target, basic_entry );
if( log_result < 0 ) {
stumpless_perror( "could not log a default entry" );
return EXIT_FAILURE;
}
// clean up by closing the target and unregistering the default source
if( register_result != ERROR_SUCCESS ) {
stumpless_perror( "WARNING - couldn't unregister the default source" );
// again, we wont fail in this scenario either
}
// opening a Windows Event Log target in the "KidsAndTrees" log
wel_target = stumpless_open_local_wel_target( "KidsAndTrees" );
if( !wel_target ) {
stumpless_perror( "could not open the local Windows Event Log" );
return EXIT_FAILURE;
}
// setting the three wel-specific fields on the entry
result = stumpless_set_wel_category( basic_entry, CATEGORY_TREE );
if( !result ) {
stumpless_perror( "could not set the basic entry category" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_event_id( basic_entry,
MSG_TREE_IDENTIFIED_BY_CHILD );
if( !result ) {
stumpless_perror( "could not set the basic entry event id" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_type( basic_entry, EVENTLOG_SUCCESS );
if( !result ) {
stumpless_perror( "could not set the basic entry type" );
return EXIT_FAILURE;
}
// logging a basic entry
log_result = stumpless_add_entry( wel_target, basic_entry );
if( log_result < 0 ) {
stumpless_perror( "could not log a basic entry" );
return EXIT_FAILURE;
}
// setting the insertion strings on the entry
result = stumpless_set_wel_insertion_string( basic_entry, 0, "cynthia" );
if( !result ) {
stumpless_perror( "could not set the first insertion string of an entry" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_insertion_string( basic_entry, 1, "oak" );
if( !result ) {
stumpless_perror( "could not set the second insertion string of an entry" );
return EXIT_FAILURE;
}
// the previous two strings could have been set with the following call:
// stumpless_set_wel_insertion_strings( basic_entry, 2, "cynthia", "oak" );
// logging the entry with insertion strings set
// note that this is the same code as before
log_result = stumpless_add_entry( wel_target, basic_entry );
if( log_result < 0 ) {
stumpless_perror( "could not log a basic entry" );
return EXIT_FAILURE;
}
// creating another entry
entry_with_params = stumpless_new_entry( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_INFO,
"tree-identifier-app",
"tree-identified-by-child",
"a child found a tree!" );
if( !entry_with_params ) {
stumpless_perror( "could not create the second entry" );
return EXIT_FAILURE;
}
// setting the three wel-specific fields on the entry
result = stumpless_set_wel_category( entry_with_params, CATEGORY_TREE );
if( !result ) {
stumpless_perror( "could not set the second entry category" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_event_id( entry_with_params,
MSG_TREE_IDENTIFIED_BY_CHILD );
if( !result ) {
stumpless_perror( "could not set the second entry event id" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_type( entry_with_params, EVENTLOG_SUCCESS );
if( !result ) {
stumpless_perror( "could not set the second entry type" );
return EXIT_FAILURE;
}
// setting the insertion params on the entry
child_name = stumpless_new_param( "child-name", "cynthia" );
if( !child_name ) {
stumpless_perror( "could not create the child name param" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_insertion_param( basic_entry, 0, child_name );
if( !result ) {
stumpless_perror( "could not set the first insertion string of an entry" );
return EXIT_FAILURE;
}
tree_type = stumpless_new_param( "tree-type", "oak" );
if( !tree_type ) {
stumpless_perror( "could not create the child name param" );
return EXIT_FAILURE;
}
result = stumpless_set_wel_insertion_param( basic_entry, 1, tree_type );
if( !result ) {
stumpless_perror( "could not set the second insertion string of an entry" );
return EXIT_FAILURE;
}
// logging the entry with insertion params set
// note that this is the same code as before once again
log_result = stumpless_add_entry( wel_target, entry_with_params );
if( log_result < 0 ) {
stumpless_perror( "could not log a basic entry" );
return EXIT_FAILURE;
}
// closing the target
// destroying all remaining resources
stumpless_destroy_param( child_name );
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_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_destroy_param(const struct stumpless_param *param)
Destroys a param, freeing any allocated memory.
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.
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.
STUMPLESS_PUBLIC_FUNCTION void stumpless_close_wel_target(struct stumpless_target *target)
Closes a Windows Event Log target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_target * stumpless_open_local_wel_target(const char *name)
Opens a Windows Event Log target on the local machine.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_set_wel_category(struct stumpless_entry *entry, WORD category)
Sets the category of an entry for use with a Windows Event Log target.
STUMPLESS_PUBLIC_FUNCTION DWORD stumpless_add_default_wel_event_source(void)
Creates the registry entries for displaying default entries in the Windows Event Log.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_set_wel_event_id(struct stumpless_entry *entry, DWORD event_id)
Sets the event id of an entry for use with a Windows Event Log target.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_set_wel_insertion_string(struct stumpless_entry *entry, WORD index, LPCSTR str)
Sets a string to use for string insertion in a Windows Event Log entry.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_set_wel_type(struct stumpless_entry *entry, WORD type)
Sets the type of an entry for use with a Windows Event Log target.
STUMPLESS_PUBLIC_FUNCTION DWORD stumpless_remove_default_wel_event_source(void)
Removes the registry entries for the event source for default WEL entries.
STUMPLESS_PUBLIC_FUNCTION struct stumpless_entry * stumpless_set_wel_insertion_param(struct stumpless_entry *entry, WORD index, const struct stumpless_param *param)
Sets a param to use for string insertion in a Windows Event Log entry.