upgrade SDL to version 2.0.8
[platform/upstream/SDL.git] / src / test / SDL_test_log.c
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2018 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 #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
30 # define _CRT_SECURE_NO_WARNINGS
31 #endif
32
33 #include "SDL_config.h"
34
35 #include <stdarg.h> /* va_list */
36 #include <stdio.h>
37 #include <string.h>
38 #include <time.h>
39
40 #include "SDL.h"
41
42 #include "SDL_test.h"
43
44 /* work around compiler warning on older GCCs. */
45 #if (defined(__GNUC__) && (__GNUC__ <= 2))
46 static size_t
47 strftime_gcc2_workaround(char *s, size_t max, const char *fmt, const struct tm *tm)
48 {
49     return strftime(s, max, fmt, tm);
50 }
51 #ifdef strftime
52 #undef strftime
53 #endif
54 #define strftime strftime_gcc2_workaround
55 #endif
56
57 /* !
58  * Converts unix timestamp to its ascii representation in localtime
59  *
60  * Note: Uses a static buffer internally, so the return value
61  * isn't valid after the next call of this function. If you
62  * want to retain the return value, make a copy of it.
63  *
64  * \param timestamp A Timestamp, i.e. time(0)
65  *
66  * \return Ascii representation of the timestamp in localtime in the format '08/23/01 14:55:02'
67  */
68 static char *SDLTest_TimestampToString(const time_t timestamp)
69 {
70     time_t copy;
71     static char buffer[64];
72     struct tm *local;
73
74     SDL_memset(buffer, 0, sizeof(buffer));
75     copy = timestamp;
76     local = localtime(&copy);
77     strftime(buffer, sizeof(buffer), "%x %X", local);
78
79     return buffer;
80 }
81
82 /*
83  * Prints given message with a timestamp in the TEST category and INFO priority.
84  */
85 void SDLTest_Log(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
86 {
87     va_list list;
88     char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
89
90     /* Print log message into a buffer */
91     SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
92     va_start(list, fmt);
93     SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
94     va_end(list);
95
96     //write log
97     char *message;
98     message = SDL_stack_alloc(char, SDLTEST_MAX_LOGMESSAGE_LENGTH);
99     if (!message) {
100         return;
101     }
102
103     size_t len = SDL_strlen(logMessage);
104     SDL_strlcpy(message, logMessage,len+1);
105
106     SDL_RWops *rwops = SDL_RWFromFile("SDL_Log_AllPrint.txt", "a+");
107     char *text;
108     text = SDL_stack_alloc(char, SDLTEST_MAX_LOGMESSAGE_LENGTH);
109     if(text)
110     {
111         SDL_snprintf(text, SDLTEST_MAX_LOGMESSAGE_LENGTH,  " INFO: %s: %s\n", SDLTest_TimestampToString(time(0)), message);
112         SDL_RWwrite(rwops, text, 1, SDL_strlen(text));
113         SDL_stack_free(text);
114     }
115
116     SDL_RWclose(rwops);
117     SDL_stack_free(message);
118
119     /* Log with timestamp and newline */
120     SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_INFO, " %s: %s", SDLTest_TimestampToString(time(0)), logMessage);
121 }
122
123 /*
124  * Prints given message with a timestamp in the TEST category and the ERROR priority.
125  */
126 void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
127 {
128     va_list list;
129     char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
130
131     /* Print log message into a buffer */
132     SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
133     va_start(list, fmt);
134     SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, fmt, list);
135     va_end(list);
136
137     //write log
138     char *message;
139     message = SDL_stack_alloc(char, SDLTEST_MAX_LOGMESSAGE_LENGTH);
140     if (!message) {
141         return;
142     }
143
144     size_t len = SDL_strlen(logMessage);
145     SDL_strlcpy(message, logMessage,len+1);
146
147     SDL_RWops *rwops = SDL_RWFromFile("SDL_Log_AllPrint.txt", "a+");
148     char *text;
149     text = SDL_stack_alloc(char, SDLTEST_MAX_LOGMESSAGE_LENGTH);
150     if(text)
151     {
152         SDL_snprintf(text, SDLTEST_MAX_LOGMESSAGE_LENGTH,  " ERROR: %s: %s\n", SDLTest_TimestampToString(time(0)), message);
153         SDL_RWwrite(rwops, text, 1, SDL_strlen(text));
154         SDL_stack_free(text);
155     }
156
157     SDL_RWclose(rwops);
158     SDL_stack_free(message);
159
160     /* Log with timestamp and newline */
161     SDL_LogMessage(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR, "%s: %s", SDLTest_TimestampToString(time(0)), logMessage);
162 }
163
164 /* vi: set ts=4 sw=4 expandtab: */