No need to handle impossible things.
The string given to logmanager_write_log cannot be NULL. The id given to logmanager_write_log cannot be invalid. Consequently we need not handle those possibilities.
This commit is contained in:
@ -627,194 +627,176 @@ static int logmanager_write_log(logfile_id_t id,
|
|||||||
size_t timestamp_len;
|
size_t timestamp_len;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
assert(str);
|
||||||
|
assert(logmanager_is_valid_id(id));
|
||||||
CHK_LOGMANAGER(lm);
|
CHK_LOGMANAGER(lm);
|
||||||
|
|
||||||
if (!logmanager_is_valid_id(id))
|
|
||||||
{
|
|
||||||
err = -1;
|
|
||||||
ss_dassert(false);
|
|
||||||
goto return_err;
|
|
||||||
}
|
|
||||||
// All messages are now logged to the error log file.
|
// All messages are now logged to the error log file.
|
||||||
lf = &lm->lm_logfile;
|
lf = &lm->lm_logfile;
|
||||||
CHK_LOGFILE(lf);
|
CHK_LOGFILE(lf);
|
||||||
|
|
||||||
|
/** Length of string that will be written, limited by bufsize */
|
||||||
|
size_t safe_str_len;
|
||||||
|
/** Length of session id */
|
||||||
|
size_t sesid_str_len;
|
||||||
|
size_t cmplen = 0;
|
||||||
/**
|
/**
|
||||||
* When string pointer is NULL, operation is flush.
|
* 2 braces, 2 spaces and terminating char
|
||||||
|
* If session id is stored to tls_log_info structure, allocate
|
||||||
|
* room for session id too.
|
||||||
*/
|
*/
|
||||||
if (str == NULL)
|
if (id == LOGFILE_TRACE && tls_log_info.li_sesid != 0)
|
||||||
{
|
{
|
||||||
if (flush)
|
sesid_str_len = 5 * sizeof(char) + get_decimal_len(tls_log_info.li_sesid);
|
||||||
{
|
|
||||||
logfile_flush(lf); /*< wakes up file writer */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/** Length of string that will be written, limited by bufsize */
|
sesid_str_len = 0;
|
||||||
size_t safe_str_len;
|
}
|
||||||
/** Length of session id */
|
if (log_config.highprec)
|
||||||
size_t sesid_str_len;
|
{
|
||||||
size_t cmplen = 0;
|
timestamp_len = get_timestamp_len_hp();
|
||||||
/**
|
}
|
||||||
* 2 braces, 2 spaces and terminating char
|
else
|
||||||
* If session id is stored to tls_log_info structure, allocate
|
{
|
||||||
* room for session id too.
|
timestamp_len = get_timestamp_len();
|
||||||
*/
|
}
|
||||||
if (id == LOGFILE_TRACE && tls_log_info.li_sesid != 0)
|
cmplen = sesid_str_len > 0 ? sesid_str_len - sizeof(char) : 0;
|
||||||
{
|
|
||||||
sesid_str_len = 5 * sizeof(char) + get_decimal_len(tls_log_info.li_sesid);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sesid_str_len = 0;
|
|
||||||
}
|
|
||||||
if (log_config.highprec)
|
|
||||||
{
|
|
||||||
timestamp_len = get_timestamp_len_hp();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
timestamp_len = get_timestamp_len();
|
|
||||||
}
|
|
||||||
cmplen = sesid_str_len > 0 ? sesid_str_len - sizeof(char) : 0;
|
|
||||||
|
|
||||||
bool overflow = false;
|
bool overflow = false;
|
||||||
/** Find out how much can be safely written with current block size */
|
/** Find out how much can be safely written with current block size */
|
||||||
if (timestamp_len - sizeof(char) + cmplen + str_len > lf->lf_buf_size)
|
if (timestamp_len - sizeof(char) + cmplen + str_len > lf->lf_buf_size)
|
||||||
{
|
{
|
||||||
safe_str_len = lf->lf_buf_size;
|
safe_str_len = lf->lf_buf_size;
|
||||||
overflow = true;
|
overflow = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
safe_str_len = timestamp_len - sizeof(char) + cmplen + str_len;
|
safe_str_len = timestamp_len - sizeof(char) + cmplen + str_len;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Seek write position and register to block buffer.
|
* Seek write position and register to block buffer.
|
||||||
* Then print formatted string to write position.
|
* Then print formatted string to write position.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined (SS_LOG_DEBUG)
|
#if defined (SS_LOG_DEBUG)
|
||||||
|
{
|
||||||
|
char *copy, *tok;
|
||||||
|
int tokval;
|
||||||
|
|
||||||
|
simple_mutex_lock(&msg_mutex, true);
|
||||||
|
copy = strdup(str);
|
||||||
|
tok = strtok(copy, "|");
|
||||||
|
tok = strtok(NULL, "|");
|
||||||
|
|
||||||
|
if (strstr(str, "message|") && tok)
|
||||||
{
|
{
|
||||||
char *copy, *tok;
|
tokval = atoi(tok);
|
||||||
int tokval;
|
|
||||||
|
|
||||||
simple_mutex_lock(&msg_mutex, true);
|
if (prevval > 0)
|
||||||
copy = strdup(str);
|
|
||||||
tok = strtok(copy, "|");
|
|
||||||
tok = strtok(NULL, "|");
|
|
||||||
|
|
||||||
if (strstr(str, "message|") && tok)
|
|
||||||
{
|
{
|
||||||
tokval = atoi(tok);
|
ss_dassert(tokval == (prevval + 1));
|
||||||
|
|
||||||
if (prevval > 0)
|
|
||||||
{
|
|
||||||
ss_dassert(tokval == (prevval + 1));
|
|
||||||
}
|
|
||||||
prevval = tokval;
|
|
||||||
}
|
}
|
||||||
free(copy);
|
prevval = tokval;
|
||||||
simple_mutex_unlock(&msg_mutex);
|
|
||||||
}
|
}
|
||||||
|
free(copy);
|
||||||
|
simple_mutex_unlock(&msg_mutex);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
/** Book space for log string from buffer */
|
/** Book space for log string from buffer */
|
||||||
if (log_config.do_maxscalelog)
|
if (log_config.do_maxscalelog)
|
||||||
{
|
{
|
||||||
// All messages are now logged to the error log file.
|
// All messages are now logged to the error log file.
|
||||||
wp = blockbuf_get_writepos(&bb, safe_str_len, flush);
|
wp = blockbuf_get_writepos(&bb, safe_str_len, flush);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
wp = (char*)malloc(sizeof(char) * (timestamp_len - sizeof(char) + cmplen + str_len + 1));
|
wp = (char*)malloc(sizeof(char) * (timestamp_len - sizeof(char) + cmplen + str_len + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wp == NULL)
|
if (wp == NULL)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined (SS_LOG_DEBUG)
|
#if defined (SS_LOG_DEBUG)
|
||||||
{
|
{
|
||||||
sprintf(wp, "[msg:%d]", atomic_add(&write_index, 1));
|
sprintf(wp, "[msg:%d]", atomic_add(&write_index, 1));
|
||||||
safe_str_len -= strlen(wp);
|
safe_str_len -= strlen(wp);
|
||||||
wp += strlen(wp);
|
wp += strlen(wp);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
/**
|
||||||
|
* Write timestamp with at most <timestamp_len> characters
|
||||||
|
* to wp.
|
||||||
|
* Returned timestamp_len doesn't include terminating null.
|
||||||
|
*/
|
||||||
|
if (log_config.highprec)
|
||||||
|
{
|
||||||
|
timestamp_len = snprint_timestamp_hp(wp, timestamp_len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
timestamp_len = snprint_timestamp(wp, timestamp_len);
|
||||||
|
}
|
||||||
|
if (sesid_str_len != 0)
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Write timestamp with at most <timestamp_len> characters
|
* Write session id
|
||||||
* to wp.
|
|
||||||
* Returned timestamp_len doesn't include terminating null.
|
|
||||||
*/
|
*/
|
||||||
if (log_config.highprec)
|
snprintf(wp + timestamp_len, sesid_str_len, "[%lu] ", tls_log_info.li_sesid);
|
||||||
{
|
sesid_str_len -= 1; /*< don't calculate terminating char anymore */
|
||||||
timestamp_len = snprint_timestamp_hp(wp, timestamp_len);
|
}
|
||||||
}
|
/**
|
||||||
else
|
* Write next string to overwrite terminating null character
|
||||||
{
|
* of the timestamp string.
|
||||||
timestamp_len = snprint_timestamp(wp, timestamp_len);
|
*/
|
||||||
}
|
snprintf(wp + timestamp_len + sesid_str_len,
|
||||||
if (sesid_str_len != 0)
|
safe_str_len-timestamp_len-sesid_str_len,
|
||||||
{
|
"%s",
|
||||||
/**
|
str);
|
||||||
* Write session id
|
|
||||||
*/
|
|
||||||
snprintf(wp + timestamp_len, sesid_str_len, "[%lu] ", tls_log_info.li_sesid);
|
|
||||||
sesid_str_len -= 1; /*< don't calculate terminating char anymore */
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Write next string to overwrite terminating null character
|
|
||||||
* of the timestamp string.
|
|
||||||
*/
|
|
||||||
snprintf(wp + timestamp_len + sesid_str_len,
|
|
||||||
safe_str_len-timestamp_len-sesid_str_len,
|
|
||||||
"%s",
|
|
||||||
str);
|
|
||||||
|
|
||||||
/** Add an ellipsis to an overflowing message to signal truncation. */
|
/** Add an ellipsis to an overflowing message to signal truncation. */
|
||||||
if (overflow && safe_str_len > 4)
|
if (overflow && safe_str_len > 4)
|
||||||
{
|
{
|
||||||
memset(wp + safe_str_len - 4, '.', 3);
|
memset(wp + safe_str_len - 4, '.', 3);
|
||||||
}
|
}
|
||||||
/** write to syslog */
|
/** write to syslog */
|
||||||
if (log_config.do_syslog)
|
if (log_config.do_syslog)
|
||||||
{
|
{
|
||||||
// Strip away the timestamp and the prefix (e.g. "[Error]: ").
|
// Strip away the timestamp and the prefix (e.g. "[Error]: ").
|
||||||
const char *message = wp + timestamp_len + prefix_len;
|
const char *message = wp + timestamp_len + prefix_len;
|
||||||
|
|
||||||
switch (id)
|
switch (id)
|
||||||
{
|
|
||||||
case LOGFILE_ERROR:
|
|
||||||
syslog(LOG_ERR, "%s", message);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LOGFILE_MESSAGE:
|
|
||||||
syslog(LOG_NOTICE, "%s", message);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/** remove double line feed */
|
|
||||||
if (wp[safe_str_len - 2] == '\n')
|
|
||||||
{
|
{
|
||||||
wp[safe_str_len - 2] = ' ';
|
case LOGFILE_ERROR:
|
||||||
}
|
syslog(LOG_ERR, "%s", message);
|
||||||
wp[safe_str_len - 1] = '\n';
|
break;
|
||||||
|
|
||||||
if (log_config.do_maxscalelog)
|
case LOGFILE_MESSAGE:
|
||||||
{
|
syslog(LOG_NOTICE, "%s", message);
|
||||||
blockbuf_unregister(bb);
|
break;
|
||||||
}
|
|
||||||
else
|
default:
|
||||||
{
|
break;
|
||||||
free(wp);
|
}
|
||||||
}
|
}
|
||||||
} /* if (str == NULL) */
|
/** remove double line feed */
|
||||||
|
if (wp[safe_str_len - 2] == '\n')
|
||||||
|
{
|
||||||
|
wp[safe_str_len - 2] = ' ';
|
||||||
|
}
|
||||||
|
wp[safe_str_len - 1] = '\n';
|
||||||
|
|
||||||
|
if (log_config.do_maxscalelog)
|
||||||
|
{
|
||||||
|
blockbuf_unregister(bb);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
free(wp);
|
||||||
|
}
|
||||||
|
|
||||||
return_err:
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user