src/test/SDL_test_log.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 11 Jul 2013 23:17:52 -0400
changeset 7405 5a9172b56b18
parent 7191 75360622e65f
child 7678 286c42d7c5ed
permissions -rw-r--r--
Attempt to fix a compiler warning on Haiku. (if this works...Haiku generates no warnings. I know, right?!?)
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
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6772
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
6717
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
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    24
 Used by the test framework and test cases.
6717
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
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    28
/* quiet windows compiler warnings */
6718
918ba414168b Update assert API in test lib; add to and harness; add test lib to VS2010 and VS2012 solution; fix VS2012 solution; fix compiler warning
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6717
diff changeset
    29
#define _CRT_SECURE_NO_WARNINGS
918ba414168b Update assert API in test lib; add to and harness; add test lib to VS2010 and VS2012 solution; fix VS2012 solution; fix compiler warning
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6717
diff changeset
    30
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    31
#include "SDL_config.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 <stdarg.h> /* va_list */
6772
5229a117ef97 Added audio test suite; minor code cleanups in test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6756
diff changeset
    34
#include <stdio.h>
6719
c250f633c040 Update VS2012 solution - add missing files; fix some static analysis warnings
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6718
diff changeset
    35
#include <string.h>
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    36
#include <time.h>
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    37
6772
5229a117ef97 Added audio test suite; minor code cleanups in test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6756
diff changeset
    38
#include "SDL.h"
5229a117ef97 Added audio test suite; minor code cleanups in test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6756
diff changeset
    39
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    40
#include "SDL_test.h"
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
/*!
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    43
 * 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
    44
 *
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    45
 * 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
    46
 * 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
    47
 * 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
    48
 *
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    49
 * \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
    50
 *
6756
398073b195bb Refactor/fix test lib harness, assert and log component; add harness driver; port platform suite from GSOC code
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6719
diff changeset
    51
 * \return Ascii representation of the timestamp in localtime in the format '08/23/01 14:55:02'
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    52
 */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    53
char *SDLTest_TimestampToString(const time_t timestamp)
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    54
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    55
    time_t copy;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    56
    static char buffer[64];
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    57
    struct tm *local;
7405
5a9172b56b18 Attempt to fix a compiler warning on Haiku.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    58
    const char *fmt = "%x %X";
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    59
7405
5a9172b56b18 Attempt to fix a compiler warning on Haiku.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    60
    SDL_memset(buffer, 0, sizeof(buffer));
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    61
    copy = timestamp;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    62
    local = localtime(&copy);
7405
5a9172b56b18 Attempt to fix a compiler warning on Haiku.
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    63
    strftime(buffer, sizeof(buffer), fmt, local);
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    64
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    65
    return buffer;
6717
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
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
 * 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
    70
 */
6772
5229a117ef97 Added audio test suite; minor code cleanups in test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6756
diff changeset
    71
void SDLTest_Log(const char *fmt, ...)
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    72
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    73
    va_list list;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    74
    char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    75
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    76
    /* Print log message into a buffer */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    77
    SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    78
    va_start(list, fmt);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    79
    SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    80
    va_end(list);
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    81
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    82
    /* Log with timestamp and newline */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    83
    SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, " %s: %s", SDLTest_TimestampToString(time(0)), logMessage);
6717
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
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
 * 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
    88
 */
6772
5229a117ef97 Added audio test suite; minor code cleanups in test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6756
diff changeset
    89
void SDLTest_LogError(const char *fmt, ...)
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    90
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    91
    va_list list;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    92
    char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    93
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    94
    /* Print log message into a buffer */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    95
    SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    96
    va_start(list, fmt);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    97
    SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    98
    va_end(list);
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
    99
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   100
    /* Log with timestamp and newline */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   101
    SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s", SDLTest_TimestampToString(time(0)), logMessage);
6717
2acd95060548 Add log, assert and harness (partial) to test lib
Andreas Schiffler <aschiffler@ferzkopp.net>
parents:
diff changeset
   102
}