/*
* serverconsole.cpp
*
* Copyright (C) 2001 Atomic Blue (info@planeshift.it, http://www.atomicblue.org) 
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation (version 2 of the License)
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
* Author: Matthias Braun <MatzeBraun@gmx.de>
*/

#include <psconfig.h>

#include <csutil/snprintf.h>

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <memory.h>

#ifdef CS_COMPILER_MSVC
#include <conio.h>
#endif

#ifdef HAVE_READLINE_READLINE_H
#   include <readline/readline.h>
#   ifdef HAVE_READLINE_HISTORY_H
#    include <readline/history.h>
#    define USE_HISTORY
#   endif
#   define USE_READLINE
#endif

#ifdef HAVE_READLINE_H
/* g++ with glibc hack... */
#   ifdef __P
#    undef __P
#   endif
#   include <readline.h>
#   ifdef HAVE_HISTORY_H
#    include <history.h>
#    define USE_HISTORY
#   endif
#   define USE_READLINE
#endif

#ifndef whitespace
#   define whitespace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\r') )
#endif

#include "consoleout.h"
#include "command.h"
#include "util/pserror.h"

static FILE* output = NULL;
static ConsoleOutMsgClass maxoutput_stdout = CON_SPAM;
static ConsoleOutMsgClass maxoutput_file = CON_SPAM;

int ConsoleOut::shift = 0;
csString *ConsoleOut::strBuffer = NULL;


void ConsoleOut::SetMaximumOutputClassStdout (ConsoleOutMsgClass con)
{
    maxoutput_stdout = con;
}

void ConsoleOut::SetMaximumOutputClassFile (ConsoleOutMsgClass con)
{
    maxoutput_file = con;
}

ConsoleOutMsgClass ConsoleOut::GetMaximumOutputClassStdout()
{
    return maxoutput_stdout;
}

void ConsoleOut::SetOutputFile (const char* filename, bool append)
{
    if (output)
    {
        fclose (output);
        output = NULL;
    }
    if (filename)
    {
        output = fopen (filename, append ? "a" : "w");
    }
}

void ConsoleOut::Intern_Printf (ConsoleOutMsgClass con, const char* string, ...)
{
    va_list args;
    va_start (args, string);

    if (con <= maxoutput_stdout)
    {
        for (int i=0; i < shift; i++)
        {
            if (!strBuffer)
                printf("  ");
            else
                strBuffer->Append("  ");
        }
        if (!strBuffer)
        {
            vprintf(string, args);
            fflush(stdout);
        }
        else
        {
            char buffer[5000];
            vsnprintf(buffer, 5000, string, args);
            strBuffer->Append(buffer);
        }
    }
    if (output && con <= maxoutput_file)
    {
        vfprintf (output, string, args);
        //fflush (output);
    }

    va_end(args);

#ifdef USE_READLINE
    rl_redisplay();
#endif
}

void ConsoleOut::Intern_Printf_LogOnly(ConsoleOutMsgClass con,
                                       const char *string, va_list args)
{
    if (output && con <= maxoutput_file)
    {
        for (int i=0; i < shift; i++)
            vfprintf (output, "  ", args);
        vfprintf (output, string, args);
        fflush (output);
    }
}

void ConsoleOut::Shift()
{
    shift ++;
}

void ConsoleOut::Unshift()
{
    shift --;
}
