Rewrite logfile word wrapping code to be more straight-forward
and actually wrap at the correct place.
This commit is contained in:
@@ -176,14 +176,16 @@ do_syslog(int pri, char *msg)
|
|||||||
#endif /* HAVE_SETLOCALE */
|
#endif /* HAVE_SETLOCALE */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define LOG_INDENT " "
|
||||||
|
|
||||||
static void
|
static void
|
||||||
do_logfile(char *msg)
|
do_logfile(char *msg)
|
||||||
{
|
{
|
||||||
char *full_line;
|
char *full_line, *beg, *end, *indent = "";
|
||||||
char *beg, *oldend, *end;
|
size_t len, maxlen;
|
||||||
FILE *fp;
|
|
||||||
mode_t oldmask;
|
mode_t oldmask;
|
||||||
size_t maxlen;
|
time_t now;
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
oldmask = umask(077);
|
oldmask = umask(077);
|
||||||
maxlen = def_loglinelen > 0 ? def_loglinelen : 0;
|
maxlen = def_loglinelen > 0 ? def_loglinelen : 0;
|
||||||
@@ -196,8 +198,6 @@ do_logfile(char *msg)
|
|||||||
send_mail(_("unable to lock log file: %s: %s"),
|
send_mail(_("unable to lock log file: %s: %s"),
|
||||||
def_logfile, strerror(errno));
|
def_logfile, strerror(errno));
|
||||||
} else {
|
} else {
|
||||||
time_t now;
|
|
||||||
|
|
||||||
#ifdef HAVE_SETLOCALE
|
#ifdef HAVE_SETLOCALE
|
||||||
const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
|
const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
|
||||||
if (!setlocale(LC_ALL, def_sudoers_locale))
|
if (!setlocale(LC_ALL, def_sudoers_locale))
|
||||||
@@ -205,7 +205,7 @@ do_logfile(char *msg)
|
|||||||
#endif /* HAVE_SETLOCALE */
|
#endif /* HAVE_SETLOCALE */
|
||||||
|
|
||||||
now = time(NULL);
|
now = time(NULL);
|
||||||
if (def_loglinelen == 0) {
|
if (def_loglinelen < sizeof(LOG_INDENT)) {
|
||||||
/* Don't pretty-print long log file lines (hard to grep) */
|
/* Don't pretty-print long log file lines (hard to grep) */
|
||||||
if (def_log_host)
|
if (def_log_host)
|
||||||
(void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
|
(void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
|
||||||
@@ -215,59 +215,39 @@ do_logfile(char *msg)
|
|||||||
get_timestr(now, def_log_year), user_name, msg);
|
get_timestr(now, def_log_year), user_name, msg);
|
||||||
} else {
|
} else {
|
||||||
if (def_log_host)
|
if (def_log_host)
|
||||||
easprintf(&full_line, "%s : %s : HOST=%s : %s",
|
len = easprintf(&full_line, "%s : %s : HOST=%s : %s",
|
||||||
get_timestr(now, def_log_year), user_name, user_shost, msg);
|
get_timestr(now, def_log_year), user_name, user_shost, msg);
|
||||||
else
|
else
|
||||||
easprintf(&full_line, "%s : %s : %s",
|
len = easprintf(&full_line, "%s : %s : %s",
|
||||||
get_timestr(now, def_log_year), user_name, msg);
|
get_timestr(now, def_log_year), user_name, msg);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print out full_line with word wrap
|
* Print out full_line with word wrap around maxlen characters.
|
||||||
*/
|
*/
|
||||||
beg = end = full_line;
|
beg = full_line;
|
||||||
while (beg) {
|
while (len > maxlen) {
|
||||||
oldend = end;
|
end = beg + maxlen + 1;
|
||||||
end = strchr(oldend, ' ');
|
while (end != beg && *end != ' ')
|
||||||
|
end--;
|
||||||
if (maxlen > 0 && end) {
|
if (beg == end) {
|
||||||
*end = '\0';
|
/* Unable to find word break within maxlen, look beyond. */
|
||||||
if (strlen(beg) > maxlen) {
|
end = strchr(beg + maxlen + 1, ' ');
|
||||||
/* too far, need to back up & print the line */
|
if (end == NULL)
|
||||||
|
break; /* no word break */
|
||||||
if (beg == (char *)full_line)
|
|
||||||
maxlen -= 4; /* don't indent first line */
|
|
||||||
|
|
||||||
*end = ' ';
|
|
||||||
if (oldend != beg) {
|
|
||||||
/* rewind & print */
|
|
||||||
end = oldend-1;
|
|
||||||
while (*end == ' ')
|
|
||||||
--end;
|
|
||||||
*(++end) = '\0';
|
|
||||||
(void) fprintf(fp, "%s\n ", beg);
|
|
||||||
*end = ' ';
|
|
||||||
} else {
|
|
||||||
(void) fprintf(fp, "%s\n ", beg);
|
|
||||||
}
|
}
|
||||||
|
fprintf(fp, "%s%.*s\n", indent, (int)(end - beg), beg);
|
||||||
/* reset beg to point to the start of the new substr */
|
while (*end == ' ')
|
||||||
|
end++;
|
||||||
|
len -= (end - beg);
|
||||||
beg = end;
|
beg = end;
|
||||||
while (*beg == ' ')
|
if (indent[0] == '\0') {
|
||||||
++beg;
|
indent = LOG_INDENT;
|
||||||
} else {
|
maxlen -= sizeof(LOG_INDENT) - 1;
|
||||||
/* we still have room */
|
|
||||||
*end = ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
/* remove leading whitespace */
|
|
||||||
while (*end == ' ')
|
|
||||||
++end;
|
|
||||||
} else {
|
|
||||||
/* final line */
|
|
||||||
(void) fprintf(fp, "%s\n", beg);
|
|
||||||
beg = NULL; /* exit condition */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Print remainder, if any. */
|
||||||
|
if (len)
|
||||||
|
fprintf(fp, "%s%s\n", indent, beg);
|
||||||
efree(full_line);
|
efree(full_line);
|
||||||
}
|
}
|
||||||
(void) fflush(fp);
|
(void) fflush(fp);
|
||||||
|
Reference in New Issue
Block a user