[PATCH 5/6] lib: log: move line counting to logging back-ends

Colin King colin.king at canonical.com
Wed Jun 20 11:30:28 UTC 2012


From: Colin Ian King <colin.king at canonical.com>

Rather than logging the line counting at the logging top level
we should do the line counting at the logging back-end level
since these are actually re-formatting the data and keeping
track of the per-log line count.

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 src/lib/include/fwts_log.h       |    2 +-
 src/lib/src/fwts_log.c           |    2 --
 src/lib/src/fwts_log_html.c      |    1 +
 src/lib/src/fwts_log_json.c      |    4 +++-
 src/lib/src/fwts_log_plaintext.c |    5 ++++-
 src/lib/src/fwts_log_xml.c       |    4 +++-
 6 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
index e1d360e..35f2801 100644
--- a/src/lib/include/fwts_log.h
+++ b/src/lib/include/fwts_log.h
@@ -87,7 +87,6 @@ typedef enum {
 typedef struct log_t {
 	unsigned int magic;			/* magic ID of the log */
 	fwts_list log_files;			/* list of fwts_log_file */
-	int line_number;			/* keeps track of the line numbering */
 	char *owner;				/* who is writing to this log */
 } fwts_log;
 
@@ -96,6 +95,7 @@ typedef struct log_t {
  */
 typedef struct {
 	FILE *fp;				/* file descriptor for log */
+	int line_number;			/* keeps track of the line numbering */
 	fwts_log *log;				/* parent log struct */
 	fwts_log_type type;			/* log type */
 	fwts_log_filename_type filename_type;	/* log filename type */
diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
index 5de609e..d1f7fe6 100644
--- a/src/lib/src/fwts_log.c
+++ b/src/lib/src/fwts_log.c
@@ -420,7 +420,6 @@ int fwts_log_printf(fwts_log *log,
 				log_file->ops->print(log_file, field, level,
 					status, label, prefix, buffer);
 		}
-		log->line_number++;
 
 		va_end(args);
 	}
@@ -460,7 +459,6 @@ void fwts_log_newline(fwts_log *log)
 			if (log_file->ops && log_file->ops->newline)
 				log_file->ops->newline(log_file);
 		}
-		log->line_number++;
 	}
 }
 
diff --git a/src/lib/src/fwts_log_html.c b/src/lib/src/fwts_log_html.c
index a040b43..09db3bc 100644
--- a/src/lib/src/fwts_log_html.c
+++ b/src/lib/src/fwts_log_html.c
@@ -343,6 +343,7 @@ static int fwts_log_print_html(
 
 	fwts_log_html(log_file, "</TR>\n");
 	fflush(log_file->fp);
+	log_file->line_number++;	/* not used, but bump it anyway */
 
 	return 0;
 }
diff --git a/src/lib/src/fwts_log_json.c b/src/lib/src/fwts_log_json.c
index 8dd65e4..75383a5 100644
--- a/src/lib/src/fwts_log_json.c
+++ b/src/lib/src/fwts_log_json.c
@@ -69,7 +69,7 @@ static int fwts_log_print_json(
 	localtime_r(&now, &tm);
 
 	header = json_object_new_object();
-	json_object_object_add(header, "line_num", json_object_new_int(log_file->log->line_number));
+	json_object_object_add(header, "line_num", json_object_new_int(log_file->line_number));
 	snprintf(tmpbuf, sizeof(tmpbuf), "%2.2d/%2.2d/%-2.2d",
 		tm.tm_mday, tm.tm_mon + 1, (tm.tm_year+1900) % 100);
 	json_object_object_add(header, "date", json_object_new_string(tmpbuf));
@@ -95,6 +95,7 @@ static int fwts_log_print_json(
 	json_object_object_add(header, "log_text", json_object_new_string(buffer));
 
 	json_object_array_add(json_log, header);
+	log_file->line_number++;	/* This is academic really */
 
 	return 0;
 }
@@ -168,6 +169,7 @@ static void fwts_log_close_json(fwts_log_file *log_file)
 	fwrite(str, 1, len, log_file->fp);
 	fwrite("\n", 1, 1, log_file->fp);
 	fflush(log_file->fp);
+	log_file->line_number++;
 
 	json_object_put(json_stack[0].obj);
 }
diff --git a/src/lib/src/fwts_log_plaintext.c b/src/lib/src/fwts_log_plaintext.c
index 7381ae3..256c8a4 100644
--- a/src/lib/src/fwts_log_plaintext.c
+++ b/src/lib/src/fwts_log_plaintext.c
@@ -52,7 +52,7 @@ static int fwts_log_header_plaintext(
 			ptr++;
 			if (!strncmp(ptr, "line", 4)) {
 				n += snprintf(buffer + n, len - n,
-					"%5.5d", log_file->log->line_number);
+					"%5.5d", log_file->line_number);
 				ptr += 4;
 			}
 			if (!strncmp(ptr, "date", 4)) {
@@ -139,6 +139,7 @@ static int fwts_log_print_plaintext(
 		fwrite(text, 1, strlen(text), log_file->fp);
 		fwrite("\n", 1, 1, log_file->fp);
 		fflush(log_file->fp);
+		log_file->line_number++;
 		len += strlen(text) + 1;
 	}
 	fwts_text_list_free(lines);
@@ -171,6 +172,7 @@ static void fwts_log_underline_plaintext(fwts_log_file *log_file, const int ch)
 
 	fwrite(buffer, 1, width, log_file->fp);
 	fflush(log_file->fp);
+	log_file->line_number++;
 
 	free(buffer);
 }
@@ -183,6 +185,7 @@ static void fwts_log_newline_plaintext(fwts_log_file *log_file)
 {
 	fwrite("\n", 1, 1, log_file->fp);
 	fflush(log_file->fp);
+	log_file->line_number++;
 }
 
 fwts_log_ops fwts_log_plaintext_ops = {
diff --git a/src/lib/src/fwts_log_xml.c b/src/lib/src/fwts_log_xml.c
index 19e5e94..47ec35c 100644
--- a/src/lib/src/fwts_log_xml.c
+++ b/src/lib/src/fwts_log_xml.c
@@ -67,7 +67,7 @@ static int fwts_log_print_xml(
 
 	fprintf(log_file->fp, "%*s<line_num>%d</line_num>\n",
 		(xml_stack_index + 1) * XML_INDENT,
-		"", log_file->log->line_number);
+		"", log_file->line_number);
 
 	fprintf(log_file->fp, "%*s<date>%2.2d/%2.2d/%-2.2d</date>\n",
 		(xml_stack_index + 1) * XML_INDENT,
@@ -102,6 +102,7 @@ static int fwts_log_print_xml(
 
 	fprintf(log_file->fp, "%*s</logentry>\n", xml_stack_index * XML_INDENT, "");
 	fflush(log_file->fp);
+	log_file->line_number++;
 
 	return 0;
 }
@@ -169,6 +170,7 @@ static void fwts_log_close_xml(fwts_log_file *log_file)
 
 	fwrite("\n", 1, 1, log_file->fp);
 	fflush(log_file->fp);
+	log_file->line_number++;
 }
 
 fwts_log_ops fwts_log_xml_ops = {
-- 
1.7.10.4





More information about the fwts-devel mailing list