src/test/SDL_test_log.c
changeset 6717 2acd95060548
child 6718 918ba414168b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_log.c	Fri Nov 30 23:25:34 2012 -0800
     1.3 @@ -0,0 +1,100 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 +
    1.27 + Used by the test framework and test cases. 
    1.28 +
    1.29 +*/
    1.30 +
    1.31 +#include "SDL_config.h"
    1.32 +
    1.33 +#include <stdarg.h> /* va_list */
    1.34 +#include <time.h>
    1.35 +
    1.36 +#include "SDL_test.h"
    1.37 +
    1.38 +/* 
    1.39 + * Note: Maximum size of SDLTest log message is less than SDLs limit 
    1.40 + * to ensure we can fit additional information such as the timestamp. 
    1.41 + */
    1.42 +#define SDLTEST_MAX_LOGMESSAGE_LENGTH	3584
    1.43 +
    1.44 +/*!
    1.45 + * Converts unix timestamp to its ascii representation in localtime
    1.46 + *
    1.47 + * Note: Uses a static buffer internally, so the return value
    1.48 + * isn't valid after the next call of this function. If you
    1.49 + * want to retain the return value, make a copy of it.
    1.50 + *
    1.51 + * \param timestamp A Timestamp, i.e. time(0)
    1.52 + *
    1.53 + * \return Ascii representation of the timestamp in localtime
    1.54 + */
    1.55 +char *SDLTest_TimestampToString(const time_t timestamp) 
    1.56 +{
    1.57 +	time_t copy;
    1.58 +	static char buffer[256];
    1.59 +	struct tm *local;
    1.60 +
    1.61 +	memset(buffer, 0, sizeof(buffer));\
    1.62 +	copy = timestamp;
    1.63 +	local = localtime(&copy);
    1.64 +	strftime(buffer, sizeof(buffer), "%a %Y-%m-%d %H:%M:%S %Z", local);
    1.65 +
    1.66 +	return buffer;
    1.67 +}
    1.68 +
    1.69 +/*
    1.70 + * Prints given message with a timestamp in the TEST category and INFO priority.
    1.71 + */
    1.72 +void SDLTest_Log(char *fmt, ...)
    1.73 +{
    1.74 +	va_list list;
    1.75 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    1.76 +
    1.77 +	// Print log message into a buffer
    1.78 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    1.79 +	va_start(list, fmt);
    1.80 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
    1.81 +	va_end(list);
    1.82 +
    1.83 +	// Log with timestamp and newline
    1.84 +	SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, "%s: %s\n", SDLTest_TimestampToString(time(0)), logMessage);
    1.85 +}
    1.86 +
    1.87 +/*
    1.88 + * Prints given message with a timestamp in the TEST category and the ERROR priority.
    1.89 + */
    1.90 +void SDLTest_LogError(char *fmt, ...)
    1.91 +{
    1.92 +	va_list list;
    1.93 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    1.94 +
    1.95 +	// Print log message into a buffer
    1.96 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    1.97 +	va_start(list, fmt);
    1.98 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
    1.99 +	va_end(list);
   1.100 +
   1.101 +	// Log with timestamp and newline
   1.102 +	SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s\n", SDLTest_TimestampToString(time(0)), logMessage);
   1.103 +}