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.
50 struct event_timeout *timer;
51 struct callback *timer_callback;
52 struct timeval last_flush;
56 struct log_data header;
58 struct log_data trailer;
63 strftime_localtime(char *buffer, int size, char *fmt)
70 strftime(buffer, 4096, fmt, tm);
74 expand_filenames(struct log *this_)
79 strftime_localtime(buffer, 4096, this_->filename);
80 this_->filename_ex1=g_strdup(buffer);
81 if (strstr(this_->filename_ex1,"%i")) {
84 g_free(this_->filename_ex2);
85 this_->filename_ex2=g_strdup_printf(this_->filename_ex1,i++);
86 } while (file_exists(this_->filename_ex2));
88 this_->filename_ex2=g_strdup(this_->filename_ex1);
92 log_set_last_flush(struct log *this_)
94 gettimeofday(&this_->last_flush, NULL);
98 log_open(struct log *this_)
101 if (this_->overwrite)
106 file_mkdir(this_->filename_ex2, 2);
107 this_->f=fopen(this_->filename_ex2, mode);
109 this_->f=fopen(this_->filename_ex2, "w");
112 if (!this_->overwrite)
113 fseek(this_->f, 0, SEEK_END);
114 this_->empty = !ftell(this_->f);
115 log_set_last_flush(this_);
119 log_close(struct log *this_)
123 if (this_->trailer.len)
124 fwrite(this_->trailer.data, 1, this_->trailer.len, this_->f);
131 log_flush(struct log *this_, enum log_flags flags)
134 if (this_->lazy && !this_->f) {
135 if (!this_->data.len)
142 if (this_->header.len)
143 fwrite(this_->header.data, 1, this_->header.len, this_->f);
144 if (this_->header.len || this_->data.len)
147 fwrite(this_->data.data, 1, this_->data.len, this_->f);
148 #ifndef HAVE_API_WIN32_BASE
149 if (flags & log_flag_truncate) {
151 ftruncate(fileno(this_->f), pos);
154 if (this_->trailer.len) {
157 fwrite(this_->trailer.data, 1, this_->trailer.len, this_->f);
158 fseek(this_->f, pos, SEEK_SET);
161 if (flags & log_flag_keep_pointer)
162 fseek(this_->f, -this_->data.len, SEEK_CUR);
164 if (!(flags & log_flag_keep_buffer)) {
165 g_free(this_->data.data);
166 this_->data.data=NULL;
167 this_->data.max_len=this_->data.len=0;
169 log_set_last_flush(this_);
173 log_flush_required(struct log *this_)
175 return this_->data.len > this_->flush_size;
180 log_change(struct log *this_)
184 expand_filenames(this_);
190 log_change_required(struct log *this_)
194 strftime_localtime(buffer, 4096, this_->filename);
195 return (strcmp(this_->filename_ex1, buffer) != 0);
199 log_timer(struct log *this_)
203 gettimeofday(&tv, NULL);
204 delta=(tv.tv_sec-this_->last_flush.tv_sec)*1000+(tv.tv_usec-this_->last_flush.tv_usec)/1000;
205 dbg(1,"delta=%d flush_time=%d\n", delta, this_->flush_time);
206 if (this_->flush_time && delta >= this_->flush_time*1000)
211 log_get_attr(struct log *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
213 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
218 log_new(struct attr * parent,struct attr **attrs)
220 struct log *ret=g_new0(struct log, 1);
221 struct attr *data,*overwrite,*lazy,*mkdir,*flush_size,*flush_time;
222 struct file_wordexp *wexp;
223 char *filename, **wexp_data;
226 data=attr_search(attrs, NULL, attr_data);
229 filename=data->u.str;
230 wexp=file_wordexp_new(filename);
231 if (wexp && file_wordexp_get_count(wexp) > 0) {
232 wexp_data=file_wordexp_get_array(wexp);
233 filename=wexp_data[0];
236 ret->filename=g_strdup(filename);
238 file_wordexp_destroy(wexp);
239 overwrite=attr_search(attrs, NULL, attr_overwrite);
241 ret->overwrite=overwrite->u.num;
242 lazy=attr_search(attrs, NULL, attr_lazy);
244 ret->lazy=lazy->u.num;
245 mkdir=attr_search(attrs, NULL, attr_mkdir);
247 ret->mkdir=mkdir->u.num;
248 flush_size=attr_search(attrs, NULL, attr_flush_size);
250 ret->flush_size=flush_size->u.num;
251 flush_time=attr_search(attrs, NULL, attr_flush_time);
253 ret->flush_time=flush_time->u.num;
254 if (ret->flush_time) {
255 dbg(1,"interval %d\n", ret->flush_time*1000);
256 ret->timer_callback=callback_new_1(callback_cast(log_timer), ret);
257 ret->timer=event_add_timeout(ret->flush_time*1000, 1, ret->timer_callback);
259 expand_filenames(ret);
261 log_set_last_flush(ret);
264 ret->attrs=attr_list_dup(attrs);
269 log_set_header(struct log *this_, char *data, int len)
271 this_->header.data=g_malloc(len);
272 this_->header.max_len=this_->header.len=len;
273 memcpy(this_->header.data, data, len);
277 log_set_trailer(struct log *this_, char *data, int len)
279 this_->trailer.data=g_malloc(len);
280 this_->trailer.max_len=this_->trailer.len=len;
281 memcpy(this_->trailer.data, data, len);
285 log_write(struct log *this_, char *data, int len, enum log_flags flags)
288 if (log_change_required(this_)) {
292 if (flags & log_flag_replace_buffer)
294 if (this_->data.len + len > this_->data.max_len) {
296 this_->data.max_len+=16384;
297 this_->data.data=g_realloc(this_->data.data,this_->data.max_len);
299 memcpy(this_->data.data+this_->data.len, data, len);
300 this_->data.len+=len;
301 if (log_flush_required(this_) || (flags & log_flag_force_flush))
302 log_flush(this_, flags);
306 log_get_buffer(struct log *this_, int *len)
309 *len=this_->data.len;
310 return this_->data.data;
315 log_printf(struct log *this_, char *fmt, ...)
317 char buffer[LOG_BUFFER_SIZE];
323 // Format the string and write it to the log
324 size = vsnprintf(buffer, LOG_BUFFER_SIZE, fmt, ap);
325 log_write(this_, buffer, size, 0);
331 log_destroy(struct log *this_)
333 callback_destroy(this_->timer_callback);
334 event_remove_timeout(this_->timer);