syslogd: avoid excessive tine() system calls

function                                             old     new   delta
timestamp_and_log_internal                             -      24     +24
log_locally                                          741     744      +3
timestamp_and_log                                    313     314      +1
syslogd_main                                         904     897      -7
quit_signal                                          101      94      -7
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/2 up/down: 28/-14)             Total: 14 bytes
This commit is contained in:
Denis Vlasenko 2008-01-03 12:13:42 +00:00
parent 4f2e8bc765
commit 4dada747e5

View File

@ -285,7 +285,7 @@ void log_to_shmem(const char *msg);
/* Print a message to the log file. */ /* Print a message to the log file. */
static void log_locally(char *msg) static void log_locally(time_t now, char *msg)
{ {
struct flock fl; struct flock fl;
int len = strlen(msg); int len = strlen(msg);
@ -297,10 +297,10 @@ static void log_locally(char *msg)
} }
#endif #endif
if (G.logFD >= 0) { if (G.logFD >= 0) {
time_t cur; if (!now)
time(&cur); now = time(NULL);
if (G.last_log_time != cur) { if (G.last_log_time != now) {
G.last_log_time = cur; /* reopen log file every second */ G.last_log_time = now; /* reopen log file every second */
close(G.logFD); close(G.logFD);
goto reopen; goto reopen;
} }
@ -397,23 +397,20 @@ static void parse_fac_prio_20(int pri, char *res20)
static void timestamp_and_log(int pri, char *msg, int len) static void timestamp_and_log(int pri, char *msg, int len)
{ {
char *timestamp; char *timestamp;
time_t now;
if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
return;
if (len < 16 || msg[3] != ' ' || msg[6] != ' ' if (len < 16 || msg[3] != ' ' || msg[6] != ' '
|| msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
) { ) {
time_t now;
time(&now); time(&now);
timestamp = ctime(&now) + 4; timestamp = ctime(&now) + 4; /* skip day of week */
} else { } else {
now = 0;
timestamp = msg; timestamp = msg;
msg += 16; msg += 16;
} }
timestamp[15] = '\0'; timestamp[15] = '\0';
/* Log message locally (to file or shared mem) */
if (option_mask32 & OPT_small) if (option_mask32 & OPT_small)
sprintf(G.printbuf, "%s %s\n", timestamp, msg); sprintf(G.printbuf, "%s %s\n", timestamp, msg);
else { else {
@ -421,7 +418,16 @@ static void timestamp_and_log(int pri, char *msg, int len)
parse_fac_prio_20(pri, res); parse_fac_prio_20(pri, res);
sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg);
} }
log_locally(G.printbuf);
/* Log message locally (to file or shared mem) */
log_locally(now, G.printbuf);
}
static void timestamp_and_log_internal(const char *msg)
{
if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
return;
timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
} }
static void split_escape_and_log(char *tmpbuf, int len) static void split_escape_and_log(char *tmpbuf, int len)
@ -462,7 +468,7 @@ static void split_escape_and_log(char *tmpbuf, int len)
static void quit_signal(int sig) static void quit_signal(int sig)
{ {
timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"syslogd exiting", 0); timestamp_and_log_internal("syslogd exiting");
puts("syslogd exiting"); puts("syslogd exiting");
if (ENABLE_FEATURE_IPC_SYSLOG) if (ENABLE_FEATURE_IPC_SYSLOG)
ipcsyslog_cleanup(); ipcsyslog_cleanup();
@ -473,7 +479,7 @@ static void quit_signal(int sig)
static void do_mark(int sig) static void do_mark(int sig)
{ {
if (G.markInterval) { if (G.markInterval) {
timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0); timestamp_and_log_internal("-- MARK --");
alarm(G.markInterval); alarm(G.markInterval);
} }
} }
@ -546,8 +552,7 @@ static void do_syslogd(void)
ipcsyslog_init(); ipcsyslog_init();
} }
timestamp_and_log(LOG_SYSLOG | LOG_INFO, timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
(char*)"syslogd started: BusyBox v" BB_VER, 0);
for (;;) { for (;;) {
size_t sz; size_t sz;