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