navit
0.5.3-trunk
|
The log object. More...
#include "config.h"
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <glib.h>
#include "file.h"
#include "item.h"
#include "event.h"
#include "callback.h"
#include "debug.h"
#include "xmlconfig.h"
#include "log.h"
#include <errno.h>
Data Structures | |
struct | log_data |
struct | log |
Functions | |
static void | strftime_localtime (char *buffer, int size, char *fmt) |
Stores formatted time to a string. More... | |
static void | expand_filenames (struct log *this_) |
Expands placeholders in a filename. More... | |
static void | log_set_last_flush (struct log *this_) |
Sets the time at which the log buffer was last flushed. More... | |
static void | log_open (struct log *this_) |
Opens a log file. More... | |
static void | log_close (struct log *this_) |
Closes a log file. More... | |
static void | log_flush (struct log *this_, enum log_flags flags) |
Flushes the buffer of a log. More... | |
static int | log_flush_required (struct log *this_) |
Determines if the maximum buffer size of a log has been exceeded. More... | |
static void | log_change (struct log *this_) |
Rotates a log file. More... | |
static int | log_change_required (struct log *this_) |
Determines if the log must be rotated. More... | |
static void | log_timer (struct log *this_) |
Determines if the flush interval of a log has elapsed and flushes the buffer if needed. More... | |
int | log_get_attr (struct log *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter) |
Gets an attribute. More... | |
struct log * | log_new (struct attr *parent, struct attr **attrs) |
Creates and initializes a new log object. More... | |
void | log_set_header (struct log *this_, char *data, int len) |
Sets the header for a log file. More... | |
void | log_set_trailer (struct log *this_, char *data, int len) |
Sets the trailer for a log file. More... | |
void | log_write (struct log *this_, char *data, int len, enum log_flags flags) |
Writes to a log. More... | |
void * | log_get_buffer (struct log *this_, int *len) |
Returns the data buffer of a log object and its length. More... | |
void | log_printf (struct log *this_, char *fmt,...) |
Writes a formatted string to a log. More... | |
void | log_destroy (struct log *this_) |
Destroys a log object and frees up its memory. More... | |
Variables | |
struct object_func | log_func |
The log object.
This file implements everything needed for logging: the log object and its functions.
|
static |
Expands placeholders in a filename.
This function examines the
and replaces any placeholders found in it with date, time or an incremental number. If an incremental number is specified, the function will ensure the filename is unique. The expanded filename will be stored
. The function uses
to store the partly-expanded filename.
this_ | The log object. |
References file_exists(), log::filename, log::filename_ex1, log::filename_ex2, startup::pos, and strftime_localtime().
Referenced by log_change(), and log_new().
|
static |
Rotates a log file.
This function rotates a log by stopping and immediately restarting it. Stopping flushes the buffer and closes the file; restarting determines the new file name and opens the file as needed (depending on the lazy member).
Depending on the file name format and how the function was called, a new log file will be created or the old log file will be reused (appended to or overwritten, depending on
): if the file name includes an incremental number, the new file will always have a different name. If a previous call to
returned true, the new file will also have a different name. In all other cases the new file will have the same name as the old one, causing the old file to be overwritten or appended to.
this_ | The log object. |
References expand_filenames(), log::lazy, log_close(), log_flush(), and log_open().
Referenced by log_write().
|
static |
Determines if the log must be rotated.
This function expands the date and time placeholders in
to determine if the resulting part of the filename has changed.
this_ | The log object. |
References log::filename, log::filename_ex1, and strftime_localtime().
Referenced by log_write().
|
static |
Closes a log file.
This function writes the trailer to a log file, flushes it and closes the log file for
.
this_ | The log object. |
References log_data::data, log::f, log_data::len, and log::trailer.
Referenced by log_change(), and log_destroy().
void log_destroy | ( | struct log * | this_ | ) |
Destroys a log object and frees up its memory.
this_ | The log object. |
References attr_list_free(), callback_destroy(), dbg, event_remove_timeout(), log_close(), log_flush(), lvl_debug, log::timer, and log::timer_callback.
Flushes the buffer of a log.
This function writes buffered log data to the log file associated with
and updates
with the current time.
If
is true, this function will open the file if needed, else the file must be opened with
prior to calling this function.
If the file is empty, the header will be written first, followed by the buffer data.
will be set to zero if header or data are written to the file.
this_ | The log object. |
flags | Flags to control behavior of the function: |
References log_data::data, log::data, dbg, log::empty, log::f, log::header, log::lazy, log_data::len, log_flag_keep_buffer, log_flag_keep_pointer, log_flag_truncate, log_open(), log_set_last_flush(), lvl_error, log_data::max_len, startup::pos, and log::trailer.
Referenced by log_change(), log_destroy(), log_timer(), and log_write().
|
static |
Determines if the maximum buffer size of a log has been exceeded.
This function examines the size of the data buffer to determine if it exceeds the maximum size specified in
and thus needs to be flushed.
this_ | The log object. |
References log::data, log::flush_size, and log_data::len.
Referenced by log_write().
int log_get_attr | ( | struct log * | this_, |
enum attr_type | type, | ||
struct attr * | attr, | ||
struct attr_iter * | iter | ||
) |
Gets an attribute.
this_ | The log object. |
attr_type | The attribute type to return |
attr | Points to a struct attr to store the attribute |
iter | An attribute iterator |
References attr_generic_get_attr(), and type.
Referenced by navit_add_log(), vehicle_add_log(), and vehicle_log_gpx().
void* log_get_buffer | ( | struct log * | this_, |
int * | len | ||
) |
Returns the data buffer of a log object and its length.
this_ | The log object. |
len | Points to an int which will receive the length of the buffer. This can be NULL, in which case no information on buffer length will be stored. |
References log_data::data, log::data, and log_data::len.
Referenced by vehicle_log_binfile().
Creates and initializes a new log object.
parent | The parent object. |
attrs | Points to an array of pointers to attributes for the new log object |
References attr_list_dup(), attr_search(), callback_cast, callback_new_1, data, dbg, event_add_timeout(), expand_filenames(), file_wordexp_destroy(), file_wordexp_get_array(), file_wordexp_get_count(), file_wordexp_new(), log::filename, filename, log::flush_size, log::flush_time, log::lazy, log_func, log_open(), log_set_last_flush(), log_timer(), lvl_debug, log::mkdir, navit_object_ref(), attr::num, log::overwrite, log::timer, log::timer_callback, and attr::u.
|
static |
Opens a log file.
This function opens the log file for
. The file name must be specified by
before this function is called.
specifies the behavior if the file exists: if true, an existing file will be overwritten, else it will be appended to.
If the directory specified in the filename does not exist and the
is true, it will be created.
After the function returns,
will contain the file handle (or NULL, if the operation failed) and
will indicate if the file is empty.
will be updated with the current time.
this_ | The log object. |
References log::empty, log::f, file_mkdir(), log::filename_ex2, log_set_last_flush(), log::mkdir, and log::overwrite.
Referenced by log_change(), log_flush(), and log_new().
void log_printf | ( | struct log * | this_, |
char * | fmt, | ||
... | |||
) |
Writes a formatted string to a log.
This function formats a string in a fashion similar to
and related functions and writes it to a log using
.
this_ | The log object. |
fmt | The format string. |
... | Additional arguments must be specified for each placeholder in the format string. |
References LOG_BUFFER_SIZE, log_write(), and size.
void log_set_header | ( | struct log * | this_, |
char * | data, | ||
int | len | ||
) |
Sets the header for a log file.
This function sets the header, which is to be inserted into any log file before the actual log data.
this_ | The log object. |
data | The header data. |
len | Size of the header data to be copied, in bytes. |
References log_data::data, data, log::header, log_data::len, and log_data::max_len.
Referenced by navit_add_log(), and vehicle_add_log().
|
static |
Sets the time at which the log buffer was last flushed.
This function sets
to current time.
this_ | The log object. |
Referenced by log_flush(), log_new(), and log_open().
void log_set_trailer | ( | struct log * | this_, |
char * | data, | ||
int | len | ||
) |
Sets the trailer for a log file.
This function sets the trailer, which is to be added to any log file after the actual log data.
this_ | The log object. |
data | The trailer data. |
len | Size of the trailer data to be copied, in bytes. |
References log_data::data, data, log_data::len, log_data::max_len, and log::trailer.
Referenced by vehicle_add_log().
|
static |
Determines if the flush interval of a log has elapsed and flushes the buffer if needed.
This function calculates the difference between current time and
. If it is greater than or equal to
, the buffer is flushed.
this_ | The log object. |
References dbg, log::flush_time, log_flush(), and lvl_debug.
Referenced by log_new().
Writes to a log.
This function appends data to a log. It rotates the log, if needed, before adding the new data. After adding, the log is flushed if the buffer exceeds its maximum size or if the
flag is set.
this_ | The log object. |
data | Points to a buffer containing the data to be appended. |
len | Length of the data to be appended, in bytes. |
flags | Flags to control behavior of the function: : discards any data in the buffer not yet written to the log file : forces a flush of the log after appending the data {code log_flag_keep_pointer}: ignored : ignored : ignored |
References log_data::data, log::data, data, dbg, log_data::len, log_change(), log_change_required(), log_flag_force_flush, log_flag_replace_buffer, log_flush(), log_flush_required(), lvl_debug, lvl_info, and log_data::max_len.
Referenced by log_printf(), navit_textfile_debug_log(), navit_textfile_debug_log_at(), vehicle_log_binfile(), vehicle_log_gpx(), vehicle_log_nmea(), and vehicle_log_textfile().
|
static |
Stores formatted time to a string.
This function obtains local system time, formats it as specified in
and stores it in buffer. Format strings follow the same syntax as those for
.
buffer | A preallocated buffer that will receive the formatted time |
size | Size of the buffer, in bytes |
fmt | The format string |
References size.
Referenced by expand_filenames(), and log_change_required().
struct object_func log_func |
Referenced by log_new(), and object_func_lookup().