SDL_test_log.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. Used by the test framework and test cases.
  20. */
  21. /* quiet windows compiler warnings */
  22. #define _CRT_SECURE_NO_WARNINGS
  23. #include "SDL_config.h"
  24. #include <stdarg.h> /* va_list */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include "SDL.h"
  29. #include "SDL_test.h"
  30. /* !
  31. * Converts unix timestamp to its ascii representation in localtime
  32. *
  33. * Note: Uses a static buffer internally, so the return value
  34. * isn't valid after the next call of this function. If you
  35. * want to retain the return value, make a copy of it.
  36. *
  37. * \param timestamp A Timestamp, i.e. time(0)
  38. *
  39. * \return Ascii representation of the timestamp in localtime in the format '08/23/01 14:55:02'
  40. */
  41. char *SDLTest_TimestampToString(const time_t timestamp)
  42. {
  43. time_t copy;
  44. static char buffer[64];
  45. struct tm *local;
  46. const char *fmt = "%x %X";
  47. SDL_memset(buffer, 0, sizeof(buffer));
  48. copy = timestamp;
  49. local = localtime(&copy);
  50. strftime(buffer, sizeof(buffer), fmt, local);
  51. return buffer;
  52. }
  53. /*
  54. * Prints given message with a timestamp in the TEST category and INFO priority.
  55. */
  56. void SDLTest_Log(const char *fmt, ...)
  57. {
  58. va_list list;
  59. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  60. /* Print log message into a buffer */
  61. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  62. va_start(list, fmt);
  63. SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
  64. va_end(list);
  65. /* Log with timestamp and newline */
  66. SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, " %s: %s", SDLTest_TimestampToString(time(0)), logMessage);
  67. }
  68. /*
  69. * Prints given message with a timestamp in the TEST category and the ERROR priority.
  70. */
  71. void SDLTest_LogError(const char *fmt, ...)
  72. {
  73. va_list list;
  74. char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
  75. /* Print log message into a buffer */
  76. SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
  77. va_start(list, fmt);
  78. SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
  79. va_end(list);
  80. /* Log with timestamp and newline */
  81. SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s", SDLTest_TimestampToString(time(0)), logMessage);
  82. }