Routino SVN Repository Browser

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

ViewVC logotype

Annotation of /trunk/src/logerror.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1313 - (hide annotations) (download) (as text)
Sat May 11 18:14:38 2013 UTC (11 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 4708 byte(s)
Create a binary log file that contains the node, way and relation id and a link
to the error message for easy parsing.

1 amb 1313 /***************************************
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 "logerror.h"
31     #include "sorting.h"
32    
33    
34     /* Local variables */
35    
36     /*+ The name of the error log file. +*/
37     static char *errorlogfilename=NULL;
38    
39     /*+ The file handle for the error log file. +*/
40     static FILE *errorlogfile=NULL;
41    
42     /*+ The name of the binary error log file. +*/
43     static char *errorbinfilename=NULL;
44    
45     /*+ The file descriptor for the binary error log file. +*/
46     static int errorbinfile=-1;
47    
48     /*+ The offset of the error message in the error log file. +*/
49     static off_t errorfileoffset=0;
50    
51    
52     /*++++++++++++++++++++++++++++++++++++++
53     Create the error log file.
54    
55     const char *filename The name of the file to create.
56    
57     int append The option to append to an existing file.
58    
59     int bin The option to enable a binary log file.
60     ++++++++++++++++++++++++++++++++++++++*/
61    
62     void open_errorlog(const char *filename,int append,int bin)
63     {
64     /* Text log file */
65    
66     errorlogfilename=(char*)malloc(strlen(filename)+8);
67    
68     strcpy(errorlogfilename,filename);
69    
70     errorlogfile=fopen(errorlogfilename,append?"a":"w");
71    
72     if(!errorlogfile)
73     {
74     fprintf(stderr,"Cannot open file '%s' for writing [%s].\n",errorlogfilename,strerror(errno));
75     exit(EXIT_FAILURE);
76     }
77    
78     /* Binary log file */
79    
80     if(bin)
81     {
82     errorbinfilename=(char*)malloc(strlen(filename)+8);
83    
84     sprintf(errorbinfilename,"%s.tmp",filename);
85    
86     errorfileoffset=0;
87    
88     if(append)
89     {
90     if(ExistsFile(filename))
91     errorfileoffset=SizeFile(filename);
92    
93     errorbinfile=OpenFileAppend(errorbinfilename);
94     }
95     else
96     errorbinfile=OpenFileNew(errorbinfilename);
97     }
98     else
99     errorbinfile=-1;
100     }
101    
102    
103     /*++++++++++++++++++++++++++++++++++++++
104     Close the error log file.
105     ++++++++++++++++++++++++++++++++++++++*/
106    
107     void close_errorlog(void)
108     {
109     if(errorlogfile)
110     {
111     fclose(errorlogfile);
112    
113     CloseFile(errorbinfile);
114     }
115     }
116    
117    
118     /*++++++++++++++++++++++++++++++++++++++
119     Log a message to the error log file.
120    
121     const char *format The format string.
122    
123     ... The other arguments.
124     ++++++++++++++++++++++++++++++++++++++*/
125    
126     void logerror(const char *format, ...)
127     {
128     va_list ap;
129    
130     if(!errorlogfile)
131     return;
132    
133     va_start(ap,format);
134    
135     errorfileoffset+=vfprintf(errorlogfile,format,ap);
136    
137     va_end(ap);
138     }
139    
140    
141     /*++++++++++++++++++++++++++++++++++++++
142     Store the node information in the binary log file for this message.
143    
144     node_t logerror_node Returns the node identifier.
145    
146     node_t The node identifier.
147     ++++++++++++++++++++++++++++++++++++++*/
148    
149     node_t logerror_node(node_t id)
150     {
151     if(errorbinfile!=-1)
152     {
153     ErrorLogObject error;
154    
155     error.type_id=id;
156     error.type_id|=((uint64_t)'N')<<56;
157    
158     error.offset=errorfileoffset;
159    
160     WriteFile(errorbinfile,&error,sizeof(ErrorLogObject));
161     }
162    
163     return(id);
164     }
165    
166    
167     /*++++++++++++++++++++++++++++++++++++++
168     Store the way information in the binary log file for this message.
169    
170     way_t logerror_way Returns the way identifier.
171    
172     way_t The way identifier.
173     ++++++++++++++++++++++++++++++++++++++*/
174    
175     way_t logerror_way(way_t id)
176     {
177     if(errorbinfile!=-1)
178     {
179     ErrorLogObject error;
180    
181     error.type_id=id;
182     error.type_id|=((uint64_t)'W')<<56;
183    
184     error.offset=errorfileoffset;
185    
186     WriteFile(errorbinfile,&error,sizeof(ErrorLogObject));
187     }
188    
189     return(id);
190     }
191    
192    
193     /*++++++++++++++++++++++++++++++++++++++
194     Store the relation information in the binary log file for this message.
195    
196     relation_t logerror_relation Returns the relation identifier.
197    
198     relation_t The relation identifier.
199     ++++++++++++++++++++++++++++++++++++++*/
200    
201     relation_t logerror_relation(relation_t id)
202     {
203     if(errorbinfile!=-1)
204     {
205     ErrorLogObject error;
206    
207     error.type_id=id;
208     error.type_id|=((uint64_t)'R')<<56;
209    
210     error.offset=errorfileoffset;
211    
212     WriteFile(errorbinfile,&error,sizeof(ErrorLogObject));
213     }
214    
215     return(id);
216     }