blob: 0e870154be2402ec2fbec695344cc96875f11c74 [file] [log] [blame]
Nicolas Capensc07dc4b2018-08-06 14:20:45 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Debug.hpp"
16
Antonio Maioranof47a73a2019-12-13 16:21:01 -050017#include <cstdarg>
18#include <cstdio>
Ben Clayton713b8d32019-12-17 20:37:56 +000019#include <string>
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040020
Antonio Maioranof47a73a2019-12-13 16:21:01 -050021#if defined(_WIN32)
22# include <windows.h>
23#endif
24
Nicolas Capens157ba262019-12-10 17:49:14 -050025namespace rr {
Ben Claytoneb50d252019-04-15 13:50:01 -040026
27void tracev(const char *format, va_list args)
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040028{
Ben Claytoneb50d252019-04-15 13:50:01 -040029#ifndef RR_DISABLE_TRACE
Antonio Maioranof47a73a2019-12-13 16:21:01 -050030 const bool traceToDebugOut = false;
31 const bool traceToFile = false;
32
33 if(traceToDebugOut)
34 {
35 char buffer[2048];
36 vsnprintf(buffer, sizeof(buffer), format, args);
37# if defined(_WIN32)
38 ::OutputDebugString(buffer);
39# else
40 printf("%s", buffer);
41# endif
42 }
43
44 if(traceToFile)
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040045 {
Ben Claytoneb50d252019-04-15 13:50:01 -040046 FILE *file = fopen(TRACE_OUTPUT_FILE, "a");
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040047
48 if(file)
49 {
Ben Claytoneb50d252019-04-15 13:50:01 -040050 vfprintf(file, format, args);
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040051 fclose(file);
52 }
53 }
Ben Claytoneb50d252019-04-15 13:50:01 -040054#endif
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040055}
Ben Claytoneb50d252019-04-15 13:50:01 -040056
57void trace(const char *format, ...)
58{
59 va_list vararg;
60 va_start(vararg, format);
61 tracev(format, vararg);
62 va_end(vararg);
63}
64
65void warn(const char *format, ...)
66{
67 va_list vararg;
68 va_start(vararg, format);
69 tracev(format, vararg);
70 va_end(vararg);
71
72 va_start(vararg, format);
73 vfprintf(stderr, format, vararg);
74 va_end(vararg);
75}
76
77void abort(const char *format, ...)
78{
79 va_list vararg;
80
81 va_start(vararg, format);
82 tracev(format, vararg);
83 va_end(vararg);
84
85 va_start(vararg, format);
86 vfprintf(stderr, format, vararg);
87 va_end(vararg);
88
89 ::abort();
90}
91
Nicolas Capens157ba262019-12-10 17:49:14 -050092} // namespace rr