/*****************************************************************************
    This code will test whether trace levels can be set successfully.
    This code will test whether trace messages can be displayed with printf
    style string.
 *****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"

static psS32 testLogMsg00();
static psS32 testLogMsg01();
static psS32 testLogMsg02();
static psS32 testLogMsg03();
static psS32 testLogMsg04();
static psS32 testLogMsg05();

testDescription tests[] = {
                              {
                                  testLogMsg00, 0, "default log levels, printf-style strings", 0, false
                              },
                              {
                                  testLogMsg01, 1, "default log levels, psVLogMsg()", 0, false
                              },
                              {
                                  testLogMsg02, 2, "psLogSetLevel()", 0, false
                              },
                              {
                                  testLogMsg03, 3, "psLogSetFormat()", 0, false
                              },
                              {
                                  testLogMsg04, 4, "Output Format", 0, false
                              },
                              {
                                  testLogMsg05, 5, "psLogSetDestination()", 0, false
                              },
                              {
                                  NULL
                              }
                          };

psS32 main( psS32 argc, char* argv[] )
{
    psLogSetLevel( PS_LOG_INFO );

    return ( ! runTestSuite( stderr, "psLogMsg", tests, argc, argv ) );
}


static void myLogMsg(const char *name,
                     psS32 level,
                     const char *fmt,
                     ...)
{
    va_list ap;

    // Test whether psLogMsgV() accept a va_list for output variables.
    va_start(ap, fmt);
    psLogMsgV(name, level, fmt, ap);
    va_end(ap);
}

static psS32 testLogMsg00()
{
    psS32 i = 0;

    // Send a log messages for levels 0:9.  Only the first four messages
    // should actually be displayed.
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d %f %s\n", i,
                 (float) i, "beep beep");
    }

    return 0;
}

static psS32 testLogMsg01()
{
    psS32 i = 0;

    // Send a log messages for levels 0:9.  Only the first four messages
    // should actually be displayed.
    for (i=0;i<10;i++) {
        myLogMsg(__func__, i, "Hello World!  My level is %d %f %s\n", i,
                 (float) i, "beep beep");
    }

    return 0;
}

static psS32 testLogMsg02()
{

    psLogSetLevel(9);
    // Send a log messages for levels 0:9.
    for (psS32 i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    psLogSetLevel(5);
    psLogMsg(__func__, 6, "This should not be displayed (level %d)\n", 6);
    psLogSetLevel(4);
    psLogMsg(__func__, 4, "This should  be displayed (level %d)\n", 4);

    return 0;
}

static psS32 testLogMsg03()
{
    psS32 i;

    fprintf(stderr,"------------- psLogSetFormat() -------------\n");
    psLogSetFormat("");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(NULL) -------------\n");
    psLogSetFormat(NULL);
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(T) -------------\n");
    psLogSetFormat("T");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(H) -------------\n");
    psLogSetFormat("H");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(L) -------------\n");
    psLogSetFormat("L");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(N) -------------\n");
    psLogSetFormat("N");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(M) -------------\n");
    psLogSetFormat("M");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    fprintf(stderr,"------------- psLogSetFormat(THLNM) -------------\n");
    psLogSetFormat("THLNM");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    return 0;
}


psS32 testLogMsg04()
{
    psLogMsg("Under 15 chars", 0, "Hello World!\n");
    psLogMsg("This string is more than 15 chars", 0, "Hello World!\n");
    psLogMsg(__func__, 0, "Line #1\n");
    psLogMsg(__func__, 0, "Line #2\n");
    psLogMsg(__func__, 0, "Line #3");
    psLogMsg(__func__, 0, "Line #4");

    return 0;
}

psS32 testLogMsg05()
{
    psS32 i = 0;
    FILE* file;
    char line[256];

    printf("--------------- psLogSetDestination(PS_LOG_NONE) ----------------\n");
    psLogSetDestination("none");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    printf("------------- psLogSetDestination(PS_LOG_TO_STDERR) -------------\n");
    psLogSetDestination("dest:stderr");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    printf("------------- psLogSetDestination(PS_LOG_TO_STDOUT) -------------\n");
    psLogSetDestination("dest:stdout");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }
    printf("--------------- psLogSetDestination(""file:log.txt"") ---------------\n");
    psLogSetDestination("file:log.txt");
    for (i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World!  My level is %d\n", i);
    }

    psLogSetDestination("none");
    printf("--------------------- The Contents of log.txt -------------------\n");
    file = fopen("log.txt","r");
    while ( fgets(line,256,file) != NULL ) {
        printf("%s",line);
    }
    fclose(file);

    printf("--------------- psLogSetDestination(""file:/eva/log.txt"") ----------\n");
    psLogSetDestination("file:/eva/log.txt");
    for ( i=0;i<10;i++) {
        psLogMsg(__func__, i, "Hello World! My level is %d\n", i);
    }

    return 0;
}
