From 23f27d8dbe5f71aa6571b172953ead260fea49a5 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Mon, 17 Jul 2023 12:49:49 +0300 Subject: [PATCH] Use OpenCV logging instead of std::cerr. --- modules/core/src/ocl.cpp | 1 - modules/core/src/system.cpp | 6 +-- .../dnn/src/layers/max_unpooling_layer.cpp | 18 +++---- modules/dnn/src/net_impl.cpp | 16 +++--- modules/highgui/src/window_wayland.cpp | 2 +- modules/imgcodecs/src/grfmt_pxm.cpp | 6 +-- modules/imgcodecs/src/loadsave.cpp | 51 +++++++++---------- 7 files changed, 47 insertions(+), 53 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index bf562c3092..f93a7be3f1 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -51,7 +51,6 @@ #include #include #include -#include // std::cerr #include #if !(defined _MSC_VER) || (defined _MSC_VER && _MSC_VER > 1700) #include diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 7811ab72f0..44e1e04a03 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -2574,7 +2574,7 @@ public: ippStatus = ippGetCpuFeatures(&cpuFeatures, NULL); if(ippStatus < 0) { - std::cerr << "ERROR: IPP cannot detect CPU features, IPP was disabled " << std::endl; + CV_LOG_ERROR(NULL, "ERROR: IPP cannot detect CPU features, IPP was disabled"); useIPP = false; return; } @@ -2612,7 +2612,7 @@ public: if(env == "disabled") { - std::cerr << "WARNING: IPP was disabled by OPENCV_IPP environment variable" << std::endl; + CV_LOG_WARNING(NULL, "WARNING: IPP was disabled by OPENCV_IPP environment variable"); useIPP = false; } else if(env == "sse42") @@ -2626,7 +2626,7 @@ public: #endif #endif else - std::cerr << "ERROR: Improper value of OPENCV_IPP: " << env.c_str() << ". Correct values are: disabled, sse42, avx2, avx512 (Intel64 only)" << std::endl; + CV_LOG_ERROR(NULL, "ERROR: Improper value of OPENCV_IPP: " << env.c_str() << ". Correct values are: disabled, sse42, avx2, avx512 (Intel64 only)"); // Trim unsupported features ippFeatures &= cpuFeatures; diff --git a/modules/dnn/src/layers/max_unpooling_layer.cpp b/modules/dnn/src/layers/max_unpooling_layer.cpp index a44d25ce89..fd47c4c919 100644 --- a/modules/dnn/src/layers/max_unpooling_layer.cpp +++ b/modules/dnn/src/layers/max_unpooling_layer.cpp @@ -14,6 +14,7 @@ Implementation of Batch Normalization layer. #include "../op_cuda.hpp" #include "../op_halide.hpp" #include +#include #ifdef HAVE_CUDA #include "../cuda4dnn/primitives/max_unpooling.hpp" @@ -110,17 +111,12 @@ public: int index = idxptr[i_wh]; if (!(0 <= index && index < outPlaneTotal)) { - std::cerr - << "i_n=" << i_n << std::endl - << "i_c=" << i_c << std::endl - << "i_wh=" << i_wh << std::endl - << "index=" << index << std::endl - << "maxval=" << inptr[i_wh] << std::endl - << "outPlaneTotal=" << outPlaneTotal << std::endl - << "input.size=" << input.size << std::endl - << "indices.size=" << indices.size << std::endl - << "outBlob=" << outBlob.size << std::endl - ; + CV_LOG_ERROR(NULL, cv::format( + "i_n=%d\ni_c=%d\ni_wh=%d\nindex=%d\nmaxval=%lf\noutPlaneTotal=%d\n", + i_n, i_c, i_wh, index, inptr[i_wh], outPlaneTotal)); + CV_LOG_ERROR(NULL, "input.size=" << input.size); + CV_LOG_ERROR(NULL, "indices.size=" << indices.size); + CV_LOG_ERROR(NULL, "outBlob=" << outBlob.size); CV_Assert(0 <= index && index < outPlaneTotal); } outptr[index] = inptr[i_wh]; diff --git a/modules/dnn/src/net_impl.cpp b/modules/dnn/src/net_impl.cpp index c8341e4c6f..16ae6d7bfb 100644 --- a/modules/dnn/src/net_impl.cpp +++ b/modules/dnn/src/net_impl.cpp @@ -662,14 +662,14 @@ void Net::Impl::forwardLayer(LayerData& ld) m = u.getMat(ACCESS_READ); if (!checkRange(m)) { - std::cerr << "WARNING: NaN detected in layer output: id=" << ld.id << " name=" << layer->name << std::endl; - std::cerr << "output id=" << i << " output shape=" << shape(m) << std::endl; + CV_LOG_WARNING(NULL, "NaN detected in layer output: id=" << ld.id << " name=" << layer->name + << " output id=" << i << " output shape=" << shape(m)); fail = true; } else if (!checkRange(m, true, NULL, -1e6, 1e6)) { - std::cerr << "WARNING: Inf detected in layer output: id=" << ld.id << " name=" << layer->name << std::endl; - std::cerr << "output id=" << i << " output shape=" << shape(m) << std::endl; + CV_LOG_WARNING(NULL, "Inf detected in layer output: id=" << ld.id << " name=" << layer->name + << " output id=" << i << " output shape=" << shape(m)); fail = true; } } @@ -738,14 +738,14 @@ void Net::Impl::forwardLayer(LayerData& ld) const Mat& m = ld.outputBlobs[i]; if (!checkRange(m)) { - std::cerr << "WARNING: NaN detected in layer output: id=" << ld.id << " name=" << layer->name << std::endl; - std::cerr << "output id=" << i << " output shape=" << shape(m) << std::endl; + CV_LOG_WARNING(NULL, "NaN detected in layer output: " + << cv::format("id=%d name=%s output id=%zu output shape=", ld.id, layer->name.c_str(), i) << shape(m)); fail = true; } else if (!checkRange(m, true, NULL, -1e6, 1e6)) { - std::cerr << "WARNING: Inf detected in layer output: id=" << ld.id << " name=" << layer->name << std::endl; - std::cerr << "output id=" << i << " output shape=" << shape(m) << std::endl; + CV_LOG_WARNING(NULL, "Inf detected in layer output: " + << cv::format("id=%d name=%s output id=%zu output shape=", ld.id, layer->name.c_str(), i) << shape(m)); fail = true; } } diff --git a/modules/highgui/src/window_wayland.cpp b/modules/highgui/src/window_wayland.cpp index 69231c0072..0d7c9788a1 100644 --- a/modules/highgui/src/window_wayland.cpp +++ b/modules/highgui/src/window_wayland.cpp @@ -1055,7 +1055,7 @@ void cv_wl_keyboard::handle_kb_keymap(void *data, struct wl_keyboard *kb, uint32 } catch (std::exception &e) { if (keyboard->xkb_.keymap) xkb_keymap_unref(keyboard->xkb_.keymap); - std::cerr << "OpenCV Error: " << e.what() << std::endl; + CV_LOG_ERROR(NULL, "OpenCV Error: " << e.what()); } close(fd); diff --git a/modules/imgcodecs/src/grfmt_pxm.cpp b/modules/imgcodecs/src/grfmt_pxm.cpp index 8da2348728..76290c43de 100644 --- a/modules/imgcodecs/src/grfmt_pxm.cpp +++ b/modules/imgcodecs/src/grfmt_pxm.cpp @@ -43,7 +43,7 @@ #include "precomp.hpp" #include "utils.hpp" #include "grfmt_pxm.hpp" -#include +#include #ifdef HAVE_IMGCODEC_PXM @@ -191,7 +191,7 @@ bool PxMDecoder::readHeader() } catch (...) { - std::cerr << "PXM::readHeader(): unknown C++ exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "PXM::readHeader(): unknown C++ exception"); throw; } @@ -364,7 +364,7 @@ bool PxMDecoder::readData( Mat& img ) } catch (...) { - std::cerr << "PXM::readData(): unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "PXM::readData(): unknown exception"); throw; } diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index d0413c1ade..4ce490610e 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -437,12 +437,12 @@ imread_( const String& filename, int flags, Mat& mat ) } catch (const cv::Exception& e) { - std::cerr << "imread_('" << filename << "'): can't read header: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imread_('" << filename << "'): can't read header: " << e.what()); return 0; } catch (...) { - std::cerr << "imread_('" << filename << "'): can't read header: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imread_('" << filename << "'): can't read header: unknown exception"); return 0; } @@ -475,11 +475,11 @@ imread_( const String& filename, int flags, Mat& mat ) } catch (const cv::Exception& e) { - std::cerr << "imread_('" << filename << "'): can't read data: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imread_('" << filename << "'): can't read data: " << e.what()); } catch (...) { - std::cerr << "imread_('" << filename << "'): can't read data: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imread_('" << filename << "'): can't read data: unknown exception"); } if (!success) { @@ -542,12 +542,12 @@ imreadmulti_(const String& filename, int flags, std::vector& mats, int star } catch (const cv::Exception& e) { - std::cerr << "imreadmulti_('" << filename << "'): can't read header: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read header: " << e.what()); return 0; } catch (...) { - std::cerr << "imreadmulti_('" << filename << "'): can't read header: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read header: unknown exception"); return 0; } @@ -591,11 +591,11 @@ imreadmulti_(const String& filename, int flags, std::vector& mats, int star } catch (const cv::Exception& e) { - std::cerr << "imreadmulti_('" << filename << "'): can't read data: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read data: " << e.what()); } catch (...) { - std::cerr << "imreadmulti_('" << filename << "'): can't read data: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read data: unknown exception"); } if (!success) break; @@ -672,7 +672,7 @@ size_t imcount_(const String& filename, int flags) return collection.size(); } catch(cv::Exception const& e) { // Reading header or finding decoder for the filename is failed - std::cerr << "imcount_('" << filename << "'): can't read header or can't find decoder: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imcount_('" << filename << "'): can't read header or can't find decoder: " << e.what()); } return 0; } @@ -768,14 +768,13 @@ static bool imwrite_( const String& filename, const std::vector& img_vec, } catch (const cv::Exception& e) { - std::cerr << "imwrite_('" << filename << "'): can't write data: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imwrite_('" << filename << "'): can't write data: " << e.what()); } catch (...) { - std::cerr << "imwrite_('" << filename << "'): can't write data: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imwrite_('" << filename << "'): can't write data: unknown exception"); } - // CV_Assert( code ); return code; } @@ -851,11 +850,11 @@ imdecode_( const Mat& buf, int flags, Mat& mat ) } catch (const cv::Exception& e) { - std::cerr << "imdecode_('" << filename << "'): can't read header: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imdecode_('" << filename << "'): can't read header: " << e.what()); } catch (...) { - std::cerr << "imdecode_('" << filename << "'): can't read header: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imdecode_('" << filename << "'): can't read header: unknown exception"); } if (!success) { @@ -864,7 +863,7 @@ imdecode_( const Mat& buf, int flags, Mat& mat ) { if (0 != remove(filename.c_str())) { - std::cerr << "unable to remove temporary file:" << filename << std::endl << std::flush; + CV_LOG_WARNING(NULL, "unable to remove temporary file:" << filename); } } return 0; @@ -896,18 +895,18 @@ imdecode_( const Mat& buf, int flags, Mat& mat ) } catch (const cv::Exception& e) { - std::cerr << "imdecode_('" << filename << "'): can't read data: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imdecode_('" << filename << "'): can't read data: " << e.what()); } catch (...) { - std::cerr << "imdecode_('" << filename << "'): can't read data: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imdecode_('" << filename << "'): can't read data: unknown exception"); } if (!filename.empty()) { if (0 != remove(filename.c_str())) { - std::cerr << "unable to remove temporary file:" << filename << std::endl << std::flush; + CV_LOG_WARNING(NULL, "unable to remove temporary file: " << filename); } } @@ -1000,11 +999,11 @@ imdecodemulti_(const Mat& buf, int flags, std::vector& mats, int start, int } catch (const cv::Exception& e) { - std::cerr << "imreadmulti_('" << filename << "'): can't read header: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read header: " << e.what()); } catch (...) { - std::cerr << "imreadmulti_('" << filename << "'): can't read header: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read header: unknown exception"); } int current = start; @@ -1025,7 +1024,7 @@ imdecodemulti_(const Mat& buf, int flags, std::vector& mats, int start, int { if (0 != remove(filename.c_str())) { - std::cerr << "unable to remove temporary file:" << filename << std::endl << std::flush; + CV_LOG_WARNING(NULL, "unable to remove temporary file: " << filename); } } return 0; @@ -1060,11 +1059,11 @@ imdecodemulti_(const Mat& buf, int flags, std::vector& mats, int start, int } catch (const cv::Exception& e) { - std::cerr << "imreadmulti_('" << filename << "'): can't read data: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read data: " << e.what()); } catch (...) { - std::cerr << "imreadmulti_('" << filename << "'): can't read data: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "imreadmulti_('" << filename << "'): can't read data: unknown exception"); } if (!success) break; @@ -1087,7 +1086,7 @@ imdecodemulti_(const Mat& buf, int flags, std::vector& mats, int start, int { if (0 != remove(filename.c_str())) { - std::cerr << "unable to remove temporary file:" << filename << std::endl << std::flush; + CV_LOG_WARNING(NULL, "unable to remove temporary file: " << filename); } } @@ -1317,10 +1316,10 @@ Mat ImageCollection::Impl::readData() { success = true; } catch (const cv::Exception &e) { - std::cerr << "ImageCollection class: can't read data: " << e.what() << std::endl << std::flush; + CV_LOG_ERROR(NULL, "ImageCollection class: can't read data: " << e.what()); } catch (...) { - std::cerr << "ImageCollection class:: can't read data: unknown exception" << std::endl << std::flush; + CV_LOG_ERROR(NULL, "ImageCollection class:: can't read data: unknown exception"); } if (!success) return cv::Mat();