Various fixes and additions to logger system.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Tue, 21 Jun 2011 22:04:44 +0300
changeset 5706 9da43acca81f
parent 5705 b320f7a66ac4
child 5707 67940bee217e
Various fixes and additions to logger system.
test/test-automation/Makefile.am
test/test-automation/logger.c
test/test-automation/logger.h
test/test-automation/plain_logger.c
test/test-automation/plain_logger.h
test/test-automation/xml.c
test/test-automation/xml.h
test/test-automation/xml_logger.c
test/test-automation/xml_logger.h
--- a/test/test-automation/Makefile.am	Tue Jun 21 19:31:46 2011 +0300
+++ b/test/test-automation/Makefile.am	Tue Jun 21 22:04:44 2011 +0300
@@ -8,7 +8,7 @@
 runner_LDFLAGS = `sdl-config --libs` 
 
 bin_PROGRAMS = logger
-logger_SOURCES = xml_logger.c xml.c
+logger_SOURCES = xml_logger.c xml.c plain_logger.c logger.c
 logger_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT
 logger_LDFLAGS = `sdl-config --libs` 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-automation/logger.c	Tue Jun 21 22:04:44 2011 +0300
@@ -0,0 +1,89 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#include "logger.h"
+#include "xml_logger.h"
+#include "plain_logger.h"
+
+// Pointers to selected logger implementation
+RunStartedFp RunStarted = 0;
+RunEndedFp RunEnded = 0;
+SuiteStartedFp SuiteStarted = 0;
+SuiteEndedFp SuiteEnded = 0;
+TestStartedFp TestStarted = 0;
+TestEndedFp TestEnded = 0;
+AssertFp Assert = 0;
+LogFp Log = 0;
+
+/*!
+ * Prints the given message to stderr. Function adds nesting
+ * to the output.
+ *
+ * \return Possible error value (\todo)
+ */
+int
+LogGenericOutput(const char *message)
+{
+	/*
+	int depth = indentDepth;
+	while(depth--) {
+		fprintf(stderr, " ");
+	}
+	*/
+
+	fprintf(stderr, "%s\n", message);
+	fflush(stderr);
+}
+
+
+/*!
+ * Test app for logging functionality
+ */
+int
+main(int argc, char *argv[])
+{
+	int xml_enabled = 1;
+
+	if(xml_enabled) {
+		RunStarted = XMLRunStarted;
+		RunEnded = XMLRunEnded;
+
+		SuiteStarted = XMLSuiteStarted;
+		SuiteEnded = XMLSuiteEnded;
+
+		TestStarted = XMLTestStarted;
+		TestEnded = XMLTestEnded;
+
+		Assert = XMLAssert;
+		Log = XMLLog;
+	} else {
+		RunStarted = PlainRunStarted;
+		RunEnded = PlainRunEnded;
+
+		SuiteStarted = PlainSuiteStarted;
+		SuiteEnded = PlainSuiteEnded;
+
+		TestStarted = PlainTestStarted;
+		TestEnded = PlainTestEnded;
+
+		Assert = PlainAssert;
+		Log = PlainLog;
+	}
+
+	RunStarted(LogGenericOutput, "All the data from harness", 0);
+	SuiteStarted("Suite data here", 0);
+
+	TestStarted("test1", "desc", 0);
+	TestEnded("test1", "desc", 0, 0, 0, 0);
+
+	//XMLTestStarted("test2", "desc", 0);
+	//XMLTestEnded("test2", "desc", 0, 0, 0, 0);
+
+	SuiteEnded(0, 0, 0, 0.0f, 0);
+	RunEnded(0, 0);
+
+	return 0;
+}
--- a/test/test-automation/logger.h	Tue Jun 21 19:31:46 2011 +0300
+++ b/test/test-automation/logger.h	Tue Jun 21 22:04:44 2011 +0300
@@ -26,25 +26,26 @@
 // Function pointer to function which handles to output
 typedef int (*LogOutputFp)(const char *);
 
+
 /*!
  * Generic logger interface
  *
  */
