Merge branch 'upstream'
[routino] / src / logging.c
1 /***************************************
2  $Header: /home/amb/routino/src/RCS/logging.c,v 1.1 2010/11/13 14:22:40 amb Exp $
3
4  Functions to handle logging functions.
5
6  Part of the Routino routing software.
7  ******************/ /******************
8  This file Copyright 2008-2010 Andrew M. Bishop
9
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU Affero General Public License for more details.
19
20  You should have received a copy of the GNU Affero General Public License
21  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  ***************************************/
23
24
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #include "logging.h"
29
30
31 /*+ The option to print the output in a way that allows logging to a file. +*/
32 int option_loggable=0;
33
34
35 /* Local functions */
36
37 static void vfprintf_first(FILE *file,const char *format,va_list ap);
38 static void vfprintf_middle(FILE *file,const char *format,va_list ap);
39 static void vfprintf_last(FILE *file,const char *format,va_list ap);
40
41 /* Local variables */
42
43 static int printed_length=0;
44
45
46 /*++++++++++++++++++++++++++++++++++++++
47   Print the first message in an overwriting sequence.
48
49   const char *format The format string.
50
51   ... The other arguments.
52   ++++++++++++++++++++++++++++++++++++++*/
53
54 void printf_first(const char *format, ...)
55 {
56  va_list ap;
57
58  if(option_loggable)
59     return;
60
61  va_start(ap,format);
62
63  vfprintf_first(stdout,format,ap);
64
65  va_end(ap);
66 }
67
68
69 /*++++++++++++++++++++++++++++++++++++++
70   Print the middle message in an overwriting sequence.
71
72   const char *format The format string.
73
74   ... The other arguments.
75   ++++++++++++++++++++++++++++++++++++++*/
76
77 void printf_middle(const char *format, ...)
78 {
79  va_list ap;
80
81  if(option_loggable)
82     return;
83
84  va_start(ap,format);
85
86  vfprintf_middle(stdout,format,ap);
87
88  va_end(ap);
89 }
90
91
92 /*++++++++++++++++++++++++++++++++++++++
93   Print the last message in an overwriting sequence.
94
95   const char *format The format string.
96
97   ... The other arguments.
98   ++++++++++++++++++++++++++++++++++++++*/
99
100 void printf_last(const char *format, ...)
101 {
102  va_list ap;
103
104  va_start(ap,format);
105
106  vfprintf_last(stdout,format,ap);
107
108  va_end(ap);
109 }
110
111
112 /*++++++++++++++++++++++++++++++++++++++
113   Print the first message in an overwriting sequence.
114
115   FILE *file The file to write to.
116
117   const char *format The format string.
118
119   ... The other arguments.
120   ++++++++++++++++++++++++++++++++++++++*/
121
122 void fprintf_first(FILE *file,const char *format, ...)
123 {
124  va_list ap;
125
126  if(option_loggable)
127     return;
128
129  va_start(ap,format);
130
131  vfprintf_first(file,format,ap);
132
133  va_end(ap);
134 }
135
136
137 /*++++++++++++++++++++++++++++++++++++++
138   Print the middle message in an overwriting sequence.
139
140   FILE *file The file to write to.
141
142   const char *format The format string.
143
144   ... The other arguments.
145   ++++++++++++++++++++++++++++++++++++++*/
146
147 void fprintf_middle(FILE *file,const char *format, ...)
148 {
149  va_list ap;
150
151  if(option_loggable)
152     return;
153
154  va_start(ap,format);
155
156  vfprintf_middle(file,format,ap);
157
158  va_end(ap);
159 }
160
161
162 /*++++++++++++++++++++++++++++++++++++++
163   Print the last message in an overwriting sequence.
164
165   FILE *file The file to write to.
166
167   const char *format The format string.
168
169   ... The other arguments.
170   ++++++++++++++++++++++++++++++++++++++*/
171
172 void fprintf_last(FILE *file,const char *format, ...)
173 {
174  va_list ap;
175
176  va_start(ap,format);
177
178  vfprintf_last(file,format,ap);
179
180  va_end(ap);
181 }
182
183
184 /*++++++++++++++++++++++++++++++++++++++
185   Print the first message in an overwriting sequence.
186
187   FILE *file The file to write to.
188
189   const char *format The format string.
190
191   va_list ap The other arguments.
192   ++++++++++++++++++++++++++++++++++++++*/
193
194 static void vfprintf_first(FILE *file,const char *format,va_list ap)
195 {
196  int retval;
197
198  retval=vfprintf(file,format,ap);
199  fflush(file);
200
201  if(retval>0)
202     printed_length=retval;
203 }
204
205
206 /*++++++++++++++++++++++++++++++++++++++
207   Print the middle message in an overwriting sequence.
208
209   FILE *file The file to write to.
210
211   const char *format The format string.
212
213   va_list ap The other arguments.
214   ++++++++++++++++++++++++++++++++++++++*/
215
216 static void vfprintf_middle(FILE *file,const char *format,va_list ap)
217 {
218  int retval;
219
220  putchar('\r');
221  retval=vfprintf(file,format,ap);
222  fflush(file);
223
224  if(retval>0)
225     printed_length=retval;
226 }
227
228
229 /*++++++++++++++++++++++++++++++++++++++
230   Print the last message in an overwriting sequence.
231
232   FILE *file The file to write to.
233
234   const char *format The format string.
235
236   va_list ap The other arguments.
237   ++++++++++++++++++++++++++++++++++++++*/
238
239 static void vfprintf_last(FILE *file,const char *format,va_list ap)
240 {
241  int retval;
242
243  if(!option_loggable)
244     putchar('\r');
245  retval=vfprintf(file,format,ap);
246
247  if(retval>0)
248     while(retval++<printed_length)
249        putchar(' ');
250
251  putchar('\n');
252  fflush(file);
253 }