

#include <windows.h>
#include <stdio.h>



/* Wrtte formatted data to the debugging console */
void console(char *fmt, ... )
{
    static HANDLE h_console = INVALID_HANDLE_VALUE;

    if (h_console == INVALID_HANDLE_VALUE)
    {
        AllocConsole();
        h_console =  GetStdHandle(STD_OUTPUT_HANDLE);
    }
   
    const unsigned int max = 1024;

    va_list args;
    char text[max];

    va_start(args, fmt);
    vsnprintf(text, max, fmt, args);
    va_end(args);    
    
    DWORD out = 0;
   
    WriteConsole(h_console, text, strlen(text), &out, NULL);
}
/* End of write_console */


/* Wrtte formatted data to the debugging console */
void log(char *fmt, ... )
{
    FILE *f;
    
    static bool started = false;
    
    if (started)
    {
        f = fopen("log.txt", "at");    
    }else
    {
        f = fopen("log.txt", "wt");            
        started = true;
    }

    if (!f) 
    {
        console ("Unable to load log file\n");
        return;
    }    
    
        
    const unsigned int max = 1024;

    va_list args;
    char text[max];

    va_start(args, fmt);
    vsnprintf(text, max, fmt, args);
    va_end(args);    
    

    fprintf(f, "%s", text);
    console("%s", text);
    
    
    fclose(f);
}
/* End of write_console */


