Branch data Line data Source code
1 : : /*
2 : : * logging.c - logging facility class implementation
3 : : *
4 : : * Copyright (C) 2003, 2004, 2005 Stefan Jahn <stefan@lkcc.org>
5 : : *
6 : : * This is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 2, or (at your option)
9 : : * any later version.
10 : : *
11 : : * This software is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this package; see the file COPYING. If not, write to
18 : : * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 : : * Boston, MA 02110-1301, USA.
20 : : *
21 : : * $Id$
22 : : *
23 : : */
24 : :
25 : : #if HAVE_CONFIG_H
26 : : # include <config.h>
27 : : #endif
28 : :
29 : : #include <stdio.h>
30 : : #include <stdlib.h>
31 : : #include <stdarg.h>
32 : :
33 : : #include "logging.h"
34 : :
35 : : /* This function prints the given messages format and the appropriate
36 : : arguments to a FILE stream depending on the given log level. */
37 : 176189 : void logprint (int level, const char * format, ...) {
38 : : FILE * f;
39 : : va_list args;
40 : :
41 [ + + ]: 176189 : f = level == LOG_STATUS ? file_status : file_error;
42 [ + - ]: 176189 : if (f != NULL) {
43 : 176189 : va_start (args, format);
44 : 176189 : vfprintf (f, format, args);
45 : 176189 : va_end (args);
46 : 176189 : fflush (f);
47 : : }
48 : 176189 : }
49 : :
50 : : /* Initialization of the logging interface. */
51 : 107 : void loginit (void) {
52 : 107 : file_error = file_status = stderr;
53 : 107 : }
54 : :
55 : : /* Both of the log level dependent FILE streams. */
56 : : FILE * file_status = NULL;
57 : : FILE * file_error = NULL;
58 : :
59 : : /* Last number of '*' in the progress bar. */
60 : : int progressbar_last = 0;
61 : :
62 : : /* Print a tiny progress-bar depending on the arguments. */
63 : 34696 : void logprogressbar (nr_double_t current, nr_double_t final, int points) {
64 : : int i;
65 [ - + ]: 34696 : if (progressbar_enable) {
66 [ # # ][ # # ]: 0 : if (((int) (current * 100 / final)) == progressbar_last && current)
67 : 34696 : return;
68 : 0 : progressbar_last = (int) (current * 100 / final);
69 [ # # ]: 0 : if (progressbar_gui) {
70 : 0 : logprint (LOG_STATUS, "\t%02d\r", progressbar_last);
71 : : }
72 : : else {
73 : 0 : logprint (LOG_STATUS, "[");
74 [ # # ]: 0 : for (i = 0; i < (current * points / final); i++)
75 : 0 : logprint (LOG_STATUS, "*");
76 [ # # ]: 0 : for (; i < points; i++) logprint (LOG_STATUS, " ");
77 : 0 : logprint (LOG_STATUS, "] %.2f%% \r",
78 : : (double) (current * 100.0 / final));
79 : : }
80 : : }
81 : : }
82 : :
83 : : /* If non-zero then progress bars are painted... */
84 : : int progressbar_enable = 0;
85 : :
86 : : /* If non-zero then progress bars are painted for the GUI. */
87 : : int progressbar_gui = 0;
88 : :
89 : : /* Clears up the progress bar if requested. */
90 : 105 : void logprogressclear (int points) {
91 : : int i;
92 : 105 : progressbar_last = 0;
93 [ - + ][ # # ]: 105 : if (progressbar_enable && !progressbar_gui) {
94 [ # # ]: 0 : for (i = 0; i < points + 15; i++) logprint (LOG_STATUS, " ");
95 : 0 : logprint (LOG_STATUS, "\r");
96 : : }
97 : 105 : }
|