comparison util.c @ 879:a77670cd178d

Send info/warning/fatal messages to logcat on Android
author Michael Pavone <pavone@retrodev.com>
date Mon, 09 Nov 2015 21:26:25 -0800
parents 540cc4a7d626
children 9f149f0e98b7
comparison
equal deleted inserted replaced
878:9f1c76714f87 879:a77670cd178d
6 #include <stdarg.h> 6 #include <stdarg.h>
7 7
8 #include <sys/types.h> 8 #include <sys/types.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <unistd.h> 10 #include <unistd.h>
11
12 #ifdef __ANDROID__
13 #include <android/log.h>
14 #define info_puts(msg) __android_log_write(ANDROID_LOG_INFO, "BlastEm", msg)
15 #define warning_puts(msg) __android_log_write(ANDROID_LOG_WARN, "BlastEm", msg)
16 #define fatal_puts(msg) __android_log_write(ANDROID_LOG_FATAL, "BlastEm", msg)
17
18 #define info_printf(msg, args) __android_log_vprint(ANDROID_LOG_INFO, "BlastEm", msg, args)
19 #define warning_printf(msg, args) __android_log_vprint(ANDROID_LOG_WARN, "BlastEm", msg, args)
20 #define fatal_printf(msg, args) __android_log_vprint(ANDROID_LOG_FATAL, "BlastEm", msg, args)
21 #else
22 #define info_puts(msg) fputs(stdout, msg);
23 #define warning_puts(msg) fputs(stderr, msg);
24 #define fatal_puts(msg) fputs(stderr, msg);
25
26 #define info_printf(msg, args vprintf(msg, args)
27 #define warning_printf(msg, args vfprintf(stderr, msg, args)
28 #define fatal_printf(msg, args vfprintf(stderr, msg, args)
29 #endif
11 30
12 #include "blastem.h" //for headless global 31 #include "blastem.h" //for headless global
13 #include "render.h" //for render_errorbox 32 #include "render.h" //for render_errorbox
14 #include "util.h" 33 #include "util.h"
15 34
106 buf = malloc(actual); 125 buf = malloc(actual);
107 va_end(args); 126 va_end(args);
108 va_start(args, format); 127 va_start(args, format);
109 vsnprintf(buf, actual, format, args); 128 vsnprintf(buf, actual, format, args);
110 } 129 }
111 fputs(buf, stderr); 130 fatal_puts(buf);
112 render_errorbox("Fatal Error", buf); 131 render_errorbox("Fatal Error", buf);
113 free(buf); 132 free(buf);
114 } else { 133 } else {
115 vfprintf(stderr, format, args); 134 fatal_printf(format, args);
116 } 135 }
117 va_end(args); 136 va_end(args);
118 exit(1); 137 exit(1);
119 } 138 }
120 139
122 { 141 {
123 va_list args; 142 va_list args;
124 va_start(args, format); 143 va_start(args, format);
125 #ifndef _WIN32 144 #ifndef _WIN32
126 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) { 145 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) {
127 vfprintf(stderr, format, args); 146 warning_printf(format, args);
128 } else { 147 } else {
129 #endif 148 #endif
130 size_t size = strlen(format) * 2; 149 size_t size = strlen(format) * 2;
131 char *buf = malloc(size); 150 char *buf = malloc(size);
132 size_t actual = vsnprintf(buf, size, format, args); 151 size_t actual = vsnprintf(buf, size, format, args);
136 buf = malloc(actual); 155 buf = malloc(actual);
137 va_end(args); 156 va_end(args);
138 va_start(args, format); 157 va_start(args, format);
139 vsnprintf(buf, actual, format, args); 158 vsnprintf(buf, actual, format, args);
140 } 159 }
141 fputs(buf, stderr); 160 warning_puts(buf);
142 render_infobox("BlastEm Info", buf); 161 render_infobox("BlastEm Info", buf);
143 free(buf); 162 free(buf);
144 #ifndef _WIN32 163 #ifndef _WIN32
145 } 164 }
146 #endif 165 #endif
151 { 170 {
152 va_list args; 171 va_list args;
153 va_start(args, format); 172 va_start(args, format);
154 #ifndef _WIN32 173 #ifndef _WIN32
155 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) { 174 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) {
156 vprintf(format, args); 175 info_printf(format, args);
157 } else { 176 } else {
158 #endif 177 #endif
159 size_t size = strlen(format) * 2; 178 size_t size = strlen(format) * 2;
160 char *buf = malloc(size); 179 char *buf = malloc(size);
161 size_t actual = vsnprintf(buf, size, format, args); 180 size_t actual = vsnprintf(buf, size, format, args);
165 buf = malloc(actual); 184 buf = malloc(actual);
166 va_end(args); 185 va_end(args);
167 va_start(args, format); 186 va_start(args, format);
168 vsnprintf(buf, actual, format, args); 187 vsnprintf(buf, actual, format, args);
169 } 188 }
170 fputs(buf, stdout); 189 info_puts(buf);
171 render_infobox("BlastEm Info", buf); 190 render_infobox("BlastEm Info", buf);
172 free(buf); 191 free(buf);
173 #ifndef _WIN32 192 #ifndef _WIN32
174 } 193 }
175 #endif 194 #endif