Qucs-core  0.0.19
logging.c
Go to the documentation of this file.
00001 /*
00002  * logging.c - logging facility class implementation
00003  *
00004  * Copyright (C) 2003, 2004, 2005 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  * 
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.  
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #if HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <stdarg.h>
00032 
00033 #include "logging.h"
00034 
00035 /* This function prints the given messages format and the appropriate
00036    arguments to a FILE stream depending on the given log level. */
00037 void logprint (int level, const char * format, ...) {
00038   FILE * f;
00039   va_list args;
00040 
00041   f = level == LOG_STATUS ? file_status : file_error;
00042   if (f != NULL) {
00043     va_start (args, format);
00044     vfprintf (f, format, args);
00045     va_end (args);
00046     fflush (f);
00047   }
00048 }
00049 
00050 /* Initialization of the logging interface. */
00051 void loginit (void) {
00052   file_error = file_status = stderr;
00053 }
00054 
00055 /* Both of the log level dependent FILE streams. */
00056 FILE * file_status = NULL;
00057 FILE * file_error = NULL;
00058 
00059 /* Last number of '*' in the progress bar. */
00060 int progressbar_last = 0;
00061 
00062 /* Print a tiny progress-bar depending on the arguments. */
00063 void logprogressbar (nr_double_t current, nr_double_t final, int points) {
00064   int i;
00065   if (progressbar_enable) {
00066     if (((int) (current * 100 / final)) == progressbar_last && current)
00067       return;
00068     progressbar_last = (int) (current * 100 / final);
00069     if (progressbar_gui) {
00070       logprint (LOG_STATUS, "\t%02d\r", progressbar_last);
00071     }
00072     else {
00073       logprint (LOG_STATUS, "[");
00074       for (i = 0; i < (current  * points / final); i++)
00075         logprint (LOG_STATUS, "*");
00076       for (; i < points; i++) logprint (LOG_STATUS, " ");
00077       logprint (LOG_STATUS, "] %.2f%%      \r",
00078                 (double) (current * 100.0 / final));
00079     }
00080   }
00081 }
00082 
00083 /* If non-zero then progress bars are painted... */
00084 int progressbar_enable = 0;
00085 
00086 /* If non-zero then progress bars are painted for the GUI. */
00087 int progressbar_gui = 0;
00088 
00089 /* Clears up the progress bar if requested. */
00090 void logprogressclear (int points) {
00091   int i;
00092   progressbar_last = 0;
00093   if (progressbar_enable && !progressbar_gui) {
00094     for (i = 0; i < points + 15; i++) logprint (LOG_STATUS, " ");
00095     logprint (LOG_STATUS, "\r");
00096   }
00097 }