Example: Using iSeries Access for Windows Serviceability APIs

The following example uses the iSeries™ Access for Windows® Serviceability APIs to log a message string to theiSeries Access for Windows History Log.

#include <stdio.h>
#include "CWBSV.H"
 
unsigned int logMessageText(char *msgtxt)
/* Write a message to the active message log. */
{
    cwbSV_MessageTextHandle messageTextHandle;
    unsigned int    rc;
 
    /* Create a handle to a message text object, so that we may write  */
    /* message text to the active message log.                         */
    if ((rc = cwbSV_CreateMessageTextHandle("ProductID", "ComponentID",
            &messageTextHandle)) != CWB_OK)
        return(rc);
 
    /* Log the supplied message text to the active message log.        */
    rc = cwbSV_LogMessageText(messageTextHandle, msgtxt, strlen(msgtxt));
 
    /* Delete the message text object identified by the handle provided.*/
    cwbSV_DeleteMessageTextHandle(messageTextHandle);
 
    return(rc);
}
 
unsigned int readMessageText(char **bufptr, cwbSV_ErrHandle errorHandle)
/* Read a message from the active message log. */
{
    cwbSV_ServiceFileHandle serviceFileHandle;
    cwbSV_ServiceRecHandle  serviceRecHandle;
    static char buffer[BUFSIZ];
    unsigned int    rc;
 
    /* Retrieve the fully-qualified path and file name of the active  */
    /* message log.                                                   */
    if ((rc = cwbSV_GetServiceFileName(CWBSV_HISTORY_LOG, buffer, BUFSIZ,
            NULL)) != CWB_OK)
        return(rc);
 
    /* Open the active message log for READ access and return a handle */
    /*  to it.                                                         */
    if ((rc = cwbSV_OpenServiceFile(buffer, &serviceFileHandle, errorHandle))
            != CWB_OK)
        return(rc);
 
    /* Create a service record object and return a handle to it.     */
    if ((rc = cwbSV_CreateServiceRecHandle(&serviceRecHandle)) != CWB_OK) {
        cwbSV_CloseServiceFile(serviceFileHandle, 0);
        return(rc);
    }
 
    /* Read the newest record in the active message log into the      */
    /* record handle provided.                                        */
    if ((rc = cwbSV_ReadNewestRecord(serviceFileHandle, serviceRecHandle,
            errorHandle)) != CWB_OK) {
        cwbSV_DeleteServiceRecHandle(serviceRecHandle);
        cwbSV_CloseServiceFile(serviceFileHandle, 0);
        return(rc);
    }
 
    /* Retrieve the message text portion of the service record object */
    /* identified by the handle provided.                             */
    if ((rc = cwbSV_GetMessageText(serviceRecHandle, buffer, BUFSIZ, NULL))
            == CWB_OK || rc == CWB_BUFFER_OVERFLOW) {
        *bufptr = buffer;
        rc = CWB_OK;
    }
 
    /* Delete the service record object identified by the             */
    /* handle provided.                                               */
    cwbSV_DeleteServiceRecHandle(serviceRecHandle);
 
    /* Close the active message log identified by the handle provided.*/
    cwbSV_CloseServiceFile(serviceFileHandle, errorHandle);
 
    return(rc);
}
 
void main(int argc, char *argv[ [)
{
    cwbSV_ErrHandle errorHandle;
    char   *msgtxt = NULL, errbuf[BUFSIZ];
    unsigned int    rc;
 
    /* Write a message to the active message log.                     */
    if (logMessageText("Sample message text") != CWB_OK)
        return;
 
    /* Create an error message object and return a handle to it.      */
    cwbSV_CreateErrHandle(&errorHandle);
 
    /* Read a message from the active message log.                    */
    if (readMessageText(&msgtxt, errorHandle) != CWB_OK) {
        if ((rc = cwbSV_GetErrText(errorHandle, errbuf, BUFSIZ, NULL)) ==
                CWB_OK || rc == CWB_BUFFER_OVERFLOW)
            fprintf(stdout, "%s\n", errbuf);
    }
    else if (msgtxt)
        fprintf(stdout, "Message text: \"%s\"\n", msgtxt);
 
    /* Delete the error message object identified by the              */
    /* handle provided.                                               */
    cwbSV_DeleteErrHandle(errorHandle);
}