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.
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:
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);
In case of the object-oriented languages, you need to derive a class and implement the flush() method.
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; } }
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;
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; } }
class MyBuffer extends Flx.SimpleBuffer { constructor(size) { super(size); } /** * * @return {number} */ flush() { let data = this.data(); let start = this.startPos(); let end = this.endPos(); console.info("flushing " + /* valueOf */ new String(start - end).toString() + " bytes"); this.clear(); return Flx.OK; } }
To collect and reset the buffer data, ther are 4 methods: