00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "stdafx.h"
00027 #include "progressreporter.h"
00028
00029
00030 #include <stdarg.h>
00031
00032
00033 #define PBRT_ERROR_IGNORE 0
00034 #define PBRT_ERROR_CONTINUE 1
00035 #define PBRT_ERROR_ABORT 2
00036
00037 const char *findWordEnd(const char *buf) {
00038 while (*buf != '\0' && !isspace(*buf))
00039 ++buf;
00040 return buf;
00041 }
00042
00043
00044 static void processError(const char *format, va_list args,
00045 const char *message, int disposition) {
00046 #if !defined(PBRT_IS_WINDOWS)
00047 char *errorBuf;
00048 if (vasprintf(&errorBuf, format, args) == -1) {
00049 fprintf(stderr, "vasprintf() unable to allocate memory!\n");
00050 abort();
00051 }
00052 #else
00053 char errorBuf[2048];
00054 vsnprintf_s(errorBuf, sizeof(errorBuf), _TRUNCATE, format, args);
00055 #endif
00056
00057 if (disposition == PBRT_ERROR_IGNORE)
00058 return;
00059 else {
00060
00061
00062 extern int line_num;
00063 int column = 0;
00064 int width = max(20, TerminalWidth() - 2);
00065 if (line_num != 0) {
00066 extern string current_file;
00067 column += fprintf(stderr, "%s(%d): ", current_file.c_str(), line_num);
00068 }
00069 fputs(message, stderr);
00070 fputs(": ", stderr);
00071 column += strlen(message) + 2;
00072 const char *msgPos = errorBuf;
00073 while (true) {
00074 while (*msgPos != '\0' && isspace(*msgPos))
00075 ++msgPos;
00076 if (*msgPos == '\0')
00077 break;
00078
00079 const char *wordEnd = findWordEnd(msgPos);
00080 if (column + wordEnd - msgPos > width)
00081 column = fprintf(stderr, "\n ");
00082 while (msgPos != wordEnd) {
00083 fputc(*msgPos++, stderr);
00084 ++column;
00085 }
00086 fputc(' ', stderr);
00087 ++column;
00088 }
00089 fputs("\n", stderr);
00090 }
00091 if (disposition == PBRT_ERROR_ABORT) {
00092 #if defined(PBRT_IS_WINDOWS)
00093 __debugbreak();
00094 #else
00095 abort();
00096 #endif
00097 }
00098 #if !defined(PBRT_IS_WINDOWS)
00099 free(errorBuf);
00100 #endif
00101 }
00102
00103
00104 void Info(const char *format, ...) {
00105 if (!PbrtOptions.verbose || PbrtOptions.quiet) return;
00106 va_list args;
00107 va_start(args, format);
00108 processError(format, args, "Notice", PBRT_ERROR_CONTINUE);
00109 va_end(args);
00110 }
00111
00112
00113 void Warning(const char *format, ...) {
00114 if (PbrtOptions.quiet) return;
00115 va_list args;
00116 va_start(args, format);
00117 processError(format, args, "Warning", PBRT_ERROR_CONTINUE);
00118 va_end(args);
00119 }
00120
00121
00122 void Error(const char *format, ...) {
00123 va_list args;
00124 va_start(args, format);
00125 processError(format, args, "Error", PBRT_ERROR_CONTINUE);
00126 va_end(args);
00127 }
00128
00129
00130 void Severe(const char *format, ...) {
00131 va_list args;
00132 va_start(args, format);
00133 processError(format, args, "Fatal Error", PBRT_ERROR_ABORT);
00134 va_end(args);
00135 }
00136
00137