1 | /*
|
---|
2 | * TOPPERS Software
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems
|
---|
4 | *
|
---|
5 | * Copyright (C) 2007-2008 by TAKAGI Nobuhisa
|
---|
6 | *
|
---|
7 | * 上記著作権者は,以下の(1)〜(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
8 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
9 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
10 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
11 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
12 | * スコード中に含まれていること.
|
---|
13 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
14 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
15 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
16 | * の無保証規定を掲載すること.
|
---|
17 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
18 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
19 | * と.
|
---|
20 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
21 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
22 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
23 | * 報告すること.
|
---|
24 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
25 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
26 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
27 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
28 | * 免責すること.
|
---|
29 | *
|
---|
30 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
31 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
32 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
33 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
34 | * の責任を負わない.
|
---|
35 | *
|
---|
36 | */
|
---|
37 | #include <cstdio>
|
---|
38 | #include <string>
|
---|
39 | #include "toppers/diagnostics.hpp"
|
---|
40 | #include "toppers/text_line.hpp"
|
---|
41 |
|
---|
42 | namespace toppers
|
---|
43 | {
|
---|
44 | namespace
|
---|
45 | {
|
---|
46 | int error_abort_threshold = 100;
|
---|
47 | int error_count = 0;
|
---|
48 | std::string program_name( "(unknown)" );
|
---|
49 | std::string error_location;
|
---|
50 | }
|
---|
51 |
|
---|
52 | int get_error_count()
|
---|
53 | {
|
---|
54 | return error_count;
|
---|
55 | }
|
---|
56 |
|
---|
57 | int increment_error_count()
|
---|
58 | {
|
---|
59 | return ++error_count;
|
---|
60 | }
|
---|
61 |
|
---|
62 | void set_program_name( char const* name )
|
---|
63 | {
|
---|
64 | program_name = name;
|
---|
65 | }
|
---|
66 |
|
---|
67 | std::string const& get_program_name()
|
---|
68 | {
|
---|
69 | return program_name;
|
---|
70 | }
|
---|
71 |
|
---|
72 | int set_error_abort_threshold( int thresh )
|
---|
73 | {
|
---|
74 | if ( thresh < 1 )
|
---|
75 | {
|
---|
76 | return -1;
|
---|
77 | }
|
---|
78 | int previous = error_abort_threshold;
|
---|
79 | error_abort_threshold = thresh;
|
---|
80 | return previous;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void warning( const char* msg )
|
---|
84 | {
|
---|
85 | if ( !error_location.empty() ) fprintf( stderr, "In function `%s`:\n", error_location.c_str() );
|
---|
86 | fprintf( stderr, "%s: %s: %s\n", program_name.c_str(), _( "warning" ), msg );
|
---|
87 | error_location.clear();
|
---|
88 | }
|
---|
89 | void warning( text_line const& line, const char* msg )
|
---|
90 | {
|
---|
91 | if ( !error_location.empty() ) fprintf( stderr, "In function `%s`:\n", error_location.c_str() );
|
---|
92 | fprintf( stderr, "%s:%s:%ld: %s: %s\n", program_name.c_str(), line.file.c_str(), line.line, _( "warning" ), msg );
|
---|
93 | error_location.clear();
|
---|
94 | }
|
---|
95 | void error( const char* msg )
|
---|
96 | {
|
---|
97 | if ( !error_location.empty() ) fprintf( stderr, "In function `%s`:\n", error_location.c_str() );
|
---|
98 | fprintf( stderr, "%s: %s: %s\n", program_name.c_str(), _( "error" ), msg );
|
---|
99 | ++error_count;
|
---|
100 | if ( error_abort_threshold <= error_count )
|
---|
101 | {
|
---|
102 | error_location.clear();
|
---|
103 | throw diagnostics_error( _( "too many errors" ) );
|
---|
104 | }
|
---|
105 | }
|
---|
106 | void error( text_line const& line, const char* msg )
|
---|
107 | {
|
---|
108 | if ( !error_location.empty() ) fprintf( stderr, "In function `%s`:\n", error_location.c_str() );
|
---|
109 | fprintf( stderr, "%s:%s:%ld: %s: %s\n", program_name.c_str(), line.file.c_str(), line.line, _( "error" ), msg );
|
---|
110 | ++error_count;
|
---|
111 | if ( error_abort_threshold <= error_count )
|
---|
112 | {
|
---|
113 | error_location.clear();
|
---|
114 | throw diagnostics_error( _( "too many errors" ) );
|
---|
115 | }
|
---|
116 | }
|
---|
117 | void fatal( const char* msg )
|
---|
118 | {
|
---|
119 | if ( !error_location.empty() ) fprintf( stderr, "In function `%s`:\n", error_location.c_str() );
|
---|
120 | fprintf( stderr, "%s: %s: %s\n", program_name.c_str(), _( "error" ), msg );
|
---|
121 | error_location.clear();
|
---|
122 | throw diagnostics_error( _( "fatal error" ) );
|
---|
123 | }
|
---|
124 | void fatal( text_line const& line, const char* msg )
|
---|
125 | {
|
---|
126 | if ( !error_location.empty() ) fprintf( stderr, "In function `%s`:\n", error_location.c_str() );
|
---|
127 | fprintf( stderr, "%s:%s:%ld: %s: %s\n", program_name.c_str(), line.file.c_str(), line.line, _( "error" ), msg );
|
---|
128 | error_location.clear();
|
---|
129 | throw diagnostics_error( _( "fatal error" ) );
|
---|
130 | }
|
---|
131 |
|
---|
132 | void set_error_location( char const* msg )
|
---|
133 | {
|
---|
134 | error_location = msg;
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|