13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Helper Library
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Debug output utilities.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "qpDebugOut.h"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
266492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#include "qpCrashHandler.h" /*!< for QP_USE_SIGNAL_HANDLER */
276492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdio.h>
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdlib.h>
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef enum MessageType_e
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MESSAGETYPE_INFO	= 0,
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MESSAGETYPE_ERROR,
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MESSAGETYPE_LAST
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} MessageType;
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void		printRaw		(MessageType type, const char* msg);
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void		printFmt		(MessageType type, const char* fmt, va_list args);
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void		exitProcess		(void);
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid qpPrint (const char* message)
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printRaw(MESSAGETYPE_INFO, message);
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid qpPrintf (const char* format, ...)
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	va_list args;
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	va_start(args, format);
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printFmt(MESSAGETYPE_INFO, format, args);
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	va_end(args);
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid qpPrintv (const char* format, va_list args)
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printFmt(MESSAGETYPE_INFO, format, args);
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid qpDief (const char* format, ...)
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	va_list args;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	va_start(args, format);
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printFmt(MESSAGETYPE_ERROR, format, args);
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	va_end(args);
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	exitProcess();
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid qpDiev (const char* format, va_list args)
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printFmt(MESSAGETYPE_ERROR, format, args);
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	exitProcess();
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* print() implementation. */
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_OS == DE_OS_ANDROID)
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <android/log.h>
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic android_LogPriority getLogPriority (MessageType type)
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	switch (type)
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case MESSAGETYPE_INFO:	return ANDROID_LOG_INFO;
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case MESSAGETYPE_ERROR:	return ANDROID_LOG_FATAL;
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		default:				return ANDROID_LOG_DEBUG;
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid printRaw (MessageType type, const char* message)
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	__android_log_write(getLogPriority(type), "dEQP", message);
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid printFmt (MessageType type, const char* format, va_list args)
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	__android_log_vprint(getLogPriority(type), "dEQP", format, args);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic FILE* getOutFile (MessageType type)
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (type == MESSAGETYPE_ERROR)
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return stderr;
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return stdout;
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid printRaw (MessageType type, const char* message)
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	FILE* out = getOutFile(type);
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (type == MESSAGETYPE_ERROR)
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		fprintf(out, "FATAL ERROR: ");
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	fputs(message, out);
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (type == MESSAGETYPE_ERROR)
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		putc('\n', out);
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		fflush(out);
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid printFmt (MessageType type, const char* format, va_list args)
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	FILE* out = getOutFile(type);
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (type == MESSAGETYPE_ERROR)
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		fprintf(out, "FATAL ERROR: ");
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vfprintf(out, format, args);
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (type == MESSAGETYPE_ERROR)
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		putc('\n', out);
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		fflush(out);
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* exitProcess() implementation. */
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_OS == DE_OS_WIN32)
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define NOMINMAX
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define VC_EXTRALEAN
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define WIN32_LEAN_AND_MEAN
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <windows.h>
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void exitProcess (void)
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	/* Some API implementations register atexit() functions that may hang.
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	   By using TerminateProcess() we can avoid calling any potentially hanging exit routines. */
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HANDLE curProc = GetCurrentProcess();
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TerminateProcess(curProc, -1);
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1626492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#else
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1646492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#if (DE_OS == DE_OS_IOS)
1656492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#	include "deThread.h"	/*!< for deSleep() */
1666492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#endif
1676492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry
1686492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#if defined(QP_USE_SIGNAL_HANDLER)
1696492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#	include <signal.h>
1706492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#endif
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void exitProcess (void)
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1746492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#if (DE_OS == DE_OS_IOS)
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	/* Since tests are in the same process as execserver, we want to give it
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	   a chance to stream complete log data before terminating. */
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deSleep(5000);
1786492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#endif
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1806492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#if defined(QP_USE_SIGNAL_HANDLER)
1816492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	/* QP_USE_SIGNAL_HANDLER defined, this means this function could have
1826492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	   been called from a signal handler. Calling exit() inside a signal
1836492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	   handler is not safe. */
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1856492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	/* Flush all open FILES */
1866492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	fflush(DE_NULL);
1876492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry
1886492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	/* Kill without calling any _at_exit handlers as those might hang */
1896492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry	raise(SIGKILL);
1906492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#else
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	exit(-1);
1926492654d213236b394b22c71dd77827aa100a2fbJarkko Pöyry#endif
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
196