// force initialization (single-threaded environment)
Mutex* __initialization_mutex_initializer = &getInitializationMutex();
+static bool param_dumpErrors = utils::getConfigurationParameterBool("OPENCV_DUMP_ERRORS",
+#if defined(_DEBUG) || defined(__ANDROID__) || (defined(__GNUC__) && !defined(__EXCEPTIONS))
+ true
+#else
+ false
+#endif
+);
+
} // namespace cv
+#ifndef CV_ERROR_SET_TERMINATE_HANDLER // build config option
+# if defined(_WIN32)
+# define CV_ERROR_SET_TERMINATE_HANDLER 1
+# endif
+#endif
+#if defined(CV_ERROR_SET_TERMINATE_HANDLER) && !CV_ERROR_SET_TERMINATE_HANDLER
+# undef CV_ERROR_SET_TERMINATE_HANDLER
+#endif
+
#ifdef _MSC_VER
# if _MSC_VER >= 1700
# pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
# endif
#endif
+#ifdef CV_ERROR_SET_TERMINATE_HANDLER
+#include <exception> // std::set_terminate
+#include <cstdlib> // std::abort
+#endif
+
#if defined __ANDROID__ || defined __linux__ || defined __FreeBSD__ || defined __HAIKU__
# include <unistd.h>
# include <fcntl.h>
void Exception::formatMessage()
{
- if( func.size() > 0 )
- msg = format("OpenCV(%s) %s:%d: error: (%d) %s: %s in function %s\n", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str(), func.c_str());
+ size_t pos = err.find('\n');
+ bool multiline = pos != cv::String::npos;
+ if (multiline)
+ {
+ std::stringstream ss;
+ size_t prev_pos = 0;
+ while (pos != cv::String::npos)
+ {
+ ss << "> " << err.substr(prev_pos, pos - prev_pos) << std::endl;
+ prev_pos = pos + 1;
+ pos = err.find('\n', prev_pos);
+ }
+ ss << "> " << err.substr(prev_pos);
+ if (err[err.size() - 1] != '\n')
+ ss << std::endl;
+ err = ss.str();
+ }
+ if (func.size() > 0)
+ {
+ if (multiline)
+ msg = format("OpenCV(%s) %s:%d: error: (%d:%s) in function '%s'\n%s", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), func.c_str(), err.c_str());
+ else
+ msg = format("OpenCV(%s) %s:%d: error: (%d:%s) %s in function '%s'\n", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str(), func.c_str());
+ }
else
- msg = format("OpenCV(%s) %s:%d: error: (%d) %s: %s\n", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str());
+ {
+ msg = format("OpenCV(%s) %s:%d: error: (%d:%s) %s%s", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str(), multiline ? "" : "\n");
+ }
}
static const char* g_hwFeatureNames[CV_HARDWARE_MAX_FEATURE] = { NULL };
"******************************************************************\n");
fprintf(stderr, "\nRequired baseline features:\n");
checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]), true);
- CV_ErrorNoReturn(cv::Error::StsAssert, "Missing support for required CPU baseline features. Check OpenCV build configuration and required CPU/HW setup.");
+ CV_Error(cv::Error::StsAssert, "Missing support for required CPU baseline features. Check OpenCV build configuration and required CPU/HW setup.");
}
readSettings(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]));
return name ? String(name) : String();
}
+std::string getCPUFeaturesLine()
+{
+ const int features[] = { CV_CPU_BASELINE_FEATURES, CV_CPU_DISPATCH_FEATURES };
+ const int sz = sizeof(features) / sizeof(features[0]);
+ std::string result;
+ std::string prefix;
+ for (int i = 1; i < sz; ++i)
+ {
+ if (features[i] == 0)
+ {
+ prefix = "*";
+ continue;
+ }
+ if (i != 1) result.append(" ");
+ result.append(prefix);
+ result.append(getHWFeatureNameSafe(features[i]));
+ if (!checkHardwareSupport(features[i])) result.append("?");
+ }
+ return result;
+}
+
volatile bool useOptimizedFlag = true;
void setUseOptimized( bool flag )
int64 getCPUTickCount(void)
{
- int64 result = 0;
unsigned upper, lower, tmp;
__asm__ volatile(
"0: \n"
va_list va;
va_start(va, fmt);
int bsize = static_cast<int>(buf.size());
- int len = cv_vsnprintf((char *)buf, bsize, fmt, va);
+ int len = cv_vsnprintf(buf.data(), bsize, fmt, va);
va_end(va);
CV_Assert(len >= 0 && "Check format string for errors");
continue;
}
buf[bsize - 1] = 0;
- return String((char *)buf, len);
+ return String(buf.data(), len);
}
}
#endif
}
+static void dumpException(const Exception& exc)
+{
+ const char* errorStr = cvErrorStr(exc.code);
+ char buf[1 << 12];
+
+ cv_snprintf(buf, sizeof(buf),
+ "OpenCV(%s) Error: %s (%s) in %s, file %s, line %d",
+ CV_VERSION,
+ errorStr, exc.err.c_str(), exc.func.size() > 0 ?
+ exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line);
+#ifdef __ANDROID__
+ __android_log_print(ANDROID_LOG_ERROR, "cv::error()", "%s", buf);
+#else
+ fflush(stdout); fflush(stderr);
+ fprintf(stderr, "%s\n", buf);
+ fflush(stderr);
+#endif
+}
+
+#ifdef CV_ERROR_SET_TERMINATE_HANDLER
+static bool cv_terminate_handler_installed = false;
+static std::terminate_handler cv_old_terminate_handler;
+static cv::Exception cv_terminate_handler_exception;
+static bool param_setupTerminateHandler = utils::getConfigurationParameterBool("OPENCV_SETUP_TERMINATE_HANDLER", true);
+static void cv_terminate_handler() {
+ std::cerr << "OpenCV: terminate handler is called! The last OpenCV error is:\n";
+ dumpException(cv_terminate_handler_exception);
+ if (false /*cv_old_terminate_handler*/) // buggy behavior is observed with doubled "abort/retry/ignore" windows
+ cv_old_terminate_handler();
+ abort();
+}
+
+#endif
+
void error( const Exception& exc )
{
+#ifdef CV_ERROR_SET_TERMINATE_HANDLER
+ {
+ cv::AutoLock lock(getInitializationMutex());
+ if (!cv_terminate_handler_installed)
+ {
+ if (param_setupTerminateHandler)
+ cv_old_terminate_handler = std::set_terminate(cv_terminate_handler);
+ cv_terminate_handler_installed = true;
+ }
+ cv_terminate_handler_exception = exc;
+ }
+#endif
+
if (customErrorCallback != 0)
customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
exc.file.c_str(), exc.line, customErrorCallbackData);
- else
+ else if (param_dumpErrors)
{
- const char* errorStr = cvErrorStr(exc.code);
- char buf[1 << 12];
-
- cv_snprintf(buf, sizeof(buf),
- "OpenCV(%s) Error: %s (%s) in %s, file %s, line %d",
- CV_VERSION,
- errorStr, exc.err.c_str(), exc.func.size() > 0 ?
- exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line);
- fprintf( stderr, "%s\n", buf );
- fflush( stderr );
-# ifdef __ANDROID__
- __android_log_print(ANDROID_LOG_ERROR, "cv::error()", "%s", buf);
-# endif
+ dumpException(exc);
}
if(breakOnError)
{
return false;
}
- CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
+ CV_Error(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
}
return v * 1024 * 1024;
else if (suffixStr == "KB" || suffixStr == "Kb" || suffixStr == "kb")
return v * 1024;
- CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
+ CV_Error(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
}
cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue)