/*==========================================================================*\
 |  This file is part of setiherder (http://www.sackheads.org/setiherder/)  |
 |  which was written by Brian L. Naylor <brian+seti@sackheads.org> and is  |
 |  under the GNU General Public License.  You should have recieved a copy  |
 |  of the GPL with this source code.  If not, we'd like to know about it.  |
\*--------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "shutil.h"
#include "shwin.h"

char *levelstr(int level) 
{
  switch(level)
  {
    case DBG_CUTE: 
      return "cute";
      break;
    case DBG_INFO:
      return "info";
      break;
    case DBG_WARN:
      return "warn";
      break;
    case DBG_ERROR:
      return "error";
      break;
    case DBG_FATAL:
      return "fatal";
      break;
    case DBG_DIAG:
      return "-diag-";
      break;
    case DBG_DUMP:
      return "<dump>";
      break;
    default:
      return "UNKNOWN";
      break;
  };
  return "UNKNOWN";
}

void dbg(int level, char *msg, ...)
{
  static int        dbgflags = 0x0;
  static char       outbuf[DBG_MAX_OUT+1];
  va_list           arg_ptr=NULL;
  extern int        gtk_initialized;

  if(!dbgflags) {
    extern int setdbgflags;
    dbgflags = setdbgflags;
  }
  
  if(level & dbgflags) {
    va_start(arg_ptr, msg);
#ifdef HAVE_SNPRINTF    
    vsnprintf(outbuf, DBG_MAX_OUT, msg, arg_ptr);  
#else
    vsprintf(outbuf, msg, arg_ptr);
#endif    
    va_end(arg_ptr);
  
    if(gtk_initialized) 
    {
      /* send message to gui */
      add_text_to_log(level, outbuf);
    }
    else 
    {
      printf("DBG(%s): %s", levelstr(level), outbuf);
    }
  }
}
