F003 To be handled...

Buffers may have handlers. In the first examples, we are using the flxWriteToFile handler (c language) and the SimpleFileOutputBuffer class (Object oriented languages). This handler writes the content to a file as soon there is not enough buffer space available or when calling the flush method.

You may define your own handler (e.g. write the trace output to a custom interface). Handlers are also used for compression. The handler of one buffer may compress its entries and sends them into a second buffer.

c-language Handler

In the embedded area, you probably will use a dedicated channel for trace data. To do so, you just need to add a handler function like below:
 

	flxresult myHandler(flxbyte command, void* buffer, flxbint* len,
			flxbyte* bytes, void*user) {
	
		// just write the data to a file
		fwrite(bytes, *len, 1, (FILE*) user);
		return FLX_OK;
	}

The parameters you need are:

  • flxbint* len: The length of data in the buffer.
  • flxbyte* bytes: A pointer to the data.
  • void* user: The user data pointer (optional).

To apply the handler to any buffer, just apply the function pointer as parameter to the flxCreateXXXBuffer function. Optionally you can add a user pointer, in this example the FILE object pointer.
 

	// static memory
	unsigned char memoryBuffer[bufferSize];
	unsigned char memoryTrace[traceSize];

	// buffer
	flxBuffer buffer = flxCreateFixedBuffer(memoryBuffer, bufferSize,
			myHandler, out);

Class Handler

In case of the object-oriented languages, you need to derive a class and implement the flush() method.

  • Java

        static class MyBuffer extends Flx.SimpleBuffer {
            
            public MyBuffer(int size) {
                super(size);
            }
            
            @Override
            public int flush() {
                         
                byte[] data = this.data();
                int start = this.startPos();
                int end = this.endPos();
                
                System.out.println("flushing " + String.valueOf(start-end) + " bytes");  
                // ....
    
                
                this.clear();
                return Flx.OK;
            }
        }
  • Python

        class MyBuffer (Flx.SimpleBuffer) :
            
            def __init__(self,size) :
                Flx.SimpleBuffer.__init__(self,size);
            
            
            
            def flush(self ,) :
                         
                data = self.data();
                start = self.startPos();
                end = self.endPos();
                
                print("flushing " + str(start-end) + " bytes");  
                # ....
        
                
                self.clear();
                return Flx.OK;
    
  • TypeScript

        export class MyBuffer extends Flx.SimpleBuffer {
            public constructor(size : number) {
                super(size);
            }
    
            /**
             * 
             * @return {number}
             */
            public flush() : number {
                let data : number[] = this.data();
                let start : number = this.startPos();
                let end : number = this.endPos();
                console.info("flushing " + /* valueOf */new String(start - end).toString() + " bytes");
                this.clear();
                return Flx.OK;
            }
        }
    
  • JavaScript

        class MyBuffer extends Flx_1.Flx.SimpleBuffer {
            constructor(size) {
                super(size);
            }
            /**
             *
             * @return {number}
             */
            flush() {
                console.info("flushing " + /* valueOf */ new String(this.pos).toString() + " bytes");
                this.pos = 0;
                return Flx_1.Flx.OK;
            }
        }
        

To collect and reset the buffer data, ther are 4 methods:

  • data(): Returns the data buffer.
  • startPos(): Returns the index of the first byte in the data buffer.
  • endPos(): Returns the index of the next byte after the end of data.
  • clear(): Clears the buffer.
toem

technical software and tooling

Company

Contact Us

This email address is being protected from spambots. You need JavaScript enabled to view it.