Added simple logging levels to logging system.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Wed, 27 Jul 2011 17:48:07 +0300
changeset 5785 7fc252ce5d1d
parent 5784 676f4613ee14
child 5786 b6df0fc13504
Added simple logging levels to logging system. Added new option: --verbose. Fixed help for option --version.
test/test-automation/SDL_test.h
test/test-automation/logger.h
test/test-automation/plain_logger.c
test/test-automation/plain_logger.h
test/test-automation/runner.c
test/test-automation/testdummy/testdummy.c
test/test-automation/xml_logger.c
test/test-automation/xml_logger.h
--- a/test/test-automation/SDL_test.h	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/SDL_test.h	Wed Jul 27 17:48:07 2011 +0300
@@ -28,16 +28,19 @@
 
 #include "fuzzer/fuzzer.h"
 
+/*
 extern int _testReturnValue;
 extern int _testAssertsFailed;
 extern int _testAssertsPassed;
+*/
 
-extern AssertFp testAssert;
-
-// \todo Should these be consts?
 #define TEST_ENABLED  1
 #define TEST_DISABLED 0
 
+//! Definitions of assert results
+#define ASSERT_PASS 1
+#define ASSERT_FAILURE 0
+
 //! Definition of all the possible test results
 #define TEST_RESULT_PASS			0
 #define TEST_RESULT_FAILURE			1
--- a/test/test-automation/logger.h	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/logger.h	Wed Jul 27 17:48:07 2011 +0300
@@ -23,6 +23,18 @@
 
 #include <time.h>
 
+/* Logging levels */
+typedef enum Level {
+	STANDARD = 1,
+	VERBOSE
+} Level;
+
+
+typedef struct LoggerData {
+	Level level; //!< Logging level of the logger (such as VERBOSE)
+	void  *custom; //!< Some custom data that a logger needs
+} LoggerData;
+
 /*!
  * Typedefs for function pointers that implement the  generic
  * logging interface. See the headers of implementations (plain_logger.h or
--- a/test/test-automation/plain_logger.c	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/plain_logger.c	Wed Jul 27 17:48:07 2011 +0300
@@ -2,12 +2,17 @@
 #ifndef _PLAIN_LOGGER
 #define _PLAIN_LOGGER
 
+#include "Logger.h"
 #include "logger_helpers.h"
 #include "plain_logger.h"
 #include "SDL_test.h"
 
+/*! Current indentationt level */
 static int indentLevel;
 
