Blame libdjvu/debug.cpp

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma implementation
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#include "debug.h"
Packit df99a1
Packit df99a1
#if ( DEBUGLVL > 0 )
Packit df99a1
Packit df99a1
#include "GThreads.h"
Packit df99a1
#include "GContainer.h"
Packit df99a1
#include "GString.h"
Packit df99a1
#include "GString.h"
Packit df99a1
#include "ByteStream.h"
Packit df99a1
#include "GURL.h"
Packit df99a1
Packit df99a1
#include <stdarg.h>
Packit df99a1
#include <stdio.h>
Packit df99a1
#include <errno.h>
Packit df99a1
Packit df99a1
#ifdef _WIN32
Packit df99a1
# include <windows.h>  // OutputDebugString
Packit df99a1
#endif 
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
#ifndef UNIX
Packit df99a1
#ifndef _WIN32
Packit df99a1
#ifndef macintosh
Packit df99a1
#define UNIX
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
static GCriticalSection debug_lock;
Packit df99a1
#ifdef RUNTIME_DEBUG_ONLY
Packit df99a1
static int              debug_level = 0;
Packit df99a1
#else
Packit df99a1
static int              debug_level = DEBUGLVL;
Packit df99a1
#endif
Packit df99a1
static int              debug_id;
Packit df99a1
static FILE            *debug_file;
Packit df99a1
static int              debug_file_count;
Packit df99a1
Packit df99a1
static GMap<long, DjVuDebug> &
Packit df99a1
debug_map(void)
Packit df99a1
{
Packit df99a1
  static GMap<long, DjVuDebug> xmap;
Packit df99a1
  return xmap;
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebug::DjVuDebug()
Packit df99a1
  : block(0), indent(0)
Packit df99a1
{
Packit df99a1
  id = debug_id++;
Packit df99a1
#ifdef UNIX
Packit df99a1
  if (debug_file_count++ == 0 && !debug_file)
Packit df99a1
    set_debug_file(stderr);
Packit df99a1
#endif
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebug::~DjVuDebug()
Packit df99a1
{
Packit df99a1
#ifdef UNIX
Packit df99a1
  if (--debug_file_count == 0)
Packit df99a1
    {
Packit df99a1
      if (debug_file && (debug_file != stderr))
Packit df99a1
        fclose(debug_file);
Packit df99a1
      debug_file = 0;
Packit df99a1
    }
Packit df99a1
#endif
Packit df99a1
}
Packit df99a1
Packit df99a1
void   
Packit df99a1
DjVuDebug::format(const char *fmt, ... )
Packit df99a1
{
Packit df99a1
  if (! block)
Packit df99a1
    {
Packit df99a1
      va_list ap;
Packit df99a1
      va_start(ap, fmt);
Packit df99a1
      GUTF8String buffer(fmt,ap);
Packit df99a1
      va_end(ap);
Packit df99a1
      GCriticalSectionLock glock(&debug_lock);
Packit df99a1
      if (debug_file)
Packit df99a1
        {
Packit df99a1
          fprintf(debug_file,"%s", (const char*)buffer);
Packit df99a1
          fflush(debug_file);
Packit df99a1
        }
Packit df99a1
#ifdef _WIN32
Packit df99a1
      else
Packit df99a1
        {
Packit df99a1
          OutputDebugStringA((const char *)buffer);
Packit df99a1
        }
Packit df99a1
#endif
Packit df99a1
    }
Packit df99a1
}
Packit df99a1
Packit df99a1
void   
Packit df99a1
DjVuDebug::set_debug_level(int lvl)
Packit df99a1
{
Packit df99a1
  debug_level = lvl;
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
DjVuDebug::set_debug_file(FILE * file)
Packit df99a1
{
Packit df99a1
  GCriticalSectionLock glock(&debug_lock);
Packit df99a1
  if (debug_file && (debug_file != stderr))
Packit df99a1
    fclose(debug_file);
Packit df99a1
  debug_file = file;
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
DjVuDebug::modify_indent(int rindent)
Packit df99a1
{
Packit df99a1
  indent += rindent;
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebug& 
Packit df99a1
DjVuDebug::lock(int lvl, int noindent)
Packit df99a1
{
Packit df99a1
  int threads_num=1;
Packit df99a1
  debug_lock.lock();
Packit df99a1
  // Get per-thread debug object
Packit df99a1
  long threadid = (long) GThread::current();
Packit df99a1
  DjVuDebug &dbg = debug_map()[threadid];
Packit df99a1
  threads_num=debug_map().size();
Packit df99a1
  // Check level
Packit df99a1
  dbg.block = (lvl > debug_level);
Packit df99a1
  // Output thread id and indentation
Packit df99a1
  if (! noindent)
Packit df99a1
    {
Packit df99a1
      if (threads_num>1)
Packit df99a1
        dbg.format("[T%d] ", dbg.id);
Packit df99a1
      int ind = dbg.indent;
Packit df99a1
      char buffer[257];
Packit df99a1
      memset(buffer,' ', sizeof(buffer)-1);
Packit df99a1
      buffer[sizeof(buffer)-1] = 0;
Packit df99a1
      while (ind > (int)sizeof(buffer)-1)
Packit df99a1
        {
Packit df99a1
          dbg.format("%s", buffer);
Packit df99a1
          ind -= sizeof(buffer)-1;
Packit df99a1
        }
Packit df99a1
      if (ind > 0)
Packit df99a1
        {
Packit df99a1
          buffer[ind] = 0;
Packit df99a1
          dbg.format("%s", buffer);
Packit df99a1
        }
Packit df99a1
    }
Packit df99a1
  // Return
Packit df99a1
  return dbg;
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
DjVuDebug::unlock()
Packit df99a1
{
Packit df99a1
  debug_lock.unlock();
Packit df99a1
}
Packit df99a1
Packit df99a1
#define OP(type, fmt) \
Packit df99a1
DjVuDebug& DjVuDebug::operator<<(type arg)\
Packit df99a1
{ format(fmt, arg); return *this; }
Packit df99a1
Packit df99a1
DjVuDebug& DjVuDebug::operator<<(bool arg)
Packit df99a1
{
Packit df99a1
   format("%s", arg ? "TRUE" : "FALSE"); return *this;
Packit df99a1
}
Packit df99a1
Packit df99a1
OP(char, "%c")
Packit df99a1
OP(unsigned char, "%c")
Packit df99a1
OP(int, "%d")
Packit df99a1
OP(unsigned int, "%u")
Packit df99a1
OP(short int, "%hd")
Packit df99a1
OP(unsigned short int, "%hu")
Packit df99a1
OP(long, "%ld")
Packit df99a1
OP(unsigned long, "%lu")
Packit df99a1
OP(float, "%g")
Packit df99a1
OP(double, "%g")
Packit df99a1
OP(const void * const, "0x%08x")
Packit df99a1
Packit df99a1
DjVuDebug& DjVuDebug::operator<<(const char * const ptr) 
Packit df99a1
{
Packit df99a1
  GUTF8String buffer(ptr?ptr:"(null)");
Packit df99a1
  if(buffer.length() > 255)
Packit df99a1
  {
Packit df99a1
    buffer=buffer.substr(0,252)+"...";
Packit df99a1
  }
Packit df99a1
  format("%s", (const char *)buffer);
Packit df99a1
  return *this; 
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebug& DjVuDebug::operator<<(const unsigned char * const ptr) 
Packit df99a1
{ 
Packit df99a1
  return operator<<( (const char *) ptr );
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebug& DjVuDebug::operator<<(const GUTF8String &ptr)
Packit df99a1
{
Packit df99a1
  GUTF8String buffer(ptr);
Packit df99a1
  if(buffer.length() > 255)
Packit df99a1
    buffer=buffer.substr(0,252)+"...";
Packit df99a1
  format("%s", (const char *)buffer);
Packit df99a1
  return *this; 
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebugIndent::DjVuDebugIndent(int inc)
Packit df99a1
  : inc(inc)
Packit df99a1
{
Packit df99a1
  DjVuDebug &dbg = DjVuDebug::lock(0,1);
Packit df99a1
  dbg.modify_indent(inc);
Packit df99a1
  dbg.unlock();
Packit df99a1
}
Packit df99a1
Packit df99a1
DjVuDebugIndent::~DjVuDebugIndent()
Packit df99a1
{
Packit df99a1
  DjVuDebug &dbg = DjVuDebug::lock(0,1);
Packit df99a1
  dbg.modify_indent(-inc);
Packit df99a1
  dbg.unlock();
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
#endif