-void RunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime);
-void RunEnded(time_t endTime, time_t totalRuntime);
+typedef	void (*RunStartedFp)(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime);
+typedef	void (*RunEndedFp)(time_t endTime, time_t totalRuntime);
 
-void SuiteStarted(const char *suiteName, time_t eventTime);
-void SuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
-                double endTime, time_t totalRuntime);
+typedef	void (*SuiteStartedFp)(const char *suiteName, time_t eventTime);
+typedef	void (*SuiteEndedFp)(int testsPassed, int testsFailed, int testsSkipped,
+		                double endTime, time_t totalRuntime);
 
-void TestStarted(const char *testName, const char *testDescription, time_t startTime);
-void TestEnded(const char *testName, const char *testDescription, int testResult,
-               int numAsserts, time_t endTime, time_t totalRuntime);
+typedef	void (*TestStartedFp)(const char *testName, const char *testDescription, time_t startTime);
+typedef	void (*TestEndedFp)(const char *testName, const char *testDescription, int testResult,
+		               int numAsserts, time_t endTime, time_t totalRuntime);
 
-void Assert(const char *assertName, int assertResult, const char *assertMessage,
-            time_t eventTime);
+typedef	void (*AssertFp)(const char *assertName, int assertResult, const char *assertMessage,
+		            time_t eventTime);
 
-void Log(const char *logMessage, time_t eventTime);
+typedef	void (*LogFp)(const char *logMessage, time_t eventTime);
 
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-automation/plain_logger.c	Tue Jun 21 22:04:44 2011 +0300
@@ -0,0 +1,63 @@
+
+#ifndef _PLAIN_LOGGER
+#define _PLAIN_LOGGER
+
+#include <stdio.h>
+
+#include "plain_logger.h"
+
+
+LogOutputFp logger = 0;
+
+void
+PlainRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
+{
+	logger = outputFn;
+}
+
+void
+PlainRunEnded(time_t endTime, time_t totalRuntime)
+{
+	// \todo add total number of tests, suites, pass/failure test count
+}
+
+void
+PlainSuiteStarted(const char *suiteName, time_t eventTime)
+{
+	printf("Executing tests in %s\n", suiteName);
+}
+
+void
+PlainSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
+           double endTime, time_t totalRuntime)
+{
+	printf("Suite executed. %d passed, %d failed and %d skipped\n", testsPassed, testsFailed, testsSkipped);
+}
+
+void
+PlainTestStarted(const char *testName, const char *testDescription, time_t startTime)
+{
+}
+
+void
+PlainTestEnded(const char *testName, const char *testDescription,
+          int testResult, int numAsserts, time_t endTime, time_t totalRuntime)
+{
+	printf("Asserts:%d\n", numAsserts);
+	printf("%s: ok\n", testName);
+}
+
+void
+PlainAssert(const char *assertName, int assertResult, const char *assertMessage,
+       time_t eventTime)
+{
+	const char *result = (assertResult) ? "passed" : "failed";
+	printf("%s %s: %s\n", assertName, assertResult, assertMessage);
+}
+
+void
+PlainLog(const char *logMessage, time_t eventTime)
+{
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-automation/plain_logger.h	Tue Jun 21 22:04:44 2011 +0300
@@ -0,0 +1,25 @@
+#ifndef _PLAIN_LOGGER_H
+#define _PLAIN_LOGGER_H
+
+#include "logger.h"
+
+void PlainRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime);
+
+void PlainRunEnded(time_t endTime, time_t totalRuntime);
+
+void PlainSuiteStarted(const char *suiteName, time_t eventTime);
+
+void PlainSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
+           double endTime, time_t totalRuntime);
+
+void PlainTestStarted(const char *testName, const char *testDescription, time_t startTime);
+
+void PlainTestEnded(const char *testName, const char *testDescription,
+          int testResult, int numAsserts, time_t endTime, time_t totalRuntime);
+
+void PlainAssert(const char *assertName, int assertResult, const char *assertMessage,
+       time_t eventTime);
+
+void PlainLog(const char *logMessage, time_t eventTime);
+
+#endif
--- a/test/test-automation/xml.c	Tue Jun 21 19:31:46 2011 +0300
+++ b/test/test-automation/xml.c	Tue Jun 21 22:04:44 2011 +0300
@@ -19,10 +19,11 @@
 */
 
 
+#ifndef _XML_C
+#define _XML_C
+
 #include <stdio.h>
-//#include <stdlib.h>
 #include <string.h>
-//#include <stdarg.h>
 #include <assert.h>
 
 #include <SDL/SDL.h>
@@ -110,12 +111,12 @@
 /*
 ===================
 
-	XML
+	Functions to handle XML creation
 
 ===================
 */
 
-static int has_open_element = 0;
+static const char *root;
 
 void
 XMLOpenDocument(const char *rootTag, LogOutputFp log)
@@ -133,29 +134,15 @@
 
 	// add open tag
 	AddOpenTag(rootTag);
+
+	root = rootTag; // it's fine, as long as rootTag points to static memory?
 }
 
 void
 XMLCloseDocument() {
-	// Close the open tags with proper nesting
-	TagList *openTag = openTags;
-	while(openTag) {
-		TagList *temp = openTag->next;
-
-		size_t size = SDL_strlen(openTag->tag) + 4 + 1; /* one extra for '\0', '<', '/' and '>' */
-		char *buffer = SDL_malloc(size);
-		snprintf(buffer, size, "%s%s%s", "</", openTag->tag, ">");
-		logger(buffer);
-		SDL_free(buffer);
-
-		RemoveOpenTag(openTag->tag);
-
-		openTag = temp;
-	}
+	XMLCloseElement(root);
 }
 
-static const char *currentTag = NULL;
-
 void
 XMLOpenElement(const char *tag)
 {
@@ -165,10 +152,6 @@
 	logger(buffer);
 	SDL_free(buffer);
 
-	currentTag = tag;
-
-	has_open_element = 1;
-
 	AddOpenTag(tag);
 }
 
@@ -183,10 +166,6 @@
 	logger(buffer);
 	SDL_free(buffer);
 
-	currentTag = tag;
-
-	has_open_element = 1;
-
 	AddOpenTag(tag);
 }
 
@@ -194,10 +173,6 @@
 void
 XMLAddAttribute(const char *attribute, const char *value)
 {
-	// Requires open element
-	if(has_open_element == 0) {
-		return ;
-	}
 	size_t attributeSize = SDL_strlen(attribute);
 	size_t valueSize = SDL_strlen(value);
 
@@ -249,6 +224,7 @@
 			break;
 		}
 	}
+}
 
-	has_open_element = 0;
-}
+
+#endif
--- a/test/test-automation/xml.h	Tue Jun 21 19:31:46 2011 +0300
+++ b/test/test-automation/xml.h	Tue Jun 21 22:04:44 2011 +0300
@@ -67,10 +67,11 @@
 void XMLAddContent(const char *content);
 
 /*!
- * Closes previously opened element.
- * Enforces proper nesting by not allowing end elements haphazardly.
+ * Closes previously opened element until tag given as parameter is met.
+ * Enforces proper nesting by not allowing to close elements out-of-order.
  *
  * Closes all the opened elements until the given element/tag is found
+ * which will be the last tag to be closed
  *
  * \param tag Element to close
  */
--- a/test/test-automation/xml_logger.c	Tue Jun 21 19:31:46 2011 +0300
+++ b/test/test-automation/xml_logger.c	Tue Jun 21 22:04:44 2011 +0300
@@ -18,42 +18,16 @@
   3. This notice may not be removed or altered from any source distribution.
 */
 
-#ifndef _LOGGER_C
-#define _LOGGER_C
-
-#include "logger.h"
+#ifndef _XML_LOGGER_C
+#define _XML_LOGGER_C
 
 #include "xml.h"
-
-#include <SDL/SDL.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
+#include "logger.h"
 
-/*!
- * Prints the given message to stderr. Function adds nesting
- * to the output.
- *
- * \return Possible error value (\todo)
- */
-int
-LogGenericOutput(const char *message)
-{
-	/*
-	int depth = indentDepth;
-	while(depth--) {
-		fprintf(stderr, " ");
-	}
-	*/
-
-	fprintf(stderr, "%s\n", message);
-	fflush(stderr);
-}
+#include "xml_logger.h"
 
 void
-RunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
+XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
 {
 	XMLOpenDocument("testlog", outputFn);
 
@@ -63,13 +37,13 @@
 }
 
 void
-RunEnded(time_t endTime, time_t totalRuntime)
+XMLRunEnded(time_t endTime, time_t totalRuntime)
 {
-	XMLCloseDocument();
+	XMLCloseDocument("testlog");
 }
 
 void
-SuiteStarted(const char *suiteName, time_t eventTime)
+XMLSuiteStarted(const char *suiteName, time_t eventTime)
 {
 	XMLOpenElement("suite");
 
@@ -79,51 +53,59 @@
 }
 
 void
-SuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
+XMLSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
            double endTime, time_t totalRuntime)
 {
 	XMLCloseElement("suite");
 }
 
 void
-TestStarted(const char *testName, const char *testDescription, time_t startTime)
+XMLTestStarted(const char *testName, const char *testDescription, time_t startTime)
 {
+	XMLOpenElement("test");
 
+	XMLOpenElement("name");
+	XMLAddContent(testName);
+	XMLCloseElement("name");
+
+	XMLOpenElement("description");
+	XMLAddContent(testDescription);
+	XMLCloseElement("description");
+
+	XMLOpenElement("starttime");
+	//XMLAddContent(startTime);
+	XMLCloseElement("starttime");
 }
 
 void
-TestEnded(const char *testName, const char *testDescription, int testResult,
-          int numAsserts, time_t endTime, time_t totalRuntime)
+XMLTestEnded(const char *testName, const char *testDescription,
+          int testResult, int numAsserts, time_t endTime, time_t totalRuntime)
 {
-
-}
-
-void
-Assert(const char *assertName, int assertResult, const char *assertMessage,
-       time_t eventTime)
-{
-
+	XMLCloseElement("test");
 }
 
 void
-Log(const char *logMessage, time_t eventTime)
+XMLAssert(const char *assertName, int assertResult, const char *assertMessage,
+       time_t eventTime)
 {
+	XMLOpenElement("assert");
 
-}
+	XMLOpenElement("result");
+	XMLAddContent((assertResult) ? "pass" : "failure");
+	XMLOpenElement("result");
 
 
-/*!
- * Main for testing the logger
- */
-int
-main(int argc, char *argv[])
+	XMLCloseElement("assert");
+}
+
+void
+XMLLog(const char *logMessage, time_t eventTime)
 {
-	RunStarted(LogGenericOutput, "All the data from harness", 0);
-	SuiteStarted("Suite data here", 0);
-	SuiteEnded(0, 0, 0, 0.0f, 0);
-	RunEnded(0, 0);
+	XMLOpenElement("log");
 
-	return 0;
+	XMLAddContent(logMessage);
+
+	XMLCloseElement("log");
 }
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test-automation/xml_logger.h	Tue Jun 21 22:04:44 2011 +0300
@@ -0,0 +1,25 @@
+#ifndef _XML_LOGGER_H
+#define _XML_LOGGER_H
+
+#include "logger.h"
+
+void XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime);
+
+void XMLRunEnded(time_t endTime, time_t totalRuntime);
+
+void XMLSuiteStarted(const char *suiteName, time_t eventTime);
+
+void XMLSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
+           double endTime, time_t totalRuntime);
+
+void XMLTestStarted(const char *testName, const char *testDescription, time_t startTime);
+
+void XMLTestEnded(const char *testName, const char *testDescription,
+          int testResult, int numAsserts, time_t endTime, time_t totalRuntime);
+
+void XMLAssert(const char *assertName, int assertResult, const char *assertMessage,
+       time_t eventTime);
+
+void XMLLog(const char *logMessage, time_t eventTime);
+
+#endif