• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 
17 #if __ANDROID__
18 #	include <android/log.h>
19 #endif
20 
21 #if defined(__unix__)
22 #	define PTRACE
23 #	include <sys/ptrace.h>
24 #	include <sys/types.h>
25 #elif defined(_WIN32) || defined(_WIN64)
26 #	include <windows.h>
27 #elif defined(__APPLE__) || defined(__MACH__)
28 #	include <sys/sysctl.h>
29 #	include <unistd.h>
30 #endif
31 
32 #include <atomic>
33 #include <cstdarg>
34 #include <cstdio>
35 #include <string>
36 
37 #ifdef ERROR
38 #	undef ERROR  // b/127920555
39 #endif
40 
41 #ifndef SWIFTSHADER_LOGGING_LEVEL
42 #	define SWIFTSHADER_LOGGING_LEVEL Info
43 #endif
44 
45 namespace {
46 
47 enum class Level
48 {
49 	Verbose,
50 	Debug,
51 	Info,
52 	Warn,
53 	Error,
54 	Fatal,
55 	Disabled,
56 };
57 
58 #ifdef __ANDROID__
logv_android(Level level,const char * msg)59 void logv_android(Level level, const char *msg)
60 {
61 	switch(level)
62 	{
63 	case Level::Debug:
64 		__android_log_write(ANDROID_LOG_DEBUG, "SwiftShader", msg);
65 		break;
66 	case Level::Info:
67 		__android_log_write(ANDROID_LOG_INFO, "SwiftShader", msg);
68 		break;
69 	case Level::Warn:
70 		__android_log_write(ANDROID_LOG_WARN, "SwiftShader", msg);
71 		break;
72 	case Level::Error:
73 		__android_log_write(ANDROID_LOG_ERROR, "SwiftShader", msg);
74 		break;
75 	case Level::Fatal:
76 		__android_log_write(ANDROID_LOG_FATAL, "SwiftShader", msg);
77 		break;
78 	default:
79 		break;
80 	}
81 }
82 #else
logv_std(Level level,const char * msg)83 void logv_std(Level level, const char *msg)
84 {
85 	switch(level)
86 	{
87 	case Level::Debug:
88 	case Level::Info:
89 		fprintf(stdout, "%s", msg);
90 		break;
91 	case Level::Warn:
92 	case Level::Error:
93 	case Level::Fatal:
94 		fprintf(stderr, "%s", msg);
95 		break;
96 	default:
97 		break;
98 	}
99 }
100 #endif
101 
logv(Level level,const char * format,va_list args)102 void logv(Level level, const char *format, va_list args)
103 {
104 	if(static_cast<int>(level) >= static_cast<int>(Level::SWIFTSHADER_LOGGING_LEVEL))
105 	{
106 #ifndef SWIFTSHADER_DISABLE_TRACE
107 		char buffer[2048];
108 		vsnprintf(buffer, sizeof(buffer), format, args);
109 
110 #	if defined(__ANDROID__)
111 		logv_android(level, buffer);
112 #	elif defined(_WIN32)
113 		logv_std(level, buffer);
114 		::OutputDebugString(buffer);
115 #	else
116 		logv_std(level, buffer);
117 #	endif
118 	}
119 
120 	const Level traceToFileLevel = Level::Disabled;
121 	if(static_cast<int>(level) >= static_cast<int>(traceToFileLevel))
122 	{
123 		FILE *file = fopen(TRACE_OUTPUT_FILE, "a");
124 
125 		if(file)
126 		{
127 			vfprintf(file, format, args);
128 			fclose(file);
129 		}
130 	}
131 #endif  // SWIFTSHADER_DISABLE_TRACE
132 }
133 
134 }  // anonymous namespace
135 
136 namespace sw {
137 
trace(const char * format,...)138 void trace(const char *format, ...)
139 {
140 	va_list vararg;
141 	va_start(vararg, format);
142 	logv(Level::Debug, format, vararg);
143 	va_end(vararg);
144 }
145 
warn(const char * format,...)146 void warn(const char *format, ...)
147 {
148 	va_list vararg;
149 	va_start(vararg, format);
150 	logv(Level::Warn, format, vararg);
151 	va_end(vararg);
152 }
153 
abort(const char * format,...)154 void abort(const char *format, ...)
155 {
156 	va_list vararg;
157 
158 	va_start(vararg, format);
159 	logv(Level::Fatal, format, vararg);
160 	va_end(vararg);
161 
162 	::abort();
163 }
164 
165 }  // namespace sw
166