[] [] [] []
Next: Conclusion Up: Writing UBS Modules Previous: UBS Daemons   Contents

UBS Event Modules

Now, a more complicated example of a sample event is given, that will demonstrate basic use of the UBS library functions:
/* sample-event.c - A sample UBS event. */

#include "ubs.h"

int main(int argc, char *argv[]) {
 /* Table for variables specific to this event context. */
 ubs_table sample;

 /* Initialize tables. */
 ubs_table_init(&GLOBAL);
 ubs_table_init(&sample);

 /* Initialize program. */
 ubs_init(argv[0]);

 /* Read ubs.conf for global context. */
 if(read_config(DEF_CONFIG, "global", &GLOBAL)) {
  /* If the configuration file can't be read for whatever reason, print
     out an error and bail. */
  console_error("Could not read global context in configuration file", FAIL);
 }
 /* Read ubs.conf for the "sample" context.  In this case, users can
    define extra variables that will only be read by this program, in
    the form of sample.variable = value.  These variables will be safely
    ignored by the rest of the UBS. */
 if(read_config(DEF_CONFIG, "sample", &sample)) {
  console_error("Could not read sample context in configuration file", FAIL);
 }

 /* This module will only have one recognized context, which will be
    sample.text, and will contain a message that will be printed out to
    the UBS debug log.  If this is not defined, then print out an error
    warning, but otherwise continue program execution. */
 if(ubs_table_exists(&sample, "text")) {
  log_error_msg(LOG_DEBUG, "Message from sample event: '%s'",
    ubs_table_data(&sample, "text"));
 }
 else {
  ubs_error_msg(LOG_ERROR, "'text' variable undefined"");
 }

 return OK;
}
In this example program, a specific configuration variable is read from the ubs.conf file, and printed out to the UBS system logs. In order for a module to define its own context, all it needs to do is call read_config, with the second parameter being the name of the context to parse (which is "sample", in this case). This data is stored in a ubs_table structure, which must be initialized before the call to read_config.
This program also demonstrates how logging is handled by the UBS. Although the UBS provides mechanisms to write to specific logfiles, with custom data fields, for most debugging and error messages a simple call to ubs_error_msg will suffice. This function takes in two arguments, the first being the logging priority (which can be LOG_EMERG, LOG_STATUS, LOG_ERROR, or LOG_DEBUG, for emergency, status, error, and debug. respectively). The second parameter is a format string followed by any arguments which are to be substituted into the string. Currently, the only recognized data types for the format string are '%s' for a string, and '%d' for an integer.


[] [] [] []
Next: Conclusion Up: Writing UBS Modules Previous: UBS Daemons   Contents
2003-10-30