/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

#ifndef BP_VIEWPORT_H
#define BP_VIEWPORT_H

#include <poliqarp-config.h>

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

struct vp_stream_data {
   FILE *stream;
   char *prefix;
   bool close_on_exit;
};

struct viewport {
   struct vp_stream_data stream;
   unsigned int *nesting;
   unsigned int local_nesting;
};

extern struct viewport *vp_out;
extern struct viewport *vp_err;

/* initialize viewports */
void vp_glob_init(void);

/* free viewports */
void vp_glob_free(void);

/* create a viewport attached to a stream */
struct viewport *vp_new(FILE *stream);

/* create a viewport attached to file */
struct viewport *vp_new_file(const char *path, const char *mode);

/* redirect to file */
void vp_redirect(struct viewport *this, const char *pathname, const char *mode);

/* flush viewport */
void vp_flush(struct viewport *this);

/* close viewport */
void vp_close(struct viewport *this);

/* nest one level */
void vp_nest(struct viewport *this);

void vp_unnest(struct viewport *this);

/* set prefix */
void vp_prefix(struct viewport *this, const char *prefix);

/* print variants: */
int vp_fprintf(struct viewport *this, const char *format, ...);

int vp_pfprintf(struct viewport *this, const char *prefix, const char *format, 
   ...);

int vp_vfprintf(struct viewport *this, const char *format, va_list ap);

int vp_vpfprintf(struct viewport *this, const char *prefix, const char *format,
   va_list ap);

static inline int vp_printf(const char *format, ...)
{
   int result;
   va_list ap;
   va_start(ap, format);
   result = vp_vfprintf(vp_out, format, ap);
   va_end(ap);
   return result;
}

static inline int vp_eprintf(const char *format, ...)
{
   int result;
   va_list ap;
   va_start(ap, format);
   result = vp_vfprintf(vp_err, format, ap);
   va_end(ap);
   return result;
}

#define VP_NORMAL        "::: "
#define VP_ERROR        "ERR "


#endif /* BP_VIEWPORT_H */
