Add uc_log_delete_destination.
Make a copy of ident for syslog destination
This commit is contained in:
+60
-3
@@ -17,7 +17,7 @@ struct uc_log_destination {
|
|||||||
union {
|
union {
|
||||||
//for syslog
|
//for syslog
|
||||||
struct {
|
struct {
|
||||||
const char *ident;
|
char *ident;
|
||||||
int facility;
|
int facility;
|
||||||
} syslog;
|
} syslog;
|
||||||
//for stderr and files
|
//for stderr and files
|
||||||
@@ -106,14 +106,22 @@ struct uc_log_destination *uc_log_new_stderr(int log_level, int log_location)
|
|||||||
struct uc_log_destination *uc_log_new_syslog(const char *ident, int facility, int log_level, int log_location)
|
struct uc_log_destination *uc_log_new_syslog(const char *ident, int facility, int log_level, int log_location)
|
||||||
{
|
{
|
||||||
struct uc_log_destination *dest = calloc(1, sizeof *dest);
|
struct uc_log_destination *dest = calloc(1, sizeof *dest);
|
||||||
|
char *id;
|
||||||
|
|
||||||
if(dest == NULL)
|
if(dest == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
//copy the ident - though it's unused inside the struct for now
|
||||||
|
id = strdup(ident);
|
||||||
|
if(id == NULL) {
|
||||||
|
free(dest);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
openlog(ident, LOG_NDELAY | LOG_PID, facility);
|
openlog(ident, LOG_NDELAY | LOG_PID, facility);
|
||||||
|
|
||||||
dest->dest_type = UC_LDEST_SYSLOG;
|
dest->dest_type = UC_LDEST_SYSLOG;
|
||||||
dest->type.syslog.ident = ident;
|
dest->type.syslog.ident = id;
|
||||||
dest->type.syslog.facility = facility;
|
dest->type.syslog.facility = facility;
|
||||||
dest->log_level = log_level;;
|
dest->log_level = log_level;;
|
||||||
dest->log_location = log_location;
|
dest->log_location = log_location;
|
||||||
@@ -172,11 +180,60 @@ void uc_log_add_destination(struct uc_log_destination *dest)
|
|||||||
pthread_mutex_unlock(&log_lock);
|
pthread_mutex_unlock(&log_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void uc_log_remove_dest_unlocked(struct uc_log_destination *dest)
|
||||||
|
{
|
||||||
|
struct uc_log_destination *it;
|
||||||
|
|
||||||
|
//check that it's not already removed
|
||||||
|
SLIST_FOREACH(it, &destinations, entry) {
|
||||||
|
if(it == dest);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(it != NULL) {
|
||||||
|
SLIST_REMOVE(&destinations, dest, uc_log_destination, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void uc_log_remove_destination(struct uc_log_destination *dest)
|
void uc_log_remove_destination(struct uc_log_destination *dest)
|
||||||
{
|
{
|
||||||
|
|
||||||
pthread_mutex_lock(&log_lock);
|
pthread_mutex_lock(&log_lock);
|
||||||
|
|
||||||
SLIST_REMOVE(&destinations, dest, uc_log_destination, entry);
|
uc_log_remove_dest_unlocked(dest);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&log_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uc_log_delete_destination(struct uc_log_destination *dest)
|
||||||
|
{
|
||||||
|
if(dest == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
pthread_mutex_lock(&log_lock);
|
||||||
|
|
||||||
|
//remove it
|
||||||
|
uc_log_remove_dest_unlocked(dest);
|
||||||
|
|
||||||
|
//clean up, according to the type
|
||||||
|
switch(dest->dest_type) {
|
||||||
|
case UC_LDEST_SYSLOG:
|
||||||
|
free(dest->type.syslog.ident);
|
||||||
|
closelog();
|
||||||
|
break;
|
||||||
|
case UC_LDEST_STDERR: //nothing to do
|
||||||
|
break;
|
||||||
|
case UC_LDEST_FILE:
|
||||||
|
if(dest->type.file.file != NULL) {
|
||||||
|
fclose(dest->type.file.file);
|
||||||
|
dest->type.file.file = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(dest->type.file.file_name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(dest);
|
||||||
|
|
||||||
pthread_mutex_unlock(&log_lock);
|
pthread_mutex_unlock(&log_lock);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,11 +111,25 @@ struct uc_log_destination *uc_log_new_file(const char *filename, int log_level,
|
|||||||
void uc_log_add_destination(struct uc_log_destination *dest);
|
void uc_log_add_destination(struct uc_log_destination *dest);
|
||||||
|
|
||||||
/** Remove a log destination.
|
/** Remove a log destination.
|
||||||
|
* Note that this does not free the destination, it can be readded later.
|
||||||
|
* For file destination, the log file is NOT closed.
|
||||||
*
|
*
|
||||||
* @param dest The uc_log_destination to remove.
|
* @param dest The uc_log_destination to remove.
|
||||||
*/
|
*/
|
||||||
void uc_log_remove_destination(struct uc_log_destination *dest);
|
void uc_log_remove_destination(struct uc_log_destination *dest);
|
||||||
|
|
||||||
|
/** Remove and free a destination.
|
||||||
|
* This frees the memory allocated to the destination. For
|
||||||
|
* UC_LDEST_FILE the log file is closed.
|
||||||
|
*
|
||||||
|
* If the destination is not already removed, it will be removed first,
|
||||||
|
* there's no need to call uc_log_remove_destination first.
|
||||||
|
* The destination cannot be used again after this function has been called.
|
||||||
|
*
|
||||||
|
* @param dest The destination to delete.
|
||||||
|
*/
|
||||||
|
void uc_log_delete_destination(struct uc_log_destination *dest);
|
||||||
|
|
||||||
/** Change the log level of a module.
|
/** Change the log level of a module.
|
||||||
*
|
*
|
||||||
* @param module The module to change, matched against the id member of a struct uc_log_module
|
* @param module The module to change, matched against the id member of a struct uc_log_module
|
||||||
|
|||||||
Reference in New Issue
Block a user