Added a ike_name logger option to prefix the IKE_SA name on each line

This commit is contained in:
Martin Willi
2010-08-25 09:55:37 +02:00
parent d9b85e28b9
commit 8427c78611
5 changed files with 69 additions and 19 deletions
+29 -6
View File
@@ -46,6 +46,11 @@ struct private_file_logger_t {
* strftime() format of time prefix, if any
*/
char *time_format;
/**
* Print the name/# of the IKE_SA?
*/
bool ike_name;
};
/**
@@ -56,7 +61,7 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
{
if (level <= this->levels[group])
{
char buffer[8192], timestr[128];
char buffer[8192], timestr[128], namestr[128] = "";
char *current = buffer, *next;
struct tm tm;
time_t t;
@@ -67,6 +72,23 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
localtime_r(&t, &tm);
strftime(timestr, sizeof(timestr), this->time_format, &tm);
}
if (this->ike_name && ike_sa)
{
if (ike_sa->get_peer_cfg(ike_sa))
{
snprintf(namestr, sizeof(namestr), " <%s|%d>",
ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
}
else
{
snprintf(namestr, sizeof(namestr), " <%d>",
ike_sa->get_unique_id(ike_sa));
}
}
else
{
namestr[0] = '\0';
}
/* write in memory buffer first */
vsnprintf(buffer, sizeof(buffer), format, args);
@@ -81,13 +103,13 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
}
if (this->time_format)
{
fprintf(this->out, "%s %.2d[%N] %s\n",
timestr, thread, debug_names, group, current);
fprintf(this->out, "%s %.2d[%N]%s %s\n",
timestr, thread, debug_names, group, namestr, current);
}
else
{
fprintf(this->out, "%.2d[%N] %s\n",
thread, debug_names, group, current);
fprintf(this->out, "%.2d[%N]%s %s\n",
thread, debug_names, group, namestr, current);
}
current = next;
}
@@ -129,7 +151,7 @@ static void destroy(private_file_logger_t *this)
/*
* Described in header.
*/
file_logger_t *file_logger_create(FILE *out, char *time_format)
file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name)
{
private_file_logger_t *this = malloc_thing(private_file_logger_t);
@@ -142,6 +164,7 @@ file_logger_t *file_logger_create(FILE *out, char *time_format)
/* private variables */
this->out = out;
this->time_format = time_format;
this->ike_name = ike_name;
set_level(this, DBG_ANY, LEVEL_SILENT);
return &this->public;
+2 -1
View File
@@ -54,8 +54,9 @@ struct file_logger_t {
*
* @param out FILE to write to
* @param time_format format of timestamp prefix, as in strftime()
* @param ike_name TRUE to prefix the name of the IKE_SA
* @return file_logger_t object
*/
file_logger_t *file_logger_create(FILE *out, char *time_format);
file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name);
#endif /** FILE_LOGGER_H_ @}*/
+24 -4
View File
@@ -41,6 +41,11 @@ struct private_sys_logger_t {
* Maximum level to log, for each group
*/
level_t levels[DBG_MAX];
/**
* Print the name/# of the IKE_SA?
*/
bool ike_name;
};
/**
@@ -51,12 +56,26 @@ static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
{
if (level <= this->levels[group])
{
char buffer[8192];
char buffer[8192], namestr[128] = "";
char *current = buffer, *next;
/* write in memory buffer first */
vsnprintf(buffer, sizeof(buffer), format, args);
if (this->ike_name && ike_sa)
{
if (ike_sa->get_peer_cfg(ike_sa))
{
snprintf(namestr, sizeof(namestr), " <%s|%d>",
ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
}
else
{
snprintf(namestr, sizeof(namestr), " <%d>",
ike_sa->get_unique_id(ike_sa));
}
}
/* do a syslog with every line */
while (current)
{
@@ -65,8 +84,8 @@ static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
{
*(next++) = '\0';
}
syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
thread, debug_names, group, current);
syslog(this->facility|LOG_INFO, "%.2d[%N]%s %s\n",
thread, debug_names, group, namestr, current);
current = next;
}
}
@@ -104,7 +123,7 @@ static void destroy(private_sys_logger_t *this)
/*
* Described in header.
*/
sys_logger_t *sys_logger_create(int facility)
sys_logger_t *sys_logger_create(int facility, bool ike_name)
{
private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
@@ -116,6 +135,7 @@ sys_logger_t *sys_logger_create(int facility)
/* private variables */
this->facility = facility;
this->ike_name = ike_name;
set_level(this, DBG_ANY, LEVEL_SILENT);
return &this->public;
+2 -1
View File
@@ -53,8 +53,9 @@ struct sys_logger_t {
* Constructor to create a sys_logger_t object.
*
* @param facility syslog facility to use
* @param ike_name TRUE to prefix the name of the IKE_SA
* @return sys_logger_t object
*/
sys_logger_t *sys_logger_create(int facility);
sys_logger_t *sys_logger_create(int facility, bool ike_name);
#endif /** SYS_LOGGER_H_ @}*/