 |
» |
|
|
 |
In Example 4-2 “Using Variable-Length
Argument Lists”, variable
items were added to an event by including them in a varargs list, using EvmItemSetVa(). You can add or change variable values by using any
of the varargs functions that take item identifiers as arguments. Example 4-3 “Adding and Retrieving
Variables” illustrates
another way to add variable data values to an existing event and shows
how to retrieve the values of the variables. The EVM library includes
primitive get and set functions that use a data structure to describe
a variable, and a set of convenience functions that simplify programming
by encapsulating the handling of the structure. This example illustrates
the use of both types of function. Example 4-3 “Adding and Retrieving
Variables” introduces
the following functions:
EvmVarSet — Sets
the value of a named variable data item in an event. This function
is used both for adding a variable and for altering the value of
an existing variable. For more information about this function,
see EvmVarSet(3). EvmVarGet — Returns information
of the specified event variable in a given variable structure. The
calling function must free any memory used by the variable by calling EvmVarRelease(). For
more information about this function, see EvmVarGet(3). EvmVarRelease — Releases memory
that was allocated when the specified variable was retrieved from
an event by EvmVarGet(). However, it releases
only heap storage referenced by the structure. It does not release
the specified variable structure. For more information about this
function, see EvmVarRelease(3).
Example 4-3 Adding and Retrieving
Variables #include <stdio.h>#include <evm/evm.h>int main(){ EvmEvent_t event; EvmStatus_t status; /* When using a primitive function to add a variable to the event, you must first place the value in a union of type EvmVarValue_t. The members of the union have the same names as the EVM variable types.*/ EvmVarValue_t varval_1, varval_2; EvmVarStruct_t varinfo; EvmString_t progname; EvmUint16_t exit_code; EvmItemValue_t itemval_name; itemval_name.NAME="sys.unix.evm"; EvmEventCreateVa(&event, EvmITEM_NAME,itemval_name, EvmITEM_NONE); |
 |
 |
/* Set and retrieve some values using basic set/get functions: */ varval_1.STRING = "my_app"; varval_2.UINT16 = 17; /* Use EvmVarSet() to add the variables to the event, giving them meaningful names. The final two arguments are set to 0 unless you are adding an opaque variable or supplying an I18N message ID for a string variable. */ EvmVarSet(event,"progname",EvmTYPE_STRING,varval_1,0,0);EvmVarSet(event,"exit_code",EvmTYPE_UINT16,varval_2,0,0); /* You can retrieve the value of any variable by passing its name to EvmVarGet() function, which copies the value into an EvmVarStruct_t structure. The structure also contains the name, type, and size of the variable, so you can write generic code to handle any type of variable. Retrieving a variable does not remove the variable from the event. You can retrieve it as many times as you want. The returned information always take up space in heap memory, so you must use EvmVarRelease() to clean up after using the value.*/ status = EvmVarGet(event,"progname",&varinfo); if (status == EvmERROR_NONE) { fprintf(stdout,"Program name:%s\n",varinfo.value.STRING); EvmVarRelease(&varinfo); } status = EvmVarGet(event,"exit_code",&varinfo); if (status == EvmERROR_NONE) { fprintf(stdout,"Exit code: %d\n",varinfo.value.UINT16); EvmVarRelease(&varinfo); } /* Set and retrieve the same values using convenience functions: */ /* The EvmVarSetString() and EvmVarSetUint16() functions, along with other functions in the EvmVarSetXxx() family, are simpler alternatives to the EvmVarSet() function. They require fewer arguments, and there is no need to set up a value structure. If you want to supply an I18N message-id with a string variable, use the EvmVarSetStringI18N() function */ EvmVarSetString(event,"progname","my_app"); EvmVarSetUint16(event,"exit_code",17); /* The EvmVarGetString() and EvmVarGetUint16() functions, along with other functions in the EvmVarGetXxx() family, are simpler alternatives to the EvmVarGet() function. They require fewer arguments, and there is no need to set up a variable information structure or to follow the retrieval operation with a call to EvmVarRelease().*/ status = EvmVarGetString(event,"progname",&progname,NULL); if (status == EvmERROR_NONE) fprintf(stdout,"Program name: %s\n",progname); /* If you use a convenience function to retrieve a string or opaque variable, the function allocates heap space for the value and returns a pointer to the heap. You must use free() to release the heap space after you have finished with the value.*/ free(progname); status = EvmVarGetUint16(event,"exit_code",&exit_code); if (status == EvmERROR_NONE) fprintf(stdout,"Exit code: %d\n",exit_code); /* After you have finished with the event, free the storage space used by the event. */ EvmEventDestroy(event);} |
 |
|