next up previous
Next: CDEV Interface On Unix Up: Logging Client API Previous: Java interface

Some Simple Examples

The following is a simple C++ example to log messages to the CMLOG server.

#include <cmlogClient.h>
#include <unistd.h>

#ifndef __vxworks
main (int argc, char** argv)
#else
clientTest (char* name)
#endif
{
    int status;
#ifndef __vxworks
    cmlogClient *client = new cmlogClient (argv[0]);
#else
    cmlogClient *client = cmlogClient::logClient ();
#endif

    if (client->connect () == CMLOG_SUCCESS) {
        cdevData::insertTag (322, "highVIn");
        cdevData::insertTag (323, "highVOut");
    
        client->setThrottle ("highVIn", 100, 25, 1.0);

        cdevData data;
        for (int i = 0; i < 100; i++) {
             sprintf (temp, "Test program error happend %d\n", i);
             status = client->postError (32, 12, CMLOG_ERROR, 
                                        "EPICS", 
                                        highVIn = %d highVOut = %d text = %s",
                                        i, i*100, temp);
    }
    client->disconnect ();
  }
  else 
    printf ("Cannot connect to daemon\n");
}

The following is the sample makefile for the above C++ code (clientTest.cc) on a Unix machine (Let us assume this machine is a hpux machine).


CXX = CC
CXXFLAGS = -I$(CMLOG)/include -D_CMLOG_BUILD_CLIENT

OBJS = clientTest.o
LIBS = -L$(CMLOG)/lib/hpux -lcmlog

all: clientTest

clientTest: $(OBJS)
        rm -f $@
        $(CXX) -o $@ $(LIBS)

.cc.o:
        rm -f $@
        $(CXX) $(CXXFLAGS) -c $< -o $@

clean:
        rm -f *.o clientTest core *~

The following is the sample makefile for the above C++ code (clientTest.cc) on a mv162 machine (Let us assume this board is running vxWorks 5.2)


CXX = c++68k
CXXFLAGS = -I$(CMLOG)/include -D_CMLOG_BUILD_CLIENT -O \
        -I$(VX_VW_BASE)/h -fstrength-reduce -fforce-mem \
        -finline-functions -fno-builtin -fno-for-scope -nostdinc \
        -DCPU=MC68040 -DCPU_FAMILY=MC680X0 -ansi -pipe \
        -Dvxworks -msoft-float

OBJS = clientTest.o

MAKELIB = ld68k -r -o

all: clientTest

clientTest: $(OBJS)
        rm -f $@
        $(MAKELIB) -o $@ $(OBJS)

.cc.o:
        rm -f $@
        $(CXX) $(CXXFLAGS) -c $< -o $@

clean:
        rm -f *.o clientTest core *~

The following is a simple c code example using the CMLOG client APIs to log messages.


#include <stdio.h>
#include <string.h>
#include <cmlog.h>

#ifndef __vxworks
main (int argc, char** argv)
#else
client_test (char* name)
#endif
{
  int status;
  cmlog_client_t cl;
  int i = 0;
  char message[256];

#ifndef __vxworks
  cl = cmlog_open (argv[0]);
#else
  cl = cmlog_open (name);
#endif

  if (cl == 0) {
    fprintf (stderr, "cannot open cmlog client\n");
    exit (1);
  }

  for (i = 0; i < 100; i++) {
    sprintf (message, "Test error c interface at %d\n", i);
    status = cmlog_logmsg (cl, 32, 12, CMLOG_ERROR,
                           "EPICS", "value = %d status = %d text = %s",
                           i*10, 0, message);
#ifndef __vxworks
    sleep (1);
#else
    taskDelay (sysClkRateGet ());
#endif
  }

  cmlog_close (cl);
}

Once again we show a example Makefile for the above c test code (client_test.c).


CXX = CC
CC  = cc
CFLAGS = -I$(CMLOG)/include -D_CMLOG_BUILD_CLIENT
OBJS = client_test.o
LIBS = -L$(CMLOG)/lib/hpux -lcmlog

all: client_test

client_test: $(OBJS)
        rm -f $@
        $(CXX) -o $@ $(OBJS) $(LIBS)

.c.o:
        rm -f $@
        $(CC) $(CFLAGS) -c $< -o $@

clean:
        rm -f *~ *.o core client_test

The following is the sample makefile for the above c code (client_test.c) on a mv167 machine (Let us assume this board is running vxWorks 5.3)


CC = cc68k
CFLAGS = -I$(CMLOG)/include -D_CMLOG_BUILD_CLIENT -O \
        -I$(WIND_BASE)/target/h -fstrength-reduce -fforce-mem \
        -finline-functions -fno-builtin -nostdinc \
        -DCPU=MC68040 -DCPU_FAMILY=MC680X0 -ansi -pipe \
        -Dvxworks

OBJS = client_test.o

MAKELIB = ld68k -r -o

all: client_test

client_test: $(OBJS)
        rm -f $@
        $(MAKELIB) -o $@ $(OBJS)

.c.o:
        rm -f $@
        $(CXX) $(CFLAGS) -c $< -o $@

clean:
        rm -f *.o client_test core *~

The following is a simple Java code to log some data into the cmlog system.


import cmlog.*;

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;


public class cmlogClient
{
    public static void main (String[] args)
    {
	if (args.length < 1) {
	    System.err.println ("Usage: cmlogClient protovesion(1 or 2) [debug]");
	    System.exit (1);
	}
	Config.PROTOCOL_MAJOR_VERSION = Integer.valueOf (args[0]).intValue();
	if (args.length >= 2) {
	    String tstring = args[1];
	    if (tstring.compareTo ("debug") == 0)
		Config.debug = true;
	}
	
	Client client = new Client("cmlogClient");

	try {
	    client.connect ();
	}catch (ConnectException ce) {
	    System.err.println (ce);
	}catch (IOException e) {
	    System.err.println (e);
	}

	cdevData data = new cdevData ();
	String   val;

	for (int i = 0; i < 100000; i++) {
	    val = new String ("New error message is generated by java at " + String.valueOf (i));
	    data.remove();
	    data.insert ("status", i%10);
	    data.insert ("severity", i%20);
	    data.insert ("facility", "javatest");
	    data.insert ("text", val);
	    
	    try {
		client.postData (data);
	    }catch (IOException e) {
		System.err.println (e);
		break;
	    }

	    try {
		Thread.sleep (1000);
	    }catch (InterruptedException e) {
		;
	    }
	}
	try {
	    client.disconnect ();
	}catch (IOException e) {
	    ;
	}
    }
}



Jie chen
2001-10-22