core(logger): strip opencv's modules base path#22924
Conversation
|
Full path to code indicates if OpenCV is build with CI (OpenCV Python, Android SDK, etc) or it's own build of OpenCV. I treat it as useful feature and prefer to stay as is. |
|
Logs with this PR: I vote for even shorter logs 😄 Same with cleaner log messages and log tag set: For reference - GStreamer logs (
I agree only partially, yes it helps to distinguish official opencv-python from manually built package, but that is all. Without version/platform/env details it is useless, we still need cmake/build logs and |
|
Currently there are 2 changes:
The last one could be reverted to provide full path with error messages. |
|
Vote for the last proposal from alalek with CV_Error exception. |
modules/core/src/logger.cpp
Outdated
| std::string file_s(file); | ||
| size_t pos1 = file_s.rfind('/'); | ||
| size_t pos2 = file_s.rfind('\\'); | ||
| size_t pos = 0; | ||
| if (pos2 == std::string::npos) | ||
| pos = pos1; | ||
| else if (pos1 == std::string::npos) | ||
| pos = pos2; | ||
| else | ||
| pos = std::max(pos1, pos2); | ||
| if (pos == std::string::npos || pos >= file_s.length() - 1) | ||
| return file; | ||
| return file + pos + 1; |
There was a problem hiding this comment.
What if we use C for this function? Something like:
const char * pos = std::max(strrchr(file, '/'), strrchr(file, '\\'));
if (!pos)
return file;
return pos + 1;
It will be inlined and evaluated at compile time unlike C++ (https://godbolt.org/z/8dxj8j46x).
There was a problem hiding this comment.
Replaced C++ verbose code.
There was a problem hiding this comment.
Interesting, for some reason only clang (5+) is able to evaluate this function at compile time, even the latest GCC adds an inlined function call: https://godbolt.org/z/v8PE7K85h While the variant with strrchr is consistently replaced with a precomputed pointer by older compilers such as GCC 4.6.4 and clang 3.6.
Also I just thought that in order to enable compile time evaluation we need to directly wrap __FILE__ macro here:
-O0) each log message will add this function call.
Perhaps I'm digging too deep here and the simple readable code is fine in the end.
asmorkalov
left a comment
There was a problem hiding this comment.
👍 Works well for me. Tested manually with Ubuntu 18.04.
CV_Error()changes.