Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/logerror.c
Parent Directory
|
Revision Log
Revision 1784 -
(show annotations)
(download)
(as text)
Sat Aug 15 13:08:37 2015 UTC (9 years, 7 months ago) by amb
File MIME type: text/x-csrc
File size: 4890 byte(s)
Sat Aug 15 13:08:37 2015 UTC (9 years, 7 months ago) by amb
File MIME type: text/x-csrc
File size: 4890 byte(s)
Merge libroutino branch back into the trunk.
1 | /*************************************** |
2 | Error logging functions |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2013, 2015 Andrew M. Bishop |
7 | |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | ***************************************/ |
21 | |
22 | |
23 | #include <stdio.h> |
24 | #include <stdarg.h> |
25 | #include <errno.h> |
26 | |
27 | #include "typesx.h" |
28 | |
29 | #include "files.h" |
30 | #include "logging.h" |
31 | |
32 | |
33 | /* Global variables */ |
34 | |
35 | /*+ The name of the error log file. +*/ |
36 | char *errorlogfilename=NULL; |
37 | |
38 | /*+ The name of the binary error log file. +*/ |
39 | char *errorbinfilename=NULL; |
40 | |
41 | |
42 | /* Local variables (re-initialised by open_errorlog() function) */ |
43 | |
44 | /*+ The file handle for the error log file. +*/ |
45 | static FILE *errorlogfile=NULL; |
46 | |
47 | /*+ The file descriptor for the binary error log file. +*/ |
48 | static int errorbinfile=-1; |
49 | |
50 | /*+ The offset of the error message in the error log file. +*/ |
51 | static offset_t errorfileoffset=0; |
52 | |
53 | |
54 | /*++++++++++++++++++++++++++++++++++++++ |
55 | Create the error log file. |
56 | |
57 | const char *filename The name of the file to create. |
58 | |
59 | int append The option to append to an existing file. |
60 | |
61 | int bin The option to enable a binary log file. |
62 | ++++++++++++++++++++++++++++++++++++++*/ |
63 | |
64 | void open_errorlog(const char *filename,int append,int bin) |
65 | { |
66 | /* Text log file */ |
67 | |
68 | errorlogfilename=(char*)malloc(strlen(filename)+8); |
69 | |
70 | strcpy(errorlogfilename,filename); |
71 | |
72 | #if defined(_MSC_VER) || defined(__MINGW32__) |
73 | errorlogfile=fopen(errorlogfilename,append?"ab":"wb"); |
74 | #else |
75 | errorlogfile=fopen(errorlogfilename,append?"a" :"w" ); |
76 | #endif |
77 | |
78 | if(!errorlogfile) |
79 | { |
80 | fprintf(stderr,"Cannot open file '%s' for writing [%s].\n",errorlogfilename,strerror(errno)); |
81 | exit(EXIT_FAILURE); |
82 | } |
83 | |
84 | /* Binary log file */ |
85 | |
86 | if(bin) |
87 | { |
88 | errorbinfilename=(char*)malloc(strlen(filename)+8); |
89 | |
90 | sprintf(errorbinfilename,"%s.tmp",filename); |
91 | |
92 | errorfileoffset=0; |
93 | |
94 | if(append) |
95 | { |
96 | if(ExistsFile(filename)) |
97 | errorfileoffset=SizeFile(filename); |
98 | |
99 | errorbinfile=OpenFileBufferedAppend(errorbinfilename); |
100 | } |
101 | else |
102 | errorbinfile=OpenFileBufferedNew(errorbinfilename); |
103 | } |
104 | else |
105 | errorbinfile=-1; |
106 | } |
107 | |
108 | |
109 | /*++++++++++++++++++++++++++++++++++++++ |
110 | Close the error log file. |
111 | ++++++++++++++++++++++++++++++++++++++*/ |
112 | |
113 | void close_errorlog(void) |
114 | { |
115 | if(errorlogfile) |
116 | { |
117 | fclose(errorlogfile); |
118 | |
119 | if(errorbinfile!=-1) |
120 | CloseFileBuffered(errorbinfile); |
121 | } |
122 | } |
123 | |
124 | |
125 | /*++++++++++++++++++++++++++++++++++++++ |
126 | Log a message to the error log file. |
127 | |
128 | const char *format The format string. |
129 | |
130 | ... The other arguments. |
131 | ++++++++++++++++++++++++++++++++++++++*/ |
132 | |
133 | void logerror(const char *format, ...) |
134 | { |
135 | va_list ap; |
136 | |
137 | if(!errorlogfile) |
138 | return; |
139 | |
140 | va_start(ap,format); |
141 | |
142 | errorfileoffset+=vfprintf(errorlogfile,format,ap); |
143 | |
144 | va_end(ap); |
145 | } |
146 | |
147 | |
148 | /*++++++++++++++++++++++++++++++++++++++ |
149 | Store the node information in the binary log file for this message. |
150 | |
151 | node_t logerror_node Returns the node identifier. |
152 | |
153 | node_t id The node identifier. |
154 | ++++++++++++++++++++++++++++++++++++++*/ |
155 | |
156 | node_t logerror_node(node_t id) |
157 | { |
158 | if(errorbinfile!=-1) |
159 | { |
160 | ErrorLogObject error={0}; |
161 | |
162 | error.id=id; |
163 | error.type='N'; |
164 | |
165 | error.offset=errorfileoffset; |
166 | |
167 | WriteFileBuffered(errorbinfile,&error,sizeof(ErrorLogObject)); |
168 | } |
169 | |
170 | return(id); |
171 | } |
172 | |
173 | |
174 | /*++++++++++++++++++++++++++++++++++++++ |
175 | Store the way information in the binary log file for this message. |
176 | |
177 | way_t logerror_way Returns the way identifier. |
178 | |
179 | way_t id The way identifier. |
180 | ++++++++++++++++++++++++++++++++++++++*/ |
181 | |
182 | way_t logerror_way(way_t id) |
183 | { |
184 | if(errorbinfile!=-1) |
185 | { |
186 | ErrorLogObject error={0}; |
187 | |
188 | error.id=id; |
189 | error.type='W'; |
190 | |
191 | error.offset=errorfileoffset; |
192 | |
193 | WriteFileBuffered(errorbinfile,&error,sizeof(ErrorLogObject)); |
194 | } |
195 | |
196 | return(id); |
197 | } |
198 | |
199 | |
200 | /*++++++++++++++++++++++++++++++++++++++ |
201 | Store the relation information in the binary log file for this message. |
202 | |
203 | relation_t logerror_relation Returns the relation identifier. |
204 | |
205 | relation_t id The relation identifier. |
206 | ++++++++++++++++++++++++++++++++++++++*/ |
207 | |
208 | relation_t logerror_relation(relation_t id) |
209 | { |
210 | if(errorbinfile!=-1) |
211 | { |
212 | ErrorLogObject error={0}; |
213 | |
214 | error.id=id; |
215 | error.type='R'; |
216 | |
217 | error.offset=errorfileoffset; |
218 | |
219 | WriteFileBuffered(errorbinfile,&error,sizeof(ErrorLogObject)); |
220 | } |
221 | |
222 | return(id); |
223 | } |