Commit f578687b authored by Markus Kauppila's avatar Markus Kauppila

Refining XML-output module

parent c7bdf3c7
...@@ -21,23 +21,6 @@ ...@@ -21,23 +21,6 @@
#ifndef _LOGGER_H #ifndef _LOGGER_H
#define _LOGGER_H #define _LOGGER_H
/*! // Put function pointers here
* Init the logger.
*/
//int LogInit();
/*!
* Quit the logger.
*/
//int LogQuit();
/*!
* Generic ouput function for the logger. Prints the
* message to stderr.
*
* \param scope Source of message (eg. suite, test, harness)
* \return non-zero if logging fails, zero on success
*/
//int LogGenericOutput(int scope, const char *message, ...);
#endif #endif
...@@ -23,45 +23,55 @@ ...@@ -23,45 +23,55 @@
#include "logger.h" #include "logger.h"
#include <SDL/SDL.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
/* \todo /* \todo
* - Fp to LogGenericOutput * - Make XML (and relevant comparisons) case-insensitive
* - print <?xml version="1.0" encoding="utf-8" ?> as header
* - use SDL_malloc etc...
* - nest output /OK
* - dummy functions for no-xml execution
* - Make XML (and relevant comparisions) case-insensitive
*/ */
/*! Function pointer definitions. \todo Move to logger.h */
typedef int (*LogInitFp)(void);
typedef int (*LogCleanUptFp)(void);
/*! Function prototypes */
int LogGenericOutput(char *message);
int XMLLogOutput(const char *tag, const char *fmt, va_list list);
static int xml_enabled = 1; static int xml_enabled = 1;
static int loggingPriority = 0; static int loggingPriority = 0;
static int nestingDepth = 0; static int nestingDepth = 0;
enum Priority { /*! Definitions of log priorities */
typedef enum Priority {
VERBOSE, VERBOSE,
DEFAULT, DEFAULT,
SILENT } Priority;
};
/*! Function pointer definitions. \todo Move to logger.h */
typedef int (*LogOutputFp)(char *);
typedef int (*LogInitFp)(LogOutputFp, Priority);
typedef int (*LogCleanUptFp)(void);
typedef int (*StartTagFp)(Priority, const char *);
typedef int (*EndTagFp)(Priority, const char *);
typedef int (*TagFp)(Priority, const char *, const char *, ...);
/*! Function pointer to output function */
static LogOutputFp OutputFp = NULL;
/*! Definitions for tag styles used in Tagify() */
#define TAG_START 0x00000001 #define TAG_START 0x00000001
#define TAG_END 0x00000002 #define TAG_END 0x00000002
#define TAG_BOTH (TAG_START & TAG_END) #define TAG_BOTH (TAG_START & TAG_END)
/*! Function prototypes \todo move to xml_logger.h */
int XMLStartTag(Priority priority, const char *tag);
int XMLEndTag(Priority priority, const char *tag);
int LogGenericOutput(char *message);
/*! /*!
* Defines structure used for "counting" open XML-tags * Defines structure used for "counting" open XML-tags
*/ */
...@@ -74,11 +84,13 @@ static TagList *openTags = NULL; ...@@ -74,11 +84,13 @@ static TagList *openTags = NULL;
/*! /*!
* Prepend the open tags list * Prepend the open tags list
*
* \return On error returns non-zero value, otherwise zero will returned
*/ */
static int static int
AddOpenTag(const char *tag) AddOpenTag(const char *tag)
{ {
TagList *openTag = malloc(sizeof(TagList)); TagList *openTag = SDL_malloc(sizeof(TagList));
if(openTag == NULL) { if(openTag == NULL) {
return 1; return 1;
} }
...@@ -94,6 +106,8 @@ AddOpenTag(const char *tag) ...@@ -94,6 +106,8 @@ AddOpenTag(const char *tag)
/*! /*!
* Removes the first tag from the open tag list * Removes the first tag from the open tag list
*
* \return On error returns non-zero value, otherwise zero will returned
*/ */
static int static int
RemoveOpenTag(const char *tag) RemoveOpenTag(const char *tag)
...@@ -105,46 +119,24 @@ RemoveOpenTag(const char *tag) ...@@ -105,46 +119,24 @@ RemoveOpenTag(const char *tag)
int retVal = 0; int retVal = 0;
// Tag should always be the same as previously opened tag // Tag should always be the same as previously opened tag
// to prevent opening and ending tag mismatch // It prevents opening and ending tag mismatch
if(strcmp(openTags->tag, tag) == 0) { if(SDL_strcmp(openTags->tag, tag) == 0) {
TagList *openTag = openTags; TagList *openTag = openTags;
openTags = openTags->next; openTags = openTags->next;
free(openTag); free(openTag);
} else { } else {
printf("Else activated!"); printf("Debug | RemoveOpenTag(): open/end tag mismatch");
retVal = 1; retVal = 1;
} }
return retVal; return retVal;
} }
/*!
* Goes through the open tag list and checks if
* given tag is already opened
*
* \return 1 is tag is open, 0 if not
*/
static int
IsTagOpen(const char *tag)
{
int retVal = 0;
TagList *openTag = NULL;
for(openTag = openTags; openTag; openTag = openTag->next) {
if(strcmp(openTag->tag, tag) == 0) {
retVal = 1;
break;
}
}
return retVal;
}
/*! /*!
* Debug function. Prints the contents of the open tags list. * Debug function. Prints the contents of the open tags list.
*/ */
static int static void
PrintOpenTags() PrintOpenTags()
{ {
printf("\nOpen tags:\n"); printf("\nOpen tags:\n");
...@@ -155,24 +147,37 @@ PrintOpenTags() ...@@ -155,24 +147,37 @@ PrintOpenTags()
} }
} }
//! \TODO move these to upwards!! /*!
int XMLStartTag(int priority, const char *tag); * Initializes the XML-logger for creating test reports in XML.
int XMLEndTag(int priority, const char *tag); *
* \return Error code. \todo
*/
int int
XMLLogInit() XMLInit(LogOutputFp logOutputFp, Priority priority)
{ {
OutputFp = logOutputFp;
loggingPriority = priority;
LogGenericOutput("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); //! make "doctype" work with priority level?
OutputFp("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
XMLStartTag(0, "testlog"); XMLStartTag(DEFAULT, "testlog");
} }
/*!
* Cleans up the logger and closes all open XML-tags
*
* \return Error code. \todo
*/
int int
XMLLogCleanUp() XMLCleanUp()
{ {
//! \todo do CloseOpenTags() instead // Close the open tags
XMLEndTag(0, "testlog"); TagList *openTag = openTags;
while(openTag) {
TagList *temp = openTag->next;
XMLEndTag(DEFAULT, openTag->tag);
openTag = temp;
}
} }
/*! /*!
...@@ -181,6 +186,8 @@ XMLLogCleanUp() ...@@ -181,6 +186,8 @@ XMLLogCleanUp()
* \param tag XML-tag to create * \param tag XML-tag to create
* \param tagStyle Do start or end tag, or both. * \param tagStyle Do start or end tag, or both.
* \param message text content of the tags * \param message text content of the tags
*
* \return Well-formed XML tag
*/ */
static char * static char *
Tagify(const char *tag, const int tagStyle, const char *message) Tagify(const char *tag, const int tagStyle, const char *message)
...@@ -207,17 +214,24 @@ Tagify(const char *tag, const int tagStyle, const char *message) ...@@ -207,17 +214,24 @@ Tagify(const char *tag, const int tagStyle, const char *message)
} }
const int size = strlen(buffer) + 1; const int size = SDL_strlen(buffer) + 1;
char *newTag = malloc(size * sizeof(char)); char *newTag = SDL_malloc(size * sizeof(char));
memset(newTag, 0, size * sizeof(char)); memset(newTag, 0, size * sizeof(char));
memcpy(newTag, buffer, size); memcpy(newTag, buffer, size);
return newTag; return newTag;
} }
/*!
* Creates and outputs an start tag
*
* \param priority Priority of the tag
* \param tag Tag for outputting
*
* \return Error code. Non-zero on failure. Zero on success
*/
int int
XMLStartTag(int priority, const char *tag) XMLStartTag(Priority priority, const char *tag)
{ {
if(priority < loggingPriority) { if(priority < loggingPriority) {
return 1; return 1;
...@@ -225,18 +239,26 @@ XMLStartTag(int priority, const char *tag) ...@@ -225,18 +239,26 @@ XMLStartTag(int priority, const char *tag)
AddOpenTag(tag); AddOpenTag(tag);
char *newTag = Tagify(tag, TAG_START, NULL); char *newTag = Tagify(tag, TAG_START, NULL);
LogGenericOutput(newTag); OutputFp(newTag);
free(newTag); SDL_free(newTag);
nestingDepth++; nestingDepth++;
} }
/*!
* Creates and outputs an end tag
*
* \param priority Priority of the tag
* \param tag Tag for outputting
*
* \return Error code. Non-zero on failure. Zero on success
*/
int int
XMLEndTag(int priority, const char *tag) XMLEndTag(Priority priority, const char *tag)
{ {
/* /*
Do it before priority check, so incorrect usage of Do it before priority check, so incorrect usage of
priorities won't mess it up priorities won't mess it up (?)
*/ */
nestingDepth--; nestingDepth--;
...@@ -247,12 +269,22 @@ XMLEndTag(int priority, const char *tag) ...@@ -247,12 +269,22 @@ XMLEndTag(int priority, const char *tag)
RemoveOpenTag(tag); RemoveOpenTag(tag);
char *newTag = Tagify(tag, TAG_END, NULL); char *newTag = Tagify(tag, TAG_END, NULL);
LogGenericOutput(newTag); OutputFp(newTag);
free(newTag); SDL_free(newTag);
} }
/*!
* Creates an XML-tag including start and end tags and text content
* between them.
*
* \param priority Priority of the tag
* \param tag Tag for outputting
* \param fmt Text content of tag as variadic parameter list
*
* \return Error code. Non-zero on failure. Zero on success
*/
int int
XMLTag(int priority, const char *tag, const char *fmt, ...) XMLTag(Priority priority, const char *tag, const char *fmt, ...)
{ {
if(priority < loggingPriority) { if(priority < loggingPriority) {
return 1; return 1;
...@@ -268,14 +300,16 @@ XMLTag(int priority, const char *tag, const char *fmt, ...) ...@@ -268,14 +300,16 @@ XMLTag(int priority, const char *tag, const char *fmt, ...)
va_end(list); va_end(list);
char *newTag = Tagify(tag, TAG_BOTH, buffer); char *newTag = Tagify(tag, TAG_BOTH, buffer);
LogGenericOutput(newTag); //LogGenericOutput(newTag);
free(newTag); OutputFp(newTag);
SDL_free(newTag);
} }
//! \TODO Make it changeable by using a function pointer
/*! /*!
* Prints the given message to stderr. Function adds nesting * Prints the given message to stderr. Function adds nesting
* to the output. * to the output.
*
* \return Possible error value (\todo)
*/ */
int int
LogGenericOutput(char *message) LogGenericOutput(char *message)
...@@ -289,32 +323,63 @@ LogGenericOutput(char *message) ...@@ -289,32 +323,63 @@ LogGenericOutput(char *message)
} }
/*! Quick Dummy functions for testing non-xml output. \todo put to proper place*/
int DummyInit(LogOutputFp output, Priority priority) {
return 0;
}
int DummyCleanUp() {
return 0;
}
int DummyStartTag(Priority priority, const char *tag) {
return 0;
}
int DummyEndTag(Priority priority, const char *tag) {
return 0;
}
int DummyTag(Priority priority, const char *tag, const char *fmt, ...) {
return 0;
}
/*! /*!
* Main for testing the logger * Main for testing the logger
*/ */
int int
main() main(int argc, char *argv[])
{ {
LogInitFp LogInit = NULL; LogInitFp LogInit = NULL;
LogCleanUptFp LogCleanUp = NULL; LogCleanUptFp LogCleanUp = NULL;
StartTagFp StartTag = NULL;
EndTagFp EndTag = NULL;
TagFp Tag = NULL;
if(xml_enabled) { if(xml_enabled) {
// set logger functions to XML // set logger functions to XML
LogInit = XMLLogInit; LogInit = XMLInit;
LogCleanUp = XMLLogCleanUp; LogCleanUp = XMLCleanUp;
StartTag = XMLStartTag;
EndTag = XMLEndTag;
Tag = XMLTag;
} else { } else {
// set up dummy functions // When no XML-output is desired, dummy functions are used
} LogInit = DummyInit;
LogCleanUp = DummyCleanUp;
LogInit(); StartTag = DummyStartTag;
XMLStartTag(0, "hello"); EndTag = DummyEndTag;
XMLStartTag(0, "world!"); Tag = DummyTag;
XMLEndTag(0, "world!"); }
XMLEndTag(0, "hello");
LogCleanUp(); LogInit(LogGenericOutput, VERBOSE);
StartTag(DEFAULT, "hello");
StartTag(DEFAULT, "world");
EndTag(DEFAULT, "world");
//EndTag(DEFAULT, "hello");
LogCleanUp();
#if 0 #if 0
XMLStartTag("log"); XMLStartTag("log");
......
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