KLOG.DLL C++ class sample
Previous  Top  Next

The following code is a sample C++ class which applies Kiwi Logger functions. It is compiled using the KLOG.LIB import library.

It provides the following methods:
·A constructor which sets the syslog host, port, transport type, and process header,  
·A method to set the priority using facility names,  
·A method to set the message to send,  
·A method to send the message, and  
·A method to return any resulting errors.  

Klogobj.h class definition:


/*
  Klogobj.h

  Kiwi Logger v1.3
  Sample C++ Logger class. 
  Links implicitly with KLOG.LIB.
  Accesses KLOG.DLL at load-time.
*/

#include "kloglib.h"

class KLog
{
private:
  int  Kret;             // Returned error code
  char Kerror[100];      // Returned error string

public:

  // Constructor. Initialise storage. Initialise the DLL.
  // Set the syslog host, port, IP protocol, and RFC header process name.
  KLog(char *Sysloghost, char *Service, int Protocol, char *Processname)
  {
    Kret = 0;
    memset(Kerror, 0, sizeof(Kerror));
    KLogInitialise();
    KLogSetHostAddress(Sysloghost);
    KLogSetPortByService(Service);
    KLogUseTCP(Protocol);
    KLogSetProcessName(1, Processname);
  }

  // Deconstructor. Do nothing.
  ~KLog()
  {
  }

  // Set the priority using facility names
  void SetPriority(char *FacilityName, char *LevelName)
  {
    KLogSetMessageFacilityName(FacilityName);
    KLogSetMessageLevelName(LevelName);
  }

  // Set the message
  void SetMessage(char *Msg)
  {
    KLogSetMessage(Msg);
  }

  // Send the message. Return FALSE if an error occurred.
  BOOL SendMessage()
  {
    if (KLogSendMessage(&Kret) == FALSE)
    {
       KLogReturnError(Kerror, sizeof(Kerror));
       return FALSE;
    }
    else
       Kerror[0] = '\0';

    return TRUE;
  }

  // Return the last error code and message
  void ReturnError(int *Rret, char *Rerror, int Rlen)
  {
    *Rret = Kret;
    strncpy(Rerror, Kerror, Rlen);
  }
};


Example message code:

// Klog C++ object usage example

#include <iostream.h>
#include "klogobj.h"

int main(int argc, char* argv[])
{
  char Loghost[] = "sunloghost";
  char Myprocess[] = "oracle_watch";
  char Message[] = "Oracle listener is not active on port 1521.";

  int  Retcode;              // Return code and error
  char Reterr[100];

  // Create a new KLog Logger object.
  // Send message to sunloghost, using UDP (0) on port "syslog".
  KLog MyKLog(Loghost, "syslog", 0, Myprocess);

  MyKLog.SetPriority("daemon", "error");
  MyKLog.SetMessage(Message);

  if (MyKLog.SendMessage() == FALSE)
  {
    MyKLog.ReturnError(&Retcode, Reterr, sizeof(Reterr));
    cout << "Error " << Retcode << " occurred: " << Reterr << endl;
  }

  return 0;
}