2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
49 struct event_timeout *timer;
50 struct callback *timer_callback;
51 struct timeval last_flush;
55 struct log_data header;
57 struct log_data trailer;
62 strftime_localtime(char *buffer, int size, char *fmt)
69 strftime(buffer, 4096, fmt, tm);
73 expand_filenames(struct log *this_)
78 strftime_localtime(buffer, 4096, this_->filename);
79 this_->filename_ex1=g_strdup(buffer);
80 if (strstr(this_->filename_ex1,"%i")) {
83 g_free(this_->filename_ex2);
84 this_->filename_ex2=g_strdup_printf(this_->filename_ex1,i++);
85 } while (file_exists(this_->filename_ex2));
87 this_->filename_ex2=g_strdup(this_->filename_ex1);
91 log_set_last_flush(struct log *this_)
93 gettimeofday(&this_->last_flush, NULL);
97 log_open(struct log *this_)
100 if (this_->overwrite)
105 file_mkdir(this_->filename_ex2, 2);
106 this_->f=fopen(this_->filename_ex2, mode);
108 this_->f=fopen(this_->filename_ex2, "w");
111 if (!this_->overwrite)
112 fseek(this_->f, 0, SEEK_END);
113 this_->empty = !ftell(this_->f);
114 log_set_last_flush(this_);
118 log_close(struct log *this_)
122 if (this_->trailer.len)
123 fwrite(this_->trailer.data, 1, this_->trailer.len, this_->f);
130 log_flush(struct log *this_)
133 if (this_->lazy && !this_->f) {
134 if (!this_->data.len)
141 if (this_->header.len)
142 fwrite(this_->header.data, 1, this_->header.len, this_->f);
143 if (this_->header.len || this_->data.len)
146 fwrite(this_->data.data, 1, this_->data.len, this_->f);
147 if (this_->trailer.len) {
150 fwrite(this_->trailer.data, 1, this_->trailer.len, this_->f);
151 fseek(this_->f, pos, SEEK_SET);
156 g_free(this_->data.data);
157 this_->data.data=NULL;
158 this_->data.max_len=this_->data.len=0;
159 log_set_last_flush(this_);
163 log_flush_required(struct log *this_)
165 return this_->data.len > this_->flush_size;
170 log_change(struct log *this_)
174 expand_filenames(this_);
180 log_change_required(struct log *this_)
184 strftime_localtime(buffer, 4096, this_->filename);
185 return (strcmp(this_->filename_ex1, buffer) != 0);
189 log_timer(struct log *this_)
193 gettimeofday(&tv, NULL);
194 delta=(tv.tv_sec-this_->last_flush.tv_sec)*1000+(tv.tv_usec-this_->last_flush.tv_usec)/1000;
195 dbg(1,"delta=%d flush_time=%d\n", delta, this_->flush_time);
196 if (this_->flush_time && delta >= this_->flush_time*1000)
201 log_get_attr(struct log *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
203 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
208 log_new(struct attr * parent,struct attr **attrs)
210 struct log *ret=g_new0(struct log, 1);
211 struct attr *data,*overwrite,*lazy,*mkdir,*flush_size,*flush_time;
212 struct file_wordexp *wexp;
213 char *filename, **wexp_data;
216 data=attr_search(attrs, NULL, attr_data);
219 filename=data->u.str;
220 wexp=file_wordexp_new(filename);
221 if (wexp && file_wordexp_get_count(wexp) > 0) {
222 wexp_data=file_wordexp_get_array(wexp);
223 filename=wexp_data[0];
226 ret->filename=g_strdup(filename);
228 file_wordexp_destroy(wexp);
229 overwrite=attr_search(attrs, NULL, attr_overwrite);
231 ret->overwrite=overwrite->u.num;
232 lazy=attr_search(attrs, NULL, attr_lazy);
234 ret->lazy=lazy->u.num;
235 mkdir=attr_search(attrs, NULL, attr_mkdir);
237 ret->mkdir=mkdir->u.num;
238 flush_size=attr_search(attrs, NULL, attr_flush_size);
240 ret->flush_size=flush_size->u.num;
241 flush_time=attr_search(attrs, NULL, attr_flush_time);
243 ret->flush_time=flush_time->u.num;
244 if (ret->flush_time) {
245 dbg(1,"interval %d\n", ret->flush_time*1000);
246 ret->timer_callback=callback_new_1(callback_cast(log_timer), ret);
247 ret->timer=event_add_timeout(ret->flush_time*1000, 1, ret->timer_callback);
249 expand_filenames(ret);
251 log_set_last_flush(ret);
254 ret->attrs=attr_list_dup(attrs);
259 log_set_header(struct log *this_, char *data, int len)
261 this_->header.data=g_malloc(len);
262 this_->header.max_len=this_->header.len=len;
263 memcpy(this_->header.data, data, len);
267 log_set_trailer(struct log *this_, char *data, int len)
269 this_->trailer.data=g_malloc(len);
270 this_->trailer.max_len=this_->trailer.len=len;
271 memcpy(this_->trailer.data, data, len);
275 log_write(struct log *this_, char *data, int len)
278 if (log_change_required(this_)) {
282 if (this_->data.len + len > this_->data.max_len) {
284 this_->data.max_len+=16384;
285 this_->data.data=g_realloc(this_->data.data,this_->data.max_len);
287 memcpy(this_->data.data+this_->data.len, data, len);
288 this_->data.len+=len;
289 if (log_flush_required(this_))
294 log_destroy(struct log *this_)
296 callback_destroy(this_->timer_callback);
297 event_remove_timeout(this_->timer);