1/*-------------------------------------------------------------------------
2 * drawElements Quality Program Helper Library
3 * -------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Debug output utilities.
22 *//*--------------------------------------------------------------------*/
23
24#include "qpDebugOut.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28
29typedef enum MessageType_e
30{
31	MESSAGETYPE_INFO	= 0,
32	MESSAGETYPE_ERROR,
33
34	MESSAGETYPE_LAST
35} MessageType;
36
37static void		printRaw		(MessageType type, const char* msg);
38static void		printFmt		(MessageType type, const char* fmt, va_list args);
39static void		exitProcess		(void);
40
41void qpPrint (const char* message)
42{
43	printRaw(MESSAGETYPE_INFO, message);
44}
45
46void qpPrintf (const char* format, ...)
47{
48	va_list args;
49	va_start(args, format);
50	printFmt(MESSAGETYPE_INFO, format, args);
51	va_end(args);
52}
53
54void qpPrintv (const char* format, va_list args)
55{
56	printFmt(MESSAGETYPE_INFO, format, args);
57}
58
59void qpDief (const char* format, ...)
60{
61	va_list args;
62	va_start(args, format);
63	printFmt(MESSAGETYPE_ERROR, format, args);
64	va_end(args);
65
66	exitProcess();
67}
68
69void qpDiev (const char* format, va_list args)
70{
71	printFmt(MESSAGETYPE_ERROR, format, args);
72	exitProcess();
73}
74
75/* print() implementation. */
76#if (DE_OS == DE_OS_ANDROID)
77
78#include <android/log.h>
79
80static android_LogPriority getLogPriority (MessageType type)
81{
82	switch (type)
83	{
84		case MESSAGETYPE_INFO:	return ANDROID_LOG_INFO;
85		case MESSAGETYPE_ERROR:	return ANDROID_LOG_FATAL;
86		default:				return ANDROID_LOG_DEBUG;
87	}
88}
89
90void printRaw (MessageType type, const char* message)
91{
92	__android_log_write(getLogPriority(type), "dEQP", message);
93}
94
95void printFmt (MessageType type, const char* format, va_list args)
96{
97	__android_log_vprint(getLogPriority(type), "dEQP", format, args);
98}
99
100#else
101
102static FILE* getOutFile (MessageType type)
103{
104	if (type == MESSAGETYPE_ERROR)
105		return stderr;
106	else
107		return stdout;
108}
109
110void printRaw (MessageType type, const char* message)
111{
112	FILE* out = getOutFile(type);
113
114	if (type == MESSAGETYPE_ERROR)
115		fprintf(out, "FATAL ERROR: ");
116
117	fputs(message, out);
118
119	if (type == MESSAGETYPE_ERROR)
120	{
121		putc('\n', out);
122		fflush(out);
123	}
124}
125
126void printFmt (MessageType type, const char* format, va_list args)
127{
128	FILE* out = getOutFile(type);
129
130	if (type == MESSAGETYPE_ERROR)
131		fprintf(out, "FATAL ERROR: ");
132
133	vfprintf(out, format, args);
134
135	if (type == MESSAGETYPE_ERROR)
136	{
137		putc('\n', out);
138		fflush(out);
139	}
140}
141
142#endif
143
144/* exitProcess() implementation. */
145#if (DE_OS == DE_OS_WIN32)
146
147#define NOMINMAX
148#define VC_EXTRALEAN
149#define WIN32_LEAN_AND_MEAN
150#include <windows.h>
151
152static void exitProcess (void)
153{
154	/* Some API implementations register atexit() functions that may hang.
155	   By using TerminateProcess() we can avoid calling any potentially hanging exit routines. */
156	HANDLE curProc = GetCurrentProcess();
157	TerminateProcess(curProc, -1);
158}
159
160#elif (DE_OS == DE_OS_IOS)
161
162#include "deThread.h"
163
164static void exitProcess (void)
165{
166	/* Since tests are in the same process as execserver, we want to give it
167	   a chance to stream complete log data before terminating. */
168	deSleep(5000);
169	exit(-1);
170}
171
172#else
173
174static void exitProcess (void)
175{
176	exit(-1);
177}
178
179#endif
180