From: Ildar Muslukhov <ildarm@google.com>
To: trinity@vger.kernel.org
Cc: davej@redhat.com, Ildar Muslukhov <ildarm@google.com>
Subject: [PATCH 3/6] refactored output function
Date: Tue, 8 Oct 2013 14:26:52 -0700 [thread overview]
Message-ID: <1381267615-9826-3-git-send-email-ildarm@google.com> (raw)
In-Reply-To: <1381267615-9826-1-git-send-email-ildarm@google.com>
Signed-off-by: Ildar Muslukhov <ildarm@google.com>
---
log.c | 77 ++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 48 insertions(+), 29 deletions(-)
diff --git a/log.c b/log.c
index 1298038..518dd81 100644
--- a/log.c
+++ b/log.c
@@ -10,7 +10,10 @@
#include "pids.h"
#include "log.h"
+#define BUFSIZE 1024
+
FILE *mainlogfile;
+bool logfiles_opened = FALSE;
void open_logfiles(void)
{
@@ -36,6 +39,7 @@ void open_logfiles(void)
}
}
free(logfilename);
+ logfiles_opened = TRUE;
}
void close_logfiles(void)
@@ -124,6 +128,25 @@ void synclogs(void)
fsync(fileno(mainlogfile));
}
+static FILE *robust_find_logfile_handle(void)
+{
+ unsigned int j;
+ FILE *handle = NULL;
+
+ if ((logging == TRUE) && (logfiles_opened)) {
+ handle = find_logfile_handle();
+ if (!handle) {
+ outputerr("## child logfile handle was null logging to main!\n");
+ (void)fflush(stdout);
+ for_each_pidslot(j)
+ shm->logfiles[j] = mainlogfile;
+ sleep(5);
+ handle = find_logfile_handle();
+ }
+ }
+ return handle;
+}
+
/*
* level defines whether it gets displayed to the screen with printf.
* (it always logs).
@@ -139,8 +162,8 @@ void output(unsigned char level, const char *fmt, ...)
FILE *handle;
unsigned int len, i, j;
pid_t pid;
- char outputbuf[1024];
- char monobuf[1024];
+ char outputbuf[BUFSIZE];
+ char monobuf[BUFSIZE];
char *prefix = NULL;
char watchdog_prefix[]="[watchdog]";
char init_prefix[]="[init]";
@@ -151,6 +174,7 @@ void output(unsigned char level, const char *fmt, ...)
if (logging == FALSE && level >= quiet_level)
return;
+ /* prefix preparation */
pid = getpid();
if (pid == watchdog_pid)
prefix = watchdog_prefix;
@@ -167,6 +191,7 @@ void output(unsigned char level, const char *fmt, ...)
prefix = child_prefix;
}
+ /* formatting output */
va_start(args, fmt);
n = vsnprintf(outputbuf, sizeof(outputbuf), fmt, args);
va_end(args);
@@ -176,49 +201,43 @@ void output(unsigned char level, const char *fmt, ...)
exit(EXIT_FAILURE);
}
+ /* stdout output if needed */
if (quiet_level > level) {
printf("%s %s", prefix, outputbuf);
(void)fflush(stdout);
}
+ /* go on with file logs only if enabled */
if (logging == FALSE)
return;
- handle = find_logfile_handle();
- if (!handle) {
- printf("## child logfile handle was null logging to main!\n");
- (void)fflush(stdout);
- for_each_pidslot(j)
- shm->logfiles[j] = mainlogfile;
- sleep(5);
+ handle = robust_find_logfile_handle();
+ if (!handle)
return;
- }
/* If we've specified monochrome, we can just dump the buffer into
* the logfile as is, because there shouldn't be any ANSI codes
* in the buffer to be stripped out. */
- if (monochrome == TRUE) {
- fprintf(handle, "%s %s", prefix, outputbuf);
- (void)fflush(handle);
- return;
- }
-
- /* copy buffer, sans ANSI codes */
- len = strlen(outputbuf);
- for (i = 0, j = 0; i < len; i++) {
- if (outputbuf[i] == '^[') {
- if (outputbuf[i + 2] == '1')
- i += 6; // ANSI_COLOUR
- else
- i += 3; // ANSI_RESET
- } else {
- monobuf[j] = outputbuf[i];
- j++;
+ if (monochrome == FALSE) {
+ /* copy buffer, sans ANSI codes */
+ len = strlen(outputbuf);
+ for (i = 0, j = 0; (i < len) && (i + 2 < BUFSIZE) && (j < BUFSIZE); i++) {
+ if (outputbuf[i] == '^[') {
+ if (outputbuf[i + 2] == '1')
+ i += 6; // ANSI_COLOUR
+ else
+ i += 3; // ANSI_RESET
+ } else {
+ monobuf[j] = outputbuf[i];
+ j++;
+ }
}
+ monobuf[j] = '\0';
+ fprintf(handle, "%s %s", prefix, monobuf);
+ } else {
+ fprintf(handle, "%s %s", prefix, outputbuf);
}
- monobuf[j] = '\0';
- fprintf(handle, "%s %s", prefix, monobuf);
(void)fflush(handle);
}
--
1.8.4
next prev parent reply other threads:[~2013-10-08 21:26 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-08 21:26 [PATCH 1/6] added outputerr/outputstd log functions Ildar Muslukhov
2013-10-08 21:26 ` [PATCH 2/6] wired outputstd/err functions Ildar Muslukhov
2013-10-08 21:26 ` Ildar Muslukhov [this message]
2013-10-10 18:20 ` [PATCH 3/6] refactored output function Dave Jones
2013-10-15 17:07 ` Ildar Muslukhov
2013-10-08 21:26 ` [PATCH 4/6] wired in output function instead of printf (and some missing outputstd) Ildar Muslukhov
2013-10-09 16:23 ` Dave Jones
2013-10-09 21:40 ` Dave Jones
2013-10-08 21:26 ` [PATCH 5/6] added bufferless logging functions for syscall pamaters Ildar Muslukhov
2013-10-09 16:27 ` Dave Jones
2013-10-08 21:26 ` [PATCH 6/6] wired syscall parameters logging function into syscall (this should fix stack smash bug detected) Ildar Muslukhov
2013-10-09 16:28 ` Dave Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1381267615-9826-3-git-send-email-ildarm@google.com \
--to=ildarm@google.com \
--cc=davej@redhat.com \
--cc=trinity@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox