Commit 94c6ae4e authored by Markus Kauppila's avatar Markus Kauppila

Various fixes and additions to logger system.

parent ac07ad67
...@@ -8,7 +8,7 @@ runner_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT ...@@ -8,7 +8,7 @@ runner_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT
runner_LDFLAGS = `sdl-config --libs` runner_LDFLAGS = `sdl-config --libs`
bin_PROGRAMS = logger 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_CLAGS = -W -Wall -Wextra -g `sdl-config --cflags` -DSDL_NO_COMPAT
logger_LDFLAGS = `sdl-config --libs` logger_LDFLAGS = `sdl-config --libs`
......
#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;
}
...@@ -26,25 +26,26 @@ ...@@ -26,25 +26,26 @@
// Function pointer to function which handles to output // Function pointer to function which handles to output
typedef int (*LogOutputFp)(const char *); typedef int (*LogOutputFp)(const char *);
/*! /*!
* Generic logger interface * Generic logger interface
* *
*/ */
void RunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime); typedef void (*RunStartedFp)(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime);
void RunEnded(time_t endTime, time_t totalRuntime); typedef void (*RunEndedFp)(time_t endTime, time_t totalRuntime);
void SuiteStarted(const char *suiteName, time_t eventTime); typedef void (*SuiteStartedFp)(const char *suiteName, time_t eventTime);
void SuiteEnded(int testsPassed, int testsFailed, int testsSkipped, typedef void (*SuiteEndedFp)(int testsPassed, int testsFailed, int testsSkipped,
double endTime, time_t totalRuntime); double endTime, time_t totalRuntime);
void TestStarted(const char *testName, const char *testDescription, time_t startTime); typedef void (*TestStartedFp)(const char *testName, const char *testDescription, time_t startTime);
void TestEnded(const char *testName, const char *testDescription, int testResult, typedef void (*TestEndedFp)(const char *testName, const char *testDescription, int testResult,
int numAsserts, time_t endTime, time_t totalRuntime); int numAsserts, time_t endTime, time_t totalRuntime);
void Assert(const char *assertName, int assertResult, const char *assertMessage, typedef void (*AssertFp)(const char *assertName, int assertResult, const char *assertMessage,
time_t eventTime); time_t eventTime);
void Log(const char *logMessage, time_t eventTime); typedef void (*LogFp)(const char *logMessage, time_t eventTime);
#endif #endif
#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
#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
...@@ -19,10 +19,11 @@ ...@@ -19,10 +19,11 @@
*/ */
#ifndef _XML_C
#define _XML_C
#include <stdio.h> #include <stdio.h>
//#include <stdlib.h>
#include <string.h> #include <string.h>
//#include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <SDL/SDL.h> #include <SDL/SDL.h>
...@@ -110,12 +111,12 @@ PrintOpenTags() ...@@ -110,12 +111,12 @@ PrintOpenTags()
/* /*
=================== ===================
XML Functions to handle XML creation
=================== ===================
*/ */
static int has_open_element = 0; static const char *root;
void void
XMLOpenDocument(const char *rootTag, LogOutputFp log) XMLOpenDocument(const char *rootTag, LogOutputFp log)
...@@ -133,29 +134,15 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log) ...@@ -133,29 +134,15 @@ XMLOpenDocument(const char *rootTag, LogOutputFp log)
// add open tag // add open tag
AddOpenTag(rootTag); AddOpenTag(rootTag);
root = rootTag; // it's fine, as long as rootTag points to static memory?
} }
void void
XMLCloseDocument() { XMLCloseDocument() {
// Close the open tags with proper nesting XMLCloseElement(root);
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;
}
} }
static const char *currentTag = NULL;
void void
XMLOpenElement(const char *tag) XMLOpenElement(const char *tag)
{ {
...@@ -165,10 +152,6 @@ XMLOpenElement(const char *tag) ...@@ -165,10 +152,6 @@ XMLOpenElement(const char *tag)
logger(buffer); logger(buffer);
SDL_free(buffer); SDL_free(buffer);
currentTag = tag;
has_open_element = 1;
AddOpenTag(tag); AddOpenTag(tag);
} }
...@@ -183,10 +166,6 @@ XMLOpenElementWithAttribute(const char *tag, Attribute attribute) ...@@ -183,10 +166,6 @@ XMLOpenElementWithAttribute(const char *tag, Attribute attribute)
logger(buffer); logger(buffer);
SDL_free(buffer); SDL_free(buffer);
currentTag = tag;
has_open_element = 1;
AddOpenTag(tag); AddOpenTag(tag);
} }
...@@ -194,10 +173,6 @@ XMLOpenElementWithAttribute(const char *tag, Attribute attribute) ...@@ -194,10 +173,6 @@ XMLOpenElementWithAttribute(const char *tag, Attribute attribute)
void void
XMLAddAttribute(const char *attribute, const char *value) XMLAddAttribute(const char *attribute, const char *value)
{ {
// Requires open element
if(has_open_element == 0) {
return ;
}
size_t attributeSize = SDL_strlen(attribute); size_t attributeSize = SDL_strlen(attribute);
size_t valueSize = SDL_strlen(value); size_t valueSize = SDL_strlen(value);
...@@ -249,6 +224,7 @@ XMLCloseElement(const char *tag) ...@@ -249,6 +224,7 @@ XMLCloseElement(const char *tag)
break; break;
} }
} }
has_open_element = 0;
} }
#endif
...@@ -67,10 +67,11 @@ void XMLAddAttribute(const char *attribute, const char *value); ...@@ -67,10 +67,11 @@ void XMLAddAttribute(const char *attribute, const char *value);
void XMLAddContent(const char *content); void XMLAddContent(const char *content);
/*! /*!
* Closes previously opened element. * Closes previously opened element until tag given as parameter is met.
* Enforces proper nesting by not allowing end elements haphazardly. * Enforces proper nesting by not allowing to close elements out-of-order.
* *
* Closes all the opened elements until the given element/tag is found * 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 * \param tag Element to close
*/ */
......
...@@ -18,42 +18,16 @@ ...@@ -18,42 +18,16 @@
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
#ifndef _LOGGER_C #ifndef _XML_LOGGER_C
#define _LOGGER_C #define _XML_LOGGER_C
#include "xml.h"
#include "logger.h" #include "logger.h"
#include "xml.h" #include "xml_logger.h"
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.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);
}
void void
RunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime) XMLRunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
{ {
XMLOpenDocument("testlog", outputFn); XMLOpenDocument("testlog", outputFn);
...@@ -63,13 +37,13 @@ RunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime) ...@@ -63,13 +37,13 @@ RunStarted(LogOutputFp outputFn, const char *runnerParameters, time_t eventTime)
} }
void void
RunEnded(time_t endTime, time_t totalRuntime) XMLRunEnded(time_t endTime, time_t totalRuntime)
{ {
XMLCloseDocument(); XMLCloseDocument("testlog");
} }
void void
SuiteStarted(const char *suiteName, time_t eventTime) XMLSuiteStarted(const char *suiteName, time_t eventTime)
{ {
XMLOpenElement("suite"); XMLOpenElement("suite");
...@@ -79,51 +53,59 @@ SuiteStarted(const char *suiteName, time_t eventTime) ...@@ -79,51 +53,59 @@ SuiteStarted(const char *suiteName, time_t eventTime)
} }
void void
SuiteEnded(int testsPassed, int testsFailed, int testsSkipped, XMLSuiteEnded(int testsPassed, int testsFailed, int testsSkipped,
double endTime, time_t totalRuntime) double endTime, time_t totalRuntime)
{ {
XMLCloseElement("suite"); XMLCloseElement("suite");
} }
void 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 void
TestEnded(const char *testName, const char *testDescription, int testResult, XMLTestEnded(const char *testName, const char *testDescription,
int numAsserts, time_t endTime, time_t totalRuntime) int testResult, int numAsserts, time_t endTime, time_t totalRuntime)
{ {
XMLCloseElement("test");
} }
void void
Assert(const char *assertName, int assertResult, const char *assertMessage, XMLAssert(const char *assertName, int assertResult, const char *assertMessage,
time_t eventTime) time_t eventTime)
{ {
XMLOpenElement("assert");
} XMLOpenElement("result");
XMLAddContent((assertResult) ? "pass" : "failure");
XMLOpenElement("result");
void
Log(const char *logMessage, time_t eventTime)
{
XMLCloseElement("assert");
} }
void
/*! XMLLog(const char *logMessage, time_t eventTime)
* Main for testing the logger
*/
int
main(int argc, char *argv[])
{ {
RunStarted(LogGenericOutput, "All the data from harness", 0); XMLOpenElement("log");
SuiteStarted("Suite data here", 0);
SuiteEnded(0, 0, 0, 0.0f, 0); XMLAddContent(logMessage);
RunEnded(0, 0);
return 0; XMLCloseElement("log");
} }
#endif #endif
#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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment