src/test/SDL_test_log.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Fri, 30 Nov 2012 23:25:34 -0800
changeset 6717 2acd95060548
child 6718 918ba414168b
permissions -rw-r--r--
Add log, assert and harness (partial) to test lib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     1
/*
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     2
  Simple DirectMedia Layer
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     4
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     7
  arising from the use of this software.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     8
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    12
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    16
     appreciated but is not required.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    18
     misrepresented as being the original software.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    20
*/
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    21
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    22
/*
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    23
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    24
 Used by the test framework and test cases. 
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    25
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    26
*/
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    27
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    28
#include "SDL_config.h"
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    29
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    30
#include <stdarg.h> /* va_list */
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    31
#include <time.h>
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    32
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    33
#include "SDL_test.h"
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    34
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    35
/* 
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    36
 * Note: Maximum size of SDLTest log message is less than SDLs limit 
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    37
 * to ensure we can fit additional information such as the timestamp. 
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    38
 */
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    39
#define SDLTEST_MAX_LOGMESSAGE_LENGTH	3584
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    40
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    41
/*!
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    42
 * Converts unix timestamp to its ascii representation in localtime
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    43
 *
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    44
 * Note: Uses a static buffer internally, so the return value
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    45
 * isn't valid after the next call of this function. If you
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    46
 * want to retain the return value, make a copy of it.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    47
 *
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    48
 * \param timestamp A Timestamp, i.e. time(0)
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    49
 *
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    50
 * \return Ascii representation of the timestamp in localtime
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    51
 */
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    52
char *SDLTest_TimestampToString(const time_t timestamp) 
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    53
{
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    54
	time_t copy;
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    55
	static char buffer[256];
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    56
	struct tm *local;
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    57
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    58
	memset(buffer, 0, sizeof(buffer));\
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    59
	copy = timestamp;
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    60
	local = localtime(&copy);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    61
	strftime(buffer, sizeof(buffer), "%a %Y-%m-%d %H:%M:%S %Z", local);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    62
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    63
	return buffer;
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    64
}
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    65
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    66
/*
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    67
 * Prints given message with a timestamp in the TEST category and INFO priority.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    68
 */
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    69
void SDLTest_Log(char *fmt, ...)
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    70
{
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    71
	va_list list;
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    72
	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    73
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    74
	// Print log message into a buffer
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    75
	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    76
	va_start(list, fmt);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    77
	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    78
	va_end(list);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    79
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    80
	// Log with timestamp and newline
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    81
	SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, "%s: %s\n", SDLTest_TimestampToString(time(0)), logMessage);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    82
}
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    83
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    84
/*
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    85
 * Prints given message with a timestamp in the TEST category and the ERROR priority.
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    86
 */
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    87
void SDLTest_LogError(char *fmt, ...)
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    88
{
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    89
	va_list list;
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    90
	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    91
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    92
	// Print log message into a buffer
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    93
	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    94
	va_start(list, fmt);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    95
	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    96
	va_end(list);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    97
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    98
	// Log with timestamp and newline
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    99
	SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s\n", SDLTest_TimestampToString(time(0)), logMessage);
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
   100
}