Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/src/logerror.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1440 - (show annotations) (download) (as text)
Sat Jun 29 16:28:51 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 4710 byte(s)
Avoid writing uninitialised to disk (found by valgrind).

1 /***************************************
2 Error logging functions
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2013 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 */
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 off_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 errorlogfile=fopen(errorlogfilename,append?"a":"w");
73
74 if(!errorlogfile)
75 {
76 fprintf(stderr,"Cannot open file '%s' for writing [%s].\n",errorlogfilename,strerror(errno));
77 exit(EXIT_FAILURE);
78 }
79
80 /* Binary log file */
81
82 if(bin)
83 {
84 errorbinfilename=(char*)malloc(strlen(filename)+8);
85
86 sprintf(errorbinfilename,"%s.tmp",filename);
87
88 errorfileoffset=0;
89
90 if(append)
91 {
92 if(ExistsFile(filename))
93 errorfileoffset=SizeFile(filename);
94
95 errorbinfile=OpenFileBufferedAppend(errorbinfilename);
96 }
97 else
98 errorbinfile=OpenFileBufferedNew(errorbinfilename);
99 }
100 else
101 errorbinfile=-1;
102 }
103
104
105 /*++++++++++++++++++++++++++++++++++++++
106 Close the error log file.
107 ++++++++++++++++++++++++++++++++++++++*/
108
109 void close_errorlog(void)
110 {
111 if(errorlogfile)
112 {
113 fclose(errorlogfile);
114
115 if(errorbinfile!=-1)
116 CloseFileBuffered(errorbinfile);
117 }
118 }
119
120
121 /*++++++++++++++++++++++++++++++++++++++
122 Log a message to the error log file.
123
124 const char *format The format string.
125
126 ... The other arguments.
127 ++++++++++++++++++++++++++++++++++++++*/
128
129 void logerror(const char *format, ...)
130 {
131 va_list ap;
132
133 if(!errorlogfile)
134 return;
135
136 va_start(ap,format);
137
138 errorfileoffset+=vfprintf(errorlogfile,format,ap);
139
140 va_end(ap);
141 }
142
143
144 /*++++++++++++++++++++++++++++++++++++++
145 Store the node information in the binary log file for this message.
146
147 node_t logerror_node Returns the node identifier.
148
149 node_t The node identifier.
150 ++++++++++++++++++++++++++++++++++++++*/
151
152 node_t logerror_node(node_t id)
153 {
154 if(errorbinfile!=-1)
155 {
156 ErrorLogObject error={0};
157
158 error.id=id;
159 error.type='N';
160
161 error.offset=errorfileoffset;
162
163 WriteFileBuffered(errorbinfile,&error,sizeof(ErrorLogObject));
164 }
165
166 return(id);
167 }
168
169
170 /*++++++++++++++++++++++++++++++++++++++
171 Store the way information in the binary log file for this message.
172
173 way_t logerror_way Returns the way identifier.
174
175 way_t The way identifier.
176 ++++++++++++++++++++++++++++++++++++++*/
177
178 way_t logerror_way(way_t id)
179 {
180 if(errorbinfile!=-1)
181 {
182 ErrorLogObject error={0};
183
184 error.id=id;
185 error.type='W';
186
187 error.offset=errorfileoffset;
188
189 WriteFileBuffered(errorbinfile,&error,sizeof(ErrorLogObject));
190 }
191
192 return(id);
193 }
194
195
196 /*++++++++++++++++++++++++++++++++++++++
197 Store the relation information in the binary log file for this message.
198
199 relation_t logerror_relation Returns the relation identifier.
200
201 relation_t The relation identifier.
202 ++++++++++++++++++++++++++++++++++++++*/
203
204 relation_t logerror_relation(relation_t id)
205 {
206 if(errorbinfile!=-1)
207 {
208 ErrorLogObject error={0};
209
210 error.id=id;
211 error.type='R';
212
213 error.offset=errorfileoffset;
214
215 WriteFileBuffered(errorbinfile,&error,sizeof(ErrorLogObject));
216 }
217
218 return(id);
219 }