lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi, right, I had a tinker with Lua over the weekend and it is really easy
to redirect the entire output of Lua if you just #define printf and

This can be done in Lua.h, however it requires that stdio.h is included
BEFORE these defines or it causes problems, and FILE isn't defined. And,
stdio.h is removed from all other headers ie. lzio.h

// Lua.h //

#include <stdio.h>

// -- Start of my optional bit to redirect output --
// Fn prototypes to my output
void Output_printf(char* fmt,...);
void Output_fprintf(FILE* stream, char* fmt,...);

#define printf  Output_printf
#define fprintf Output_fprintf
// -- end of my bit --

 ... rest if Lua.h ...

Then you can add two fns handling variable argument to print.

void Output_AddTextToConsole(char* text)
 ... add text to my window ...

void Output_printf(char* fmt, ...)
  ... print text into buffer ...

void Output_fprintf(FILE* stream, char* fmt, ...)
  ... print text into buffer ...
  if (stream==stdout || stream==stderr)
    Output_AddTextToConsole(buffer); // I want to redirect stdout and
stderr streams to my console
    fprintf(stream, buffer);

This is all ANSI compliant and gives users who can't redirect the standard
output streams easily facility to do it.

Any thoughts,