~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to ftsh/src/ftsh_error.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "ftsh_error.h"
 
9
 
 
10
#include <stdarg.h>
 
11
#include <stdio.h>
 
12
#include <stdlib.h>
 
13
#include <time.h>
 
14
#include <sys/time.h>
 
15
#include <unistd.h>
 
16
#include <string.h>
 
17
 
 
18
static FILE * error_stream = 0;
 
19
static int error_level = FTSH_ERROR_FAILURE;
 
20
static const char * error_name = "unknown";
 
21
static int decimal_time = 0;
 
22
 
 
23
void ftsh_error_name( const char *name )
 
24
{
 
25
        error_name = name;
 
26
}
 
27
 
 
28
void ftsh_error_level( int level )
 
29
{
 
30
        error_level = level;
 
31
}
 
32
 
 
33
void ftsh_error_stream( FILE *stream )
 
34
{
 
35
        error_stream = stream;
 
36
}
 
37
 
 
38
void ftsh_error_decimal_time( int onoff )
 
39
{
 
40
        decimal_time = onoff;
 
41
}
 
42
 
 
43
static char * make_prefix( int line )
 
44
{
 
45
        static char txt[1024];
 
46
        struct timeval tv;
 
47
        char *c;
 
48
 
 
49
        gettimeofday(&tv,0);
 
50
 
 
51
        if(decimal_time) {
 
52
                sprintf(txt,"%d.%06d [%d] %s:%d",(int)tv.tv_sec,(int)tv.tv_usec,(int)getpid(),error_name,line);
 
53
        } else {
 
54
                time_t t = tv.tv_sec;
 
55
                sprintf(txt,"%s[%d] %s:%d",ctime(&t),(int)getpid(),error_name,line);
 
56
                c = strchr(txt,'\n');
 
57
                if(c) *c = ' ';
 
58
        }
 
59
 
 
60
        return txt;
 
61
}
 
62
 
 
63
static void do_error( int level, int line, const char *fmt, va_list args )
 
64
{
 
65
        if(!error_stream) {
 
66
                error_stream = stderr;
 
67
        }
 
68
        if(error_level>=level) {
 
69
                fprintf(error_stream,"%s ",make_prefix(line));
 
70
                vfprintf(error_stream,fmt,args);
 
71
                fprintf(error_stream,"\n");
 
72
                fflush(error_stream);
 
73
        }
 
74
}
 
75
 
 
76
void ftsh_error( int level, int line, const char *fmt, ... )
 
77
{
 
78
        va_list args;
 
79
        va_start(args,fmt);
 
80
        do_error(level,line,fmt,args);
 
81
        va_end(args);
 
82
}
 
83
 
 
84
void ftsh_fatal( int line, const char *fmt, ... )
 
85
{
 
86
        va_list args;
 
87
        va_start(args,fmt);
 
88
        do_error(error_level,line,fmt,args);
 
89
        exit(1);
 
90
        va_end(args);
 
91
}