improved the file path management in the logging function

This commit is contained in:
hyung-hwan 2017-09-19 14:08:46 +00:00
parent db421d4208
commit 4e097466ae
2 changed files with 64 additions and 16 deletions

View File

@ -210,7 +210,8 @@ struct qse_log_t
struct struct
{ {
qse_char_t path[/*QSE_PATH_MAX + 1*/2048]; /* TODO: use the defined macro for sizing */ qse_char_t pathbuf[128]; /* static path buffer */
qse_char_t* path;
qse_sio_t* sio; qse_sio_t* sio;
} file; } file;
@ -282,12 +283,18 @@ QSE_EXPORT void qse_log_setident (
const qse_char_t* ident const qse_char_t* ident
); );
QSE_EXPORT void qse_log_settarget ( /**
* \return 0 on success, -1 on failure
*/
QSE_EXPORT int qse_log_settarget (
qse_log_t* log, qse_log_t* log,
int flags, int flags,
const qse_log_target_t* target const qse_log_target_t* target
); );
/**
* \return an integer bitwise-ORed of the target bits.
*/
QSE_EXPORT int qse_log_gettarget ( QSE_EXPORT int qse_log_gettarget (
qse_log_t* log, qse_log_t* log,
qse_log_target_t* target qse_log_target_t* target

View File

@ -228,23 +228,37 @@ int qse_log_init (qse_log_t* log, qse_mmgr_t* mmgr, const qse_char_t* ident, int
log->flags = potflags & (QSE_LOG_MASK_PRIORITY | QSE_LOG_MASK_OPTION); log->flags = potflags & (QSE_LOG_MASK_PRIORITY | QSE_LOG_MASK_OPTION);
log->t.syslog_remote.sock = -1; log->t.syslog_remote.sock = -1;
if (potflags & QSE_LOG_FILE) if (potflags & QSE_LOG_FILE) log->flags |= QSE_LOG_FILE;
if (target->file)
{ {
qse_strxcpy (log->t.file.path, QSE_COUNTOF(log->t.file.path), target->file); if (qse_strlen(target->file) >= QSE_COUNTOF(log->t.file.pathbuf))
log->flags |= QSE_LOG_FILE; {
log->t.file.path = qse_strdup (target->file, log->mmgr);
if (!log->t.file.path) return -1;
}
else
{
qse_strxcpy (log->t.file.pathbuf, QSE_COUNTOF(log->t.file.pathbuf), target->file);
log->t.file.path = log->t.file.pathbuf;
}
} }
if (potflags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE; if (potflags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE;
if (potflags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG; if (potflags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG;
if (potflags & QSE_LOG_SYSLOG_REMOTE) if (potflags & QSE_LOG_SYSLOG_REMOTE) log->flags |= QSE_LOG_SYSLOG_REMOTE;
{ if (qse_skadfamily(&target->syslog_remote) >= 0) log->t.syslog_remote.addr = target->syslog_remote;
/* flags_arg should be qse_skad_t* */
log->t.syslog_remote.addr = target->syslog_remote;
log->flags |= QSE_LOG_SYSLOG_REMOTE;
}
if (ident) qse_strxcpy (log->ident, QSE_COUNTOF(log->ident), ident); if (ident) qse_strxcpy (log->ident, QSE_COUNTOF(log->ident), ident);
if (qse_mtx_init(&log->mtx, mmgr) <= -1) return -1; if (qse_mtx_init(&log->mtx, mmgr) <= -1)
{
if (log->t.file.path && log->t.file.path != log->t.file.pathbuf)
{
QSE_MMGR_FREE (mmgr, log->t.file.path);
log->t.file.path = QSE_NULL;
}
return -1;
}
log->active_priority_bits = get_active_priority_bits(log->flags); log->active_priority_bits = get_active_priority_bits(log->flags);
@ -252,7 +266,6 @@ int qse_log_init (qse_log_t* log, qse_mmgr_t* mmgr, const qse_char_t* ident, int
/* TODO: windows event logging */ /* TODO: windows event logging */
#else #else
log->syslog_facility = QSE_LOG_USER; log->syslog_facility = QSE_LOG_USER;
#endif #endif
return 0; return 0;
@ -272,6 +285,12 @@ void qse_log_fini (qse_log_t* log)
log->t.console.sio = QSE_NULL; log->t.console.sio = QSE_NULL;
} }
if (log->t.file.path && log->t.file.path != log->t.file.pathbuf)
{
QSE_MMGR_FREE (log->mmgr, log->t.file.path);
log->t.file.path = QSE_NULL;
}
if (log->t.file.sio) if (log->t.file.sio)
{ {
qse_sio_close (log->t.file.sio); qse_sio_close (log->t.file.sio);
@ -323,8 +342,15 @@ void qse_log_setident (qse_log_t* log, const qse_char_t* ident)
} }
} }
void qse_log_settarget (qse_log_t* log, int flags, const qse_log_target_t* target) int qse_log_settarget (qse_log_t* log, int flags, const qse_log_target_t* target)
{ {
qse_char_t* target_file = QSE_NULL;
if (target->file && qse_strlen(target->file) >= QSE_COUNTOF(log->t.file.pathbuf))
{
target_file = qse_strdup (target->file, log->mmgr);
if (!target_file) return -1;
}
if (log->t.syslog_remote.sock >= 0) if (log->t.syslog_remote.sock >= 0)
{ {
QSE_CLOSE (log->t.syslog_remote.sock); QSE_CLOSE (log->t.syslog_remote.sock);
@ -355,13 +381,28 @@ void qse_log_settarget (qse_log_t* log, int flags, const qse_log_target_t* targe
/* If you just want to set the target file path without enable QSE_LOG_FILE, /* If you just want to set the target file path without enable QSE_LOG_FILE,
* just set target->file without QSE_LOG_FILE in the flags. * just set target->file without QSE_LOG_FILE in the flags.
* later, you can call this function with QSE_LOG_FILE set but with target->file or QSE_NULL */ * later, you can call this function with QSE_LOG_FILE set but with target->file or QSE_NULL */
if (target->file) qse_strxcpy (log->t.file.path, QSE_COUNTOF(log->t.file.path), target->file); if (target->file)
{
if (log->t.file.path && log->t.file.path != log->t.file.pathbuf) QSE_MMGR_FREE (log->mmgr, log->t.file.path);
if (target_file)
{
log->t.file.path = target_file;
}
else
{
qse_strxcpy (log->t.file.pathbuf, QSE_COUNTOF(log->t.file.pathbuf), target->file);
log->t.file.path = log->t.file.pathbuf;
}
}
if (flags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE; if (flags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE;
if (flags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG; if (flags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG;
if (flags & QSE_LOG_SYSLOG_REMOTE) log->flags |= QSE_LOG_SYSLOG_REMOTE; if (flags & QSE_LOG_SYSLOG_REMOTE) log->flags |= QSE_LOG_SYSLOG_REMOTE;
if (qse_skadfamily(&target->syslog_remote) != -1) log->t.syslog_remote.addr = target->syslog_remote; if (qse_skadfamily(&target->syslog_remote) >= 0) log->t.syslog_remote.addr = target->syslog_remote;
return 0;
} }
int qse_log_gettarget (qse_log_t* log, qse_log_target_t* target) int qse_log_gettarget (qse_log_t* log, qse_log_target_t* target)