File: output.c

package info (click to toggle)
afterstep 2.2.12-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 33,168 kB
  • sloc: ansic: 201,695; sh: 5,894; xml: 3,721; makefile: 2,095; perl: 1,558; cpp: 811
file content (267 lines) | stat: -rw-r--r-- 6,654 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/****************************************************************************
 *
 * Copyright (c) 1999 Sasha Vasko <sasha at aftercode.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ****************************************************************************/
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include "astypes.h"
#include "output.h"
#include "ashash.h"
#include "parse.h"

char         *ApplicationName = "unknown";                    /* name are we known by */


static stream_func  as_default_stream_func = (stream_func)fprintf;
static void        *as_default_stream_stream = NULL ;
static unsigned int as_output_threshold = OUTPUT_DEFAULT_THRESHOLD ;
static unsigned int as_output_prev_level = OUTPUT_DEFAULT_THRESHOLD ;
static unsigned int as_output_curr_level = OUTPUT_DEFAULT_THRESHOLD ;

void
set_application_name (char *argv0)
{
	char *temp = strrchr(argv0, '/' );
	ApplicationName = temp ? temp+1:argv0;
}

const char *
get_application_name()
{
	return ApplicationName;
}

unsigned int
set_output_threshold( unsigned int threshold )
{
  unsigned int old = as_output_threshold;
    as_output_threshold = threshold ;
    if( threshold > 0 )
        as_default_stream_stream = stderr ;
    return old;
}

unsigned int
get_output_threshold()
{
  return as_output_threshold ;
}


unsigned int
set_output_level( unsigned int level )
{
  unsigned int old = as_output_curr_level;
    if( as_output_curr_level != level && level  > OUTPUT_LEVEL_INVALID)
    {
        as_output_prev_level = as_output_curr_level ;
        as_output_curr_level = level ;
    }
    return old;
}

void
restore_output_level()
{
    as_output_curr_level = as_output_prev_level;
}

Bool
is_output_level_under_threshold( unsigned int level )
{
    if( level == OUTPUT_LEVEL_INVALID )
        return (as_output_curr_level <= as_output_threshold);
    else
        return (level <= as_output_threshold);
}

Bool
show_error( const char *error_format, ...)
{
    if( OUTPUT_LEVEL_ERROR <= as_output_threshold)
    {
        va_list ap;
        fprintf (stderr, "%s ERROR: ", ApplicationName );
        va_start (ap, error_format);
        vfprintf (stderr, error_format, ap);
        va_end (ap);
        fprintf (stderr, "\n" );
        return True;
    }
    return False;
}

Bool
show_system_error( const char *error_format, ...)
{
    if( OUTPUT_LEVEL_ERROR <= as_output_threshold)
    {
        va_list ap;
        fprintf (stderr, "%s SYSTEM ERROR: ", ApplicationName );
        va_start (ap, error_format);
        vfprintf (stderr, error_format, ap);
        va_end (ap);
        fprintf (stderr, ".\n\tSystem error text" );
        perror("");
        return True;
    }
    return False;
}

Bool
show_warning( const char *warning_format, ...)
{
    if( OUTPUT_LEVEL_WARNING <= as_output_threshold)
    {
        va_list ap;
        fprintf (stderr, "%s warning: ", ApplicationName );
        va_start (ap, warning_format);
        vfprintf (stderr, warning_format, ap);
        va_end (ap);
        fprintf (stderr, "\n" );
        return True;
    }
    return False;
}

Bool
show_progress( const char *msg_format, ...)
{
    if( OUTPUT_LEVEL_PROGRESS <= as_output_threshold)
    {
        va_list ap;
        fprintf (stderr, "%s : ", ApplicationName );
        va_start (ap, msg_format);
        vfprintf (stderr, msg_format, ap);
        va_end (ap);
        fprintf (stderr, "\n" );
        return True;
    }
    return False;
}

Bool
show_activity( const char *msg_format, ...)
{
    if( OUTPUT_LEVEL_ACTIVITY <= as_output_threshold)
    {
        va_list ap;
        fprintf (stderr, "%s : ", ApplicationName );
        va_start (ap, msg_format);
        vfprintf (stderr, msg_format, ap);
        va_end (ap);
        fprintf (stderr, "\n" );
        return True;
    }
    return False;
}

Bool
show_debug( const char *file, const char *func, int line, const char *msg_format, ...)
{
    if( OUTPUT_LEVEL_DEBUG <= as_output_threshold)
    {
        va_list ap;
        fprintf (stderr, "%s debug msg: %s:%s():%d: ", ApplicationName, file, func, line );
        va_start (ap, msg_format);
        vfprintf (stderr, msg_format, ap);
        va_end (ap);
        fprintf (stderr, "\n" );
        return True;
    }
    return False;
}

void debugout_print_linestamp(const char *file, const char *func, int line )
{	
 	static time_t startup_time = 0 ; 
 	static time_t last_timestamp = 0 ; 
	struct timeval tv;
	
	gettimeofday(&tv,NULL);
	if( startup_time == 0 )
		startup_time = tv.tv_sec;
	if( tv.tv_sec-last_timestamp > 60 ) 
	{
		fprintf( stderr, "%s:TS> %s", ApplicationName, ctime(&tv.tv_sec) );
		last_timestamp = tv.tv_sec ;
	}
	if( line > 0 )
	 	fprintf( stderr, "%s:%2.2li.%5.5li:%s:%s:%d>", ApplicationName, tv.tv_sec-startup_time, tv.tv_usec/10, file, func, line );
	else
	 	fprintf( stderr, "%s:%2.2li.%5.5li:%s:%s>", ApplicationName, tv.tv_sec-startup_time, tv.tv_usec/10, file, func );
	
}



inline void
nonGNUC_debugout_stub( const char *format, ...)
{}

void
nonGNUC_debugout( const char *format, ...)
{
    va_list ap;
    fprintf (stderr, "%s: ", ApplicationName );
    va_start (ap, format);
    vfprintf (stderr, format, ap);
    va_end (ap);
    fprintf (stderr, "\n" );
}

Bool
pre_print_check( stream_func *pfunc, void** pstream, void *data, const char *msg )
{

    if( *pfunc == NULL )
    {
        if( as_output_curr_level <= as_output_threshold )
            *pfunc = as_default_stream_func ;

        if( *pfunc == NULL )
            return False ;
    }

    if( *pstream == NULL )
        *pstream = as_default_stream_stream ;

    if( data == NULL && msg != NULL)
        (*pfunc)( *pstream, "ERROR=\"%s\"\n", msg);

    return (data != NULL);
}