/** Print the given string to the given FILE, assuming the given output column width. Break words as needed to avoid words spilling past the last column. */ static void lPrintWithWordBreaks(const char *buf, int columnWidth, FILE *out) { #ifdef ISPC_IS_WINDOWS fputs(buf, out); #else int column = 0; assert(strchr(buf, ':') != NULL); int indent = strchr(buf, ':') - buf + 2; int width = std::max(40, columnWidth - 2); // Collect everything into a string and print it all at once at the end // -> try to avoid troubles with mangled error messages with // multi-threaded builds. std::string outStr; const char *msgPos = buf; while (true) { while (*msgPos != '\0' && isspace(*msgPos)) ++msgPos; if (*msgPos == '\0') break; const char *wordEnd = lFindWordEnd(msgPos); if (column > indent && column + wordEnd - msgPos > width) { // This word would overflow, so start a new line column = indent; outStr.push_back('\n'); // Indent to the same column as the ":" at the start of the // message for (int i = 0; i < indent; ++i) outStr.push_back(' '); } // Finally go and copy the word while (msgPos != wordEnd) { outStr.push_back(*msgPos++); ++column; } outStr.push_back(' '); ++column; } outStr.push_back('\n'); fputs(outStr.c_str(), out); #endif }
/** Print the given string to the given FILE, assuming the given output column width. Break words as needed to avoid words spilling past the last column. */ void PrintWithWordBreaks(const char *buf, int indent, int columnWidth, FILE *out) { #ifdef ISPC_IS_WINDOWS fputs(buf, out); fputs("\n", out); #else int column = 0; int width = std::max(40, columnWidth - 2); // Collect everything into a string and print it all at once at the end // -> try to avoid troubles with mangled error messages with // multi-threaded builds. std::string outStr; const char *msgPos = buf; while (true) { if (*msgPos == '\033') { // handle ANSI color escape: copy it to the output buffer // without charging for the characters it uses do { outStr.push_back(*msgPos++); } while (*msgPos != '\0' && *msgPos != 'm'); continue; } else if (*msgPos == '\n') { // Handle newlines cleanly column = indent; outStr.push_back('\n'); for (int i = 0; i < indent; ++i) outStr.push_back(' '); // Respect spaces after newlines ++msgPos; while (*msgPos == ' ') { outStr.push_back(' '); ++msgPos; } continue; } while (*msgPos != '\0' && isspace(*msgPos)) ++msgPos; if (*msgPos == '\0') break; const char *wordEnd = lFindWordEnd(msgPos); if (column > indent && column + wordEnd - msgPos > width) { // This word would overflow, so start a new line column = indent; outStr.push_back('\n'); // Indent to the same column as the ":" at the start of the // message. for (int i = 0; i < indent; ++i) outStr.push_back(' '); } // Finally go and copy the word while (msgPos != wordEnd) { outStr.push_back(*msgPos++); ++column; } outStr.push_back(' '); ++column; } outStr.push_back('\n'); fputs(outStr.c_str(), out); #endif }