Search code examples
cvxworks

directing output to both stdout and shared memory


I am working on logging functionality in a embedded project in Vxworks in C.

Here we have shared memory where we write logs and read by another device.

Here I want all the output directed to both to shared memory and stdout i.e., serial console as well. How can we achieve this with out using any third party libraries in Vxworks and using C language.

Thanks for your time and inputs.


Solution

  • Replace all printfs by myprintf(implementation below).

    The myprintf function acts exactly as printf but does some further processing with the line.

    void myprintf(const char *format, ...)
    {
        char buffer[500];   // lines are restricted to maximum of 500 chars
        va_list args;
        va_start (args, format);
        vsnprintf (buffer, 500, format, args);
        va_end (args);
    
        // here buffer contains the string resulting from the invocation of myprintf
        // now here you can do whatever you want with the content of buffer
    
        puts(buffer);  // write to stdout
    
        ... code that writes the content of buffer to shared memory or whatever
    }