+/*! Logging level of the logger */
+static Level level = STANDARD;
+
 /*!
  * Prints out the output of the logger
  *
@@ -38,7 +43,7 @@
 
 void
 PlainRunStarted(int parameterCount, char *runnerParameters[], char *runSeed,
-			    time_t eventTime, void *data)
+			    time_t eventTime, LoggerData *data)
 {
 	Output(indentLevel, "Test run started at %s", TimestampToString(eventTime));
 	Output(indentLevel, "Fuzzer seed is %s", runSeed);
@@ -50,6 +55,8 @@
 		Output(indentLevel, "\t%s", parameter);
 	}
 
+	level = data->level;
+
 	Output(indentLevel, "");
 }
 
@@ -117,6 +124,11 @@
 PlainAssert(const char *assertName, int assertResult, const char *assertMessage,
 		time_t eventTime)
 {
+	// Log passed asserts only on VERBOSE level
+	if(level <= STANDARD && assertResult == ASSERT_PASS) {
+		return ;
+	}
+
 	const char *result = (assertResult) ? "passed" : "failed";
 	Output(indentLevel, "%s: %s - %s", assertName, result, assertMessage);
 }
@@ -125,6 +137,11 @@
 PlainAssertWithValues(const char *assertName, int assertResult, const char *assertMessage,
 		int actualValue, int expectedValue, time_t eventTime)
 {
+	// Log passed asserts only on VERBOSE level
+	if(level <= STANDARD && assertResult == ASSERT_PASS) {
+		return ;
+	}
+
 	const char *result = (assertResult) ? "passed" : "failed";
 	Output(indentLevel, "%s: %s (expected %d, actualValue %d) - %s",
 			assertName, result, expectedValue, actualValue, assertMessage);
--- a/test/test-automation/plain_logger.h	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/plain_logger.h	Wed Jul 27 17:48:07 2011 +0300
@@ -10,11 +10,11 @@
  * \param runnerParameters What parameters were given to the runner
  * \param runSeed Fuzzer seed of the harness
  * \param eventTime When the execution started
- * \param data Any additional data logger needs
+ * \param loggerData LoggerData structure which contains data for the logger
  *
  */
 void PlainRunStarted(int parameterCount, char *runnerParameters[], char *runSeed,
-					 time_t eventTime, void *data);
+					 time_t eventTime, LoggerData *data);
 
 /*!
  * Prints out information about ending the test run.
--- a/test/test-automation/runner.c	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/runner.c	Wed Jul 27 17:48:07 2011 +0300
@@ -72,6 +72,9 @@
 static int xsl_enabled = 0;
 //! Flag for enabling universal timeout for tests
 static int universal_timeout_enabled = 0;
+//! Flag for enabling verbose logging
+static int enable_verbose_logger = 0;
+
 
 //!< Size of the test and suite name buffers
 #define NAME_BUFFER_SIZE 1024
@@ -804,12 +807,19 @@
 /*!
  * Sets up the logger.
  *
- * \return Some special data that will be passed to StartRun() logger call
+ * \return Logger data structure (that needs be deallocated)
  */
 void *
 SetUpLogger()
 {
-	void *loggerData = NULL;
+	LoggerData *loggerData = SDL_malloc(sizeof(loggerData));
+	if(loggerData == NULL) {
+		fprintf(stderr, "Error: Logger data structure not allocated.");
+		return NULL;
+	}
+
+	loggerData->level = (enable_verbose_logger ? VERBOSE : STANDARD);
+
 	if(xml_enabled) {
 		RunStarted = XMLRunStarted;
 		RunEnded = XMLRunEnded;
@@ -835,7 +845,7 @@
 			sheet = xsl_stylesheet_name;
 		}
 
-		loggerData = sheet;
+		loggerData->custom = sheet;
 	} else {
 		RunStarted = PlainRunStarted;
 		RunEnded = PlainRunEnded;
@@ -862,14 +872,15 @@
  */
 void
 PrintUsage() {
-	  printf("Usage: ./runner [--in-proc] [--suite SUITE] [--test TEST]\n");
-	  printf("                [--name-contains SUBSTR] [--show-tests]\n");
-	  printf("                [--xml] [--xsl [STYLESHEET]] [--timeout VALUE]\n");
-	  printf("                [--exec-key KEY] [--iterations VALUE]\n");
-	  printf("                [--seed VALUE] [--version] [--help]\n");
+	  printf("Usage: ./runner [--in-proc] [--show-tests] [--verbose] [--xml]\n");
+	  printf("                [--xsl [STYLESHEET]] [--seed VALUE] [--iterations VALUE]\n");
+	  printf("                [--exec-key KEY] [--timeout VALUE] [--test TEST]\n");
+	  printf("                [--name-contains SUBSTR] [--suite SUITE]\n");
+	  printf("                [--version] [--help]\n");
 	  printf("Options:\n");
 	  printf("     --in-proc                Executes tests in-process\n");
 	  printf("     --show-tests             Prints out all the executable tests\n");
+	  printf(" -v  --verbose                Enables verbose logging\n");
 	  printf("     --xml                    Enables XML logger\n");
 	  printf("     --xsl [STYLESHEET]       Adds XSL stylesheet to the XML test reports for\n");
 	  printf("                              browser viewing. Optionally uses the specified XSL\n");
@@ -887,6 +898,7 @@
 	  printf("                              substring in test name\n");
 	  printf(" -s  --suite SUITE            Executes only the given test suite\n");
 
+	  printf("     --version                Print version information\n");
 	  printf(" -h  --help                   Print this help\n");
 }
 
@@ -913,6 +925,9 @@
       else if(SDL_strcmp(arg, "--xml") == 0) {
     	  xml_enabled = 1;
       }
+      else if(SDL_strcmp(arg, "--verbose") == 0 || SDL_strcmp(arg, "-v") == 0) {
+    	  enable_verbose_logger = 1;
+      }
       else if(SDL_strcmp(arg, "--timeout") == 0 || SDL_strcmp(arg, "-tm") == 0) {
     	  universal_timeout_enabled = 1;
     	  char *timeoutString = NULL;
@@ -1062,7 +1077,7 @@
 	char *extension = "dylib";
 #endif
 
-	void *loggerData = SetUpLogger();
+	LoggerData *loggerData = SetUpLogger();
 
 	const Uint32 startTicks = SDL_GetTicks();
 
@@ -1083,6 +1098,9 @@
 
 	RunStarted(argc, argv, runSeed, time(0), loggerData);
 
+	// logger data is no longer used
+	SDL_free(loggerData);
+
 	if(execute_inproc && universal_timeout_enabled) {
 		Log(time(0), "Test timeout is not supported with in-proc execution.");
 		Log(time(0), "Timeout will be disabled...");
--- a/test/test-automation/testdummy/testdummy.c	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/testdummy/testdummy.c	Wed Jul 27 17:48:07 2011 +0300
@@ -107,7 +107,7 @@
 		Log(0, "%d", random);
 	}
 
-	Log(0, "Random: %s", RandomAsciiString());
+	//Log(0, "Random: %s", RandomAsciiString());
 }
 
 void
--- a/test/test-automation/xml_logger.c	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/xml_logger.c	Wed Jul 27 17:48:07 2011 +0300
@@ -24,6 +24,7 @@
 
 #include <SDL/SDL.h>
 
+#include "Logger.h"
 #include "xml.h"
 #include "logger_helpers.h"
 #include "SDL_test.h"
@@ -66,6 +67,9 @@
 /*! Current indentationt level */
 static int indentLevel;
 
+/*! Logging level of the logger */
+static Level level = STANDARD;
+
 //! Constants for XMLOuputters EOL parameter
 #define YES 1
 #define NO 0
@@ -111,9 +115,10 @@
 
 void
 XMLRunStarted(int parameterCount, char *runnerParameters[], char *runSeed,
-			 time_t eventTime, void *data)
+			 time_t eventTime, LoggerData *data)
 {
-	char *xslStylesheet = (char *)data;
+	char *xslStylesheet = (char *)data->custom;
+	level = data->level;
 
 	char *output = XMLOpenDocument(documentRoot, xslStylesheet);
 	XMLOutputter(indentLevel++, YES, output);
@@ -241,6 +246,7 @@
 void
 XMLSuiteStarted(const char *suiteName, time_t eventTime)
 {
+	// log suite name
 	char *output = XMLOpenElement(suiteElementName);
 	XMLOutputter(indentLevel++, YES, output);
 
@@ -250,12 +256,14 @@
 	output = XMLAddContent(suiteName);
 	XMLOutputter(indentLevel, NO, output);
 
+	// log test name
 	output = XMLCloseElement(nameElementName);
 	XMLOutputter(--indentLevel, YES, output);
 
 	output = XMLOpenElement(startTimeElementName);
 	XMLOutputter(indentLevel++, NO, output);
 
+	// log beginning time
 	output = XMLAddContent(TimestampToString(eventTime));
 	XMLOutputter(indentLevel, NO, output);
 
@@ -329,8 +337,7 @@
 	char * output = XMLOpenElement(testElementName);
 	XMLOutputter(indentLevel++, YES, output);
 
-	//Attribute attribute = {"test", "value"};
-	//XMLOpenElementWithAttribute("name", &attribute);
+	// log test name
 	output = XMLOpenElement(nameElementName);
 	XMLOutputter(indentLevel++, NO, output);
 
@@ -340,6 +347,7 @@
 	output = XMLCloseElement(nameElementName);
 	XMLOutputter(--indentLevel, YES, output);
 
+	// log test description
 	output = XMLOpenElement(descriptionElementName);
 	XMLOutputter(indentLevel++, NO, output);
 
@@ -349,7 +357,7 @@
 	output = XMLCloseElement(descriptionElementName);
 	XMLOutputter(--indentLevel, YES, output);
 
-	// log exec key
+	// log execution key
 	output = XMLOpenElement(execKeyElementName);
 	XMLOutputter(indentLevel++, NO, output);
 
@@ -457,6 +465,11 @@
 XMLAssert(const char *assertName, int assertResult, const char *assertMessage,
 		  time_t eventTime)
 {
+	// Log passed asserts only on VERBOSE level
+	if(level <= STANDARD && assertResult == ASSERT_PASS) {
+		return ;
+	}
+
 	char *output = XMLOpenElement(assertElementName);
 	XMLOutputter(indentLevel++, YES, output);
 
@@ -509,6 +522,11 @@
 XMLAssertWithValues(const char *assertName, int assertResult, const char *assertMessage,
 		int actualValue, int excpected, time_t eventTime)
 {
+	// Log passed asserts only on VERBOSE level
+	if(level <= STANDARD && assertResult == ASSERT_PASS) {
+		return ;
+	}
+
 	char *output = XMLOpenElement(assertElementName);
 	XMLOutputter(indentLevel++, YES, output);
 
--- a/test/test-automation/xml_logger.h	Mon Jul 25 20:32:31 2011 +0300
+++ b/test/test-automation/xml_logger.h	Wed Jul 27 17:48:07 2011 +0300
@@ -10,10 +10,10 @@
  * \param runnerParameters What parameters were given to the runner
  * \param runSeed Fuzzer seed of the harness
  * \param eventTime When the execution started
- * \param data Any additional data logger needs
+ * \param loggerData LoggerData structure which contains data for the logger
  */
 void XMLRunStarted(int parameterCount, char *runnerParameters[], char *runSeed,
-					time_t eventTime, void *data);
+					time_t eventTime, LoggerData *data);
 
 /*!
  * Prints out information about ending the test run in XML