添加项目文件。

This commit is contained in:
CaiXiang
2024-09-25 09:43:03 +08:00
parent f5c6245902
commit 3e82af9e90
730 changed files with 350436 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_CORE_ALLOCATOR_STATS_HPP
#define OPENCV_CORE_ALLOCATOR_STATS_HPP
#include "../cvdef.h"
namespace cv { namespace utils {
class AllocatorStatisticsInterface
{
protected:
AllocatorStatisticsInterface() {}
virtual ~AllocatorStatisticsInterface() {}
public:
virtual uint64_t getCurrentUsage() const = 0;
virtual uint64_t getTotalUsage() const = 0;
virtual uint64_t getNumberOfAllocations() const = 0;
virtual uint64_t getPeakUsage() const = 0;
/** set peak usage = current usage */
virtual void resetPeakUsage() = 0;
};
}} // namespace
#endif // OPENCV_CORE_ALLOCATOR_STATS_HPP

View File

@@ -0,0 +1,158 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
#define OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP
#include "./allocator_stats.hpp"
//#define OPENCV_DISABLE_ALLOCATOR_STATS
#ifdef CV_CXX11
#include <atomic>
#ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
#if defined(__GNUC__) && (\
(defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4) || \
(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && !defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)) \
)
#define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE int
#endif
#endif
#ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
#define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE long long
#endif
#else // CV_CXX11
#ifndef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE
#define OPENCV_ALLOCATOR_STATS_COUNTER_TYPE int // CV_XADD supports int only
#endif
#endif // CV_CXX11
namespace cv { namespace utils {
#ifdef CV__ALLOCATOR_STATS_LOG
namespace {
#endif
class AllocatorStatistics : public AllocatorStatisticsInterface
{
#ifdef OPENCV_DISABLE_ALLOCATOR_STATS
public:
AllocatorStatistics() {}
~AllocatorStatistics() CV_OVERRIDE {}
uint64_t getCurrentUsage() const CV_OVERRIDE { return 0; }
uint64_t getTotalUsage() const CV_OVERRIDE { return 0; }
uint64_t getNumberOfAllocations() const CV_OVERRIDE { return 0; }
uint64_t getPeakUsage() const CV_OVERRIDE { return 0; }
/** set peak usage = current usage */
void resetPeakUsage() CV_OVERRIDE {};
void onAllocate(size_t /*sz*/) {}
void onFree(size_t /*sz*/) {}
#elif defined(CV_CXX11)
protected:
typedef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE counter_t;
std::atomic<counter_t> curr, total, total_allocs, peak;
public:
AllocatorStatistics() {}
~AllocatorStatistics() CV_OVERRIDE {}
uint64_t getCurrentUsage() const CV_OVERRIDE { return (uint64_t)curr.load(); }
uint64_t getTotalUsage() const CV_OVERRIDE { return (uint64_t)total.load(); }
uint64_t getNumberOfAllocations() const CV_OVERRIDE { return (uint64_t)total_allocs.load(); }
uint64_t getPeakUsage() const CV_OVERRIDE { return (uint64_t)peak.load(); }
/** set peak usage = current usage */
void resetPeakUsage() CV_OVERRIDE { peak.store(curr.load()); }
// Controller interface
void onAllocate(size_t sz)
{
#ifdef CV__ALLOCATOR_STATS_LOG
CV__ALLOCATOR_STATS_LOG(cv::format("allocate: %lld (curr=%lld)", (long long int)sz, (long long int)curr.load()));
#endif
counter_t new_curr = curr.fetch_add((counter_t)sz) + (counter_t)sz;
// peak = std::max((uint64_t)peak, new_curr);
auto prev_peak = peak.load();
while (prev_peak < new_curr)
{
if (peak.compare_exchange_weak(prev_peak, new_curr))
break;
}
// end of peak = max(...)
total += (counter_t)sz;
total_allocs++;
}
void onFree(size_t sz)
{
#ifdef CV__ALLOCATOR_STATS_LOG
CV__ALLOCATOR_STATS_LOG(cv::format("free: %lld (curr=%lld)", (long long int)sz, (long long int)curr.load()));
#endif
curr -= (counter_t)sz;
}
#else // non C++11
protected:
typedef OPENCV_ALLOCATOR_STATS_COUNTER_TYPE counter_t;
volatile counter_t curr, total, total_allocs, peak; // overflow is possible, CV_XADD operates with 'int' only
public:
AllocatorStatistics()
: curr(0), total(0), total_allocs(0), peak(0)
{}
~AllocatorStatistics() CV_OVERRIDE {}
uint64_t getCurrentUsage() const CV_OVERRIDE { return (uint64_t)curr; }
uint64_t getTotalUsage() const CV_OVERRIDE { return (uint64_t)total; }
uint64_t getNumberOfAllocations() const CV_OVERRIDE { return (uint64_t)total_allocs; }
uint64_t getPeakUsage() const CV_OVERRIDE { return (uint64_t)peak; }
void resetPeakUsage() CV_OVERRIDE { peak = curr; }
// Controller interface
void onAllocate(size_t sz)
{
#ifdef CV__ALLOCATOR_STATS_LOG
CV__ALLOCATOR_STATS_LOG(cv::format("allocate: %lld (curr=%lld)", (long long int)sz, (long long int)curr));
#endif
counter_t new_curr = (counter_t)CV_XADD(&curr, (counter_t)sz) + (counter_t)sz;
peak = std::max((counter_t)peak, new_curr); // non-thread safe
//CV_XADD(&total, (uint64_t)sz); // overflow with int, non-reliable...
total += sz;
CV_XADD(&total_allocs, (counter_t)1);
}
void onFree(size_t sz)
{
#ifdef CV__ALLOCATOR_STATS_LOG
CV__ALLOCATOR_STATS_LOG(cv::format("free: %lld (curr=%lld)", (long long int)sz, (long long int)curr));
#endif
CV_XADD(&curr, (counter_t)-sz);
}
#endif
};
#ifdef CV__ALLOCATOR_STATS_LOG
} // namespace
#endif
}} // namespace
#endif // OPENCV_CORE_ALLOCATOR_STATS_IMPL_HPP

View File

@@ -0,0 +1,82 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_UTILS_FILESYSTEM_HPP
#define OPENCV_UTILS_FILESYSTEM_HPP
namespace cv { namespace utils { namespace fs {
CV_EXPORTS bool exists(const cv::String& path);
CV_EXPORTS bool isDirectory(const cv::String& path);
CV_EXPORTS void remove_all(const cv::String& path);
CV_EXPORTS cv::String getcwd();
/** @brief Converts path p to a canonical absolute path
* Symlinks are processed if there is support for them on running platform.
*
* @param path input path. Target file/directory should exist.
*/
CV_EXPORTS cv::String canonical(const cv::String& path);
/** Join path components */
CV_EXPORTS cv::String join(const cv::String& base, const cv::String& path);
/** Get parent directory */
CV_EXPORTS cv::String getParent(const cv::String &path);
CV_EXPORTS std::wstring getParent(const std::wstring& path);
/**
* Generate a list of all files that match the globbing pattern.
*
* Result entries are prefixed by base directory path.
*
* @param directory base directory
* @param pattern filter pattern (based on '*'/'?' symbols). Use empty string to disable filtering and return all results
* @param[out] result result of globing.
* @param recursive scan nested directories too
* @param includeDirectories include directories into results list
*/
CV_EXPORTS void glob(const cv::String& directory, const cv::String& pattern,
CV_OUT std::vector<cv::String>& result,
bool recursive = false, bool includeDirectories = false);
/**
* Generate a list of all files that match the globbing pattern.
*
* @param directory base directory
* @param pattern filter pattern (based on '*'/'?' symbols). Use empty string to disable filtering and return all results
* @param[out] result globbing result with relative paths from base directory
* @param recursive scan nested directories too
* @param includeDirectories include directories into results list
*/
CV_EXPORTS void glob_relative(const cv::String& directory, const cv::String& pattern,
CV_OUT std::vector<cv::String>& result,
bool recursive = false, bool includeDirectories = false);
CV_EXPORTS bool createDirectory(const cv::String& path);
CV_EXPORTS bool createDirectories(const cv::String& path);
#ifdef __OPENCV_BUILD
// TODO
//CV_EXPORTS cv::String getTempDirectory();
/**
* @brief Returns directory to store OpenCV cache files
* Create sub-directory in common OpenCV cache directory if it doesn't exist.
* @param sub_directory_name name of sub-directory. NULL or "" value asks to return root cache directory.
* @param configuration_name optional name of configuration parameter name which overrides default behavior.
* @return Path to cache directory. Returns empty string if cache directories support is not available. Returns "disabled" if cache disabled by user.
*/
CV_EXPORTS cv::String getCacheDirectory(const char* sub_directory_name, const char* configuration_name = NULL);
#endif
}}} // namespace
#endif // OPENCV_UTILS_FILESYSTEM_HPP

View File

@@ -0,0 +1,125 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_UTILS_INSTR_HPP
#define OPENCV_UTILS_INSTR_HPP
#include <opencv2/core/utility.hpp>
#include <opencv2/core/utils/tls.hpp>
namespace cv {
//! @addtogroup core_utils
//! @{
#ifdef CV_COLLECT_IMPL_DATA
CV_EXPORTS void setImpl(int flags); // set implementation flags and reset storage arrays
CV_EXPORTS void addImpl(int flag, const char* func = 0); // add implementation and function name to storage arrays
// Get stored implementation flags and functions names arrays
// Each implementation entry correspond to function name entry, so you can find which implementation was executed in which function
CV_EXPORTS int getImpl(std::vector<int> &impl, std::vector<String> &funName);
CV_EXPORTS bool useCollection(); // return implementation collection state
CV_EXPORTS void setUseCollection(bool flag); // set implementation collection state
#define CV_IMPL_PLAIN 0x01 // native CPU OpenCV implementation
#define CV_IMPL_OCL 0x02 // OpenCL implementation
#define CV_IMPL_IPP 0x04 // IPP implementation
#define CV_IMPL_MT 0x10 // multithreaded implementation
#undef CV_IMPL_ADD
#define CV_IMPL_ADD(impl) \
if(cv::useCollection()) \
{ \
cv::addImpl(impl, CV_Func); \
}
#endif
// Instrumentation external interface
namespace instr
{
#if !defined OPENCV_ABI_CHECK
enum TYPE
{
TYPE_GENERAL = 0, // OpenCV API function, e.g. exported function
TYPE_MARKER, // Information marker
TYPE_WRAPPER, // Wrapper function for implementation
TYPE_FUN, // Simple function call
};
enum IMPL
{
IMPL_PLAIN = 0,
IMPL_IPP,
IMPL_OPENCL,
};
struct NodeDataTls
{
NodeDataTls()
{
m_ticksTotal = 0;
}
uint64 m_ticksTotal;
};
class CV_EXPORTS NodeData
{
public:
NodeData(const char* funName = 0, const char* fileName = NULL, int lineNum = 0, void* retAddress = NULL, bool alwaysExpand = false, cv::instr::TYPE instrType = TYPE_GENERAL, cv::instr::IMPL implType = IMPL_PLAIN);
NodeData(NodeData &ref);
~NodeData();
NodeData& operator=(const NodeData&);
cv::String m_funName;
cv::instr::TYPE m_instrType;
cv::instr::IMPL m_implType;
const char* m_fileName;
int m_lineNum;
void* m_retAddress;
bool m_alwaysExpand;
bool m_funError;
volatile int m_counter;
volatile uint64 m_ticksTotal;
TLSDataAccumulator<NodeDataTls> m_tls;
int m_threads;
// No synchronization
double getTotalMs() const { return ((double)m_ticksTotal / cv::getTickFrequency()) * 1000; }
double getMeanMs() const { return (((double)m_ticksTotal/m_counter) / cv::getTickFrequency()) * 1000; }
};
bool operator==(const NodeData& lhs, const NodeData& rhs);
typedef Node<NodeData> InstrNode;
CV_EXPORTS InstrNode* getTrace();
#endif // !defined OPENCV_ABI_CHECK
CV_EXPORTS bool useInstrumentation();
CV_EXPORTS void setUseInstrumentation(bool flag);
CV_EXPORTS void resetTrace();
enum FLAGS
{
FLAGS_NONE = 0,
FLAGS_MAPPING = 0x01,
FLAGS_EXPAND_SAME_NAMES = 0x02,
};
CV_EXPORTS void setFlags(FLAGS modeFlags);
static inline void setFlags(int modeFlags) { setFlags((FLAGS)modeFlags); }
CV_EXPORTS FLAGS getFlags();
} // namespace instr
//! @}
} // namespace
#endif // OPENCV_UTILS_TLS_HPP

View File

@@ -0,0 +1,42 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_LOGGER_DEFINES_HPP
#define OPENCV_LOGGER_DEFINES_HPP
//! @addtogroup core_logging
//! @{
// Supported logging levels and their semantic
#define CV_LOG_LEVEL_SILENT 0 //!< for using in setLogLevel() call
#define CV_LOG_LEVEL_FATAL 1 //!< Fatal (critical) error (unrecoverable internal error)
#define CV_LOG_LEVEL_ERROR 2 //!< Error message
#define CV_LOG_LEVEL_WARN 3 //!< Warning message
#define CV_LOG_LEVEL_INFO 4 //!< Info message
#define CV_LOG_LEVEL_DEBUG 5 //!< Debug message. Disabled in the "Release" build.
#define CV_LOG_LEVEL_VERBOSE 6 //!< Verbose (trace) messages. Requires verbosity level. Disabled in the "Release" build.
namespace cv {
namespace utils {
namespace logging {
//! Supported logging levels and their semantic
enum LogLevel {
LOG_LEVEL_SILENT = 0, //!< for using in setLogVevel() call
LOG_LEVEL_FATAL = 1, //!< Fatal (critical) error (unrecoverable internal error)
LOG_LEVEL_ERROR = 2, //!< Error message
LOG_LEVEL_WARNING = 3, //!< Warning message
LOG_LEVEL_INFO = 4, //!< Info message
LOG_LEVEL_DEBUG = 5, //!< Debug message. Disabled in the "Release" build.
LOG_LEVEL_VERBOSE = 6, //!< Verbose (trace) messages. Requires verbosity level. Disabled in the "Release" build.
#ifndef CV_DOXYGEN
ENUM_LOG_LEVEL_FORCE_INT = INT_MAX
#endif
};
}}} // namespace
//! @}
#endif // OPENCV_LOGGER_DEFINES_HPP

View File

@@ -0,0 +1,218 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_LOGGER_HPP
#define OPENCV_LOGGER_HPP
#include <iostream>
#include <sstream>
#include <limits.h> // INT_MAX
#include "logger.defines.hpp"
#include "logtag.hpp"
namespace cv {
namespace utils {
namespace logging {
//! @addtogroup core_logging
//! @{
/** Set global logging level
@return previous logging level
*/
CV_EXPORTS LogLevel setLogLevel(LogLevel logLevel);
/** Get global logging level */
CV_EXPORTS LogLevel getLogLevel();
CV_EXPORTS void registerLogTag(cv::utils::logging::LogTag* plogtag);
CV_EXPORTS void setLogTagLevel(const char* tag, cv::utils::logging::LogLevel level);
CV_EXPORTS cv::utils::logging::LogLevel getLogTagLevel(const char* tag);
namespace internal {
/** Get global log tag */
CV_EXPORTS cv::utils::logging::LogTag* getGlobalLogTag();
/** Write log message */
CV_EXPORTS void writeLogMessage(LogLevel logLevel, const char* message);
/** Write log message */
CV_EXPORTS void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message);
} // namespace
struct LogTagAuto
: public LogTag
{
inline LogTagAuto(const char* _name, LogLevel _level)
: LogTag(_name, _level)
{
registerLogTag(this);
}
};
/**
* \def CV_LOG_STRIP_LEVEL
*
* Define CV_LOG_STRIP_LEVEL=CV_LOG_LEVEL_[DEBUG|INFO|WARN|ERROR|FATAL|SILENT] to compile out anything at that and before that logging level
*/
#ifndef CV_LOG_STRIP_LEVEL
# if defined NDEBUG
# define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_DEBUG
# else
# define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE
# endif
#endif
#define CV_LOGTAG_PTR_CAST(expr) static_cast<const cv::utils::logging::LogTag*>(expr)
// CV_LOGTAG_EXPAND_NAME is intended to be re-defined (undef and then define again)
// to allows logging users to use a shorter name argument when calling
// CV_LOG_WITH_TAG or its related macros such as CV_LOG_INFO.
//
// This macro is intended to modify the tag argument as a string (token), via
// preprocessor token pasting or metaprogramming techniques. A typical usage
// is to apply a prefix, such as
// ...... #define CV_LOGTAG_EXPAND_NAME(tag) cv_logtag_##tag
//
// It is permitted to re-define to a hard-coded expression, ignoring the tag.
// This would work identically like the CV_LOGTAG_FALLBACK macro.
//
// Important: When the logging macro is called with tag being NULL, a user-defined
// CV_LOGTAG_EXPAND_NAME may expand it into cv_logtag_0, cv_logtag_NULL, or
// cv_logtag_nullptr. Use with care. Also be mindful of C++ symbol redefinitions.
//
// If there is significant amount of logging code with tag being NULL, it is
// recommended to use (re-define) CV_LOGTAG_FALLBACK to inject locally a default
// tag at the beginning of a compilation unit, to minimize lines of code changes.
//
#define CV_LOGTAG_EXPAND_NAME(tag) tag
// CV_LOGTAG_FALLBACK is intended to be re-defined (undef and then define again)
// by any other compilation units to provide a log tag when the logging statement
// does not specify one. The macro needs to expand into a C++ expression that can
// be static_cast into (cv::utils::logging::LogTag*). Null (nullptr) is permitted.
#define CV_LOGTAG_FALLBACK nullptr
// CV_LOGTAG_GLOBAL is the tag used when a log tag is not specified in the logging
// statement nor the compilation unit. The macro needs to expand into a C++
// expression that can be static_cast into (cv::utils::logging::LogTag*). Must be
// non-null. Do not re-define.
#define CV_LOGTAG_GLOBAL cv::utils::logging::internal::getGlobalLogTag()
#define CV_LOG_WITH_TAG(tag, msgLevel, extra_check0, extra_check1, ...) \
for(;;) { \
extra_check0; \
const auto cv_temp_msglevel = (cv::utils::logging::LogLevel)(msgLevel); \
if (cv_temp_msglevel >= (CV_LOG_STRIP_LEVEL)) break; \
auto cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_EXPAND_NAME(tag)); \
if (!cv_temp_logtagptr) cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_FALLBACK); \
if (!cv_temp_logtagptr) cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_GLOBAL); \
if (cv_temp_logtagptr && (cv_temp_msglevel > cv_temp_logtagptr->level)) break; \
extra_check1; \
std::stringstream cv_temp_logstream; \
cv_temp_logstream << __VA_ARGS__; \
cv::utils::logging::internal::writeLogMessageEx( \
cv_temp_msglevel, \
(cv_temp_logtagptr ? cv_temp_logtagptr->name : nullptr), \
__FILE__, \
__LINE__, \
CV_Func, \
cv_temp_logstream.str().c_str()); \
break; \
}
#define CV_LOG_FATAL(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_FATAL, , , __VA_ARGS__)
#define CV_LOG_ERROR(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, , , __VA_ARGS__)
#define CV_LOG_WARNING(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, , , __VA_ARGS__)
#define CV_LOG_INFO(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, , , __VA_ARGS__)
#define CV_LOG_DEBUG(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, , , __VA_ARGS__)
#define CV_LOG_VERBOSE(tag, v, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), , , __VA_ARGS__)
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
#undef CV_LOG_INFO
#define CV_LOG_INFO(tag, ...)
#endif
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
#undef CV_LOG_DEBUG
#define CV_LOG_DEBUG(tag, ...)
#endif
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
#undef CV_LOG_VERBOSE
#define CV_LOG_VERBOSE(tag, v, ...)
#endif
//! @cond IGNORED
#define CV__LOG_ONCE_CHECK_PRE \
static bool _cv_log_once_ ## __LINE__ = false; \
if (_cv_log_once_ ## __LINE__) break;
#define CV__LOG_ONCE_CHECK_POST \
_cv_log_once_ ## __LINE__ = true;
#define CV__LOG_IF_CHECK(logging_cond) \
if (!(logging_cond)) break;
//! @endcond
// CV_LOG_ONCE_XXX macros
#define CV_LOG_ONCE_ERROR(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
#define CV_LOG_ONCE_WARNING(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
#define CV_LOG_ONCE_INFO(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
#define CV_LOG_ONCE_DEBUG(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
#define CV_LOG_ONCE_VERBOSE(tag, v, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
#undef CV_LOG_ONCE_INFO
#define CV_LOG_ONCE_INFO(tag, ...)
#endif
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
#undef CV_LOG_ONCE_DEBUG
#define CV_LOG_ONCE_DEBUG(tag, ...)
#endif
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
#undef CV_LOG_ONCE_VERBOSE
#define CV_LOG_ONCE_VERBOSE(tag, v, ...)
#endif
// CV_LOG_IF_XXX macros
#define CV_LOG_IF_FATAL(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_FATAL, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
#define CV_LOG_IF_ERROR(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
#define CV_LOG_IF_WARNING(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
#define CV_LOG_IF_INFO(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
#define CV_LOG_IF_DEBUG(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
#define CV_LOG_IF_VERBOSE(tag, v, logging_cond, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
#undef CV_LOG_IF_INFO
#define CV_LOG_IF_INFO(tag, logging_cond, ...)
#endif
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
#undef CV_LOG_IF_DEBUG
#define CV_LOG_IF_DEBUG(tag, logging_cond, ...)
#endif
#if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
#undef CV_LOG_IF_VERBOSE
#define CV_LOG_IF_VERBOSE(tag, v, logging_cond, ...)
#endif
//! @}
}}} // namespace
#endif // OPENCV_LOGGER_HPP

View File

@@ -0,0 +1,28 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_CORE_LOGTAG_HPP
#define OPENCV_CORE_LOGTAG_HPP
#include "opencv2/core/cvstd.hpp"
#include "logger.defines.hpp"
namespace cv {
namespace utils {
namespace logging {
struct LogTag
{
const char* name;
LogLevel level;
inline LogTag(const char* _name, LogLevel _level)
: name(_name)
, level(_level)
{}
};
}}}
#endif

View File

@@ -0,0 +1,235 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_UTILS_TLS_HPP
#define OPENCV_UTILS_TLS_HPP
#ifndef OPENCV_CORE_UTILITY_H
#error "tls.hpp must be included after opencv2/core/utility.hpp or opencv2/core.hpp"
#endif
namespace cv {
//! @addtogroup core_utils
//! @{
namespace details { class TlsStorage; }
/** TLS container base implementation
*
* Don't use directly.
*
* @sa TLSData, TLSDataAccumulator templates
*/
class CV_EXPORTS TLSDataContainer
{
protected:
TLSDataContainer();
virtual ~TLSDataContainer();
/// @deprecated use detachData() instead
void gatherData(std::vector<void*> &data) const;
/// get TLS data and detach all data from threads (similar to cleanup() call)
void detachData(std::vector<void*>& data);
void* getData() const;
void release();
protected:
virtual void* createDataInstance() const = 0;
virtual void deleteDataInstance(void* pData) const = 0;
private:
int key_;
friend class cv::details::TlsStorage; // core/src/system.cpp
public:
void cleanup(); //!< Release created TLS data container objects. It is similar to release() call, but it keeps TLS container valid.
private:
// Disable copy/assign (noncopyable pattern)
TLSDataContainer(TLSDataContainer &) = delete;
TLSDataContainer& operator =(const TLSDataContainer &) = delete;
};
/** @brief Simple TLS data class
*
* @sa TLSDataAccumulator
*/
template <typename T>
class TLSData : protected TLSDataContainer
{
public:
inline TLSData() {}
inline ~TLSData() { release(); }
inline T* get() const { return (T*)getData(); } //!< Get data associated with key
inline T& getRef() const { T* ptr = (T*)getData(); CV_DbgAssert(ptr); return *ptr; } //!< Get data associated with key
/// Release associated thread data
inline void cleanup()
{
TLSDataContainer::cleanup();
}
protected:
/// Wrapper to allocate data by template
virtual void* createDataInstance() const CV_OVERRIDE { return new T; }
/// Wrapper to release data by template
virtual void deleteDataInstance(void* pData) const CV_OVERRIDE { delete (T*)pData; }
};
/// TLS data accumulator with gathering methods
template <typename T>
class TLSDataAccumulator : public TLSData<T>
{
mutable cv::Mutex mutex;
mutable std::vector<T*> dataFromTerminatedThreads;
std::vector<T*> detachedData;
bool cleanupMode;
public:
TLSDataAccumulator() : cleanupMode(false) {}
~TLSDataAccumulator()
{
release();
}
/** @brief Get data from all threads
* @deprecated replaced by detachData()
*
* Lifetime of vector data is valid until next detachData()/cleanup()/release() calls
*
* @param[out] data result buffer (should be empty)
*/
void gather(std::vector<T*> &data) const
{
CV_Assert(cleanupMode == false); // state is not valid
CV_Assert(data.empty());
{
std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
TLSDataContainer::gatherData(dataVoid);
}
{
AutoLock lock(mutex);
data.reserve(data.size() + dataFromTerminatedThreads.size());
for (typename std::vector<T*>::const_iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
{
data.push_back((T*)*i);
}
}
}
/** @brief Get and detach data from all threads
*
* Call cleanupDetachedData() when returned vector is not needed anymore.
*
* @return Vector with associated data. Content is preserved (including lifetime of attached data pointers) until next detachData()/cleanupDetachedData()/cleanup()/release() calls
*/
std::vector<T*>& detachData()
{
CV_Assert(cleanupMode == false); // state is not valid
std::vector<void*> dataVoid;
{
TLSDataContainer::detachData(dataVoid);
}
{
AutoLock lock(mutex);
detachedData.reserve(dataVoid.size() + dataFromTerminatedThreads.size());
for (typename std::vector<T*>::const_iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
{
detachedData.push_back((T*)*i);
}
dataFromTerminatedThreads.clear();
for (typename std::vector<void*>::const_iterator i = dataVoid.begin(); i != dataVoid.end(); ++i)
{
detachedData.push_back((T*)(void*)*i);
}
}
dataVoid.clear();
return detachedData;
}
/// Release associated thread data returned by detachData() call
void cleanupDetachedData()
{
AutoLock lock(mutex);
cleanupMode = true;
_cleanupDetachedData();
cleanupMode = false;
}
/// Release associated thread data
void cleanup()
{
cleanupMode = true;
TLSDataContainer::cleanup();
AutoLock lock(mutex);
_cleanupDetachedData();
_cleanupTerminatedData();
cleanupMode = false;
}
/// Release associated thread data and free TLS key
void release()
{
cleanupMode = true;
TLSDataContainer::release();
{
AutoLock lock(mutex);
_cleanupDetachedData();
_cleanupTerminatedData();
}
}
protected:
// synchronized
void _cleanupDetachedData()
{
for (typename std::vector<T*>::iterator i = detachedData.begin(); i != detachedData.end(); ++i)
{
deleteDataInstance((T*)*i);
}
detachedData.clear();
}
// synchronized
void _cleanupTerminatedData()
{
for (typename std::vector<T*>::iterator i = dataFromTerminatedThreads.begin(); i != dataFromTerminatedThreads.end(); ++i)
{
deleteDataInstance((T*)*i);
}
dataFromTerminatedThreads.clear();
}
protected:
virtual void* createDataInstance() const CV_OVERRIDE
{
// Note: we can collect all allocated data here, but this would require raced mutex locks
return new T;
}
virtual void deleteDataInstance(void* pData) const CV_OVERRIDE
{
if (cleanupMode)
{
delete (T*)pData;
}
else
{
AutoLock lock(mutex);
dataFromTerminatedThreads.push_back((T*)pData);
}
}
};
//! @}
} // namespace
#endif // OPENCV_UTILS_TLS_HPP

View File

@@ -0,0 +1,252 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_TRACE_HPP
#define OPENCV_TRACE_HPP
#include <opencv2/core/cvdef.h>
namespace cv {
namespace utils {
namespace trace {
//! @addtogroup core_logging
//! @{
//! Macro to trace function
#define CV_TRACE_FUNCTION()
#define CV_TRACE_FUNCTION_SKIP_NESTED()
//! Trace code scope.
//! @note Dynamic names are not supported in this macro (on stack or heap). Use string literals here only, like "initialize".
#define CV_TRACE_REGION(name_as_static_string_literal)
//! mark completed of the current opened region and create new one
//! @note Dynamic names are not supported in this macro (on stack or heap). Use string literals here only, like "step1".
#define CV_TRACE_REGION_NEXT(name_as_static_string_literal)
//! Macro to trace argument value
#define CV_TRACE_ARG(arg_id)
//! Macro to trace argument value (expanded version)
#define CV_TRACE_ARG_VALUE(arg_id, arg_name, value)
//! @cond IGNORED
#define CV_TRACE_NS cv::utils::trace
#if !defined(OPENCV_DISABLE_TRACE) && defined(__EMSCRIPTEN__)
#define OPENCV_DISABLE_TRACE 1
#endif
namespace details {
#ifndef __OPENCV_TRACE
# if defined __OPENCV_BUILD && !defined __OPENCV_TESTS && !defined __OPENCV_APPS
# define __OPENCV_TRACE 1
# else
# define __OPENCV_TRACE 0
# endif
#endif
#ifndef CV_TRACE_FILENAME
# define CV_TRACE_FILENAME __FILE__
#endif
#ifndef CV__TRACE_FUNCTION
# if defined _MSC_VER
# define CV__TRACE_FUNCTION __FUNCSIG__
# elif defined __GNUC__
# define CV__TRACE_FUNCTION __PRETTY_FUNCTION__
# else
# define CV__TRACE_FUNCTION "<unknown>"
# endif
#endif
//! Thread-local instance (usually allocated on stack)
class CV_EXPORTS Region
{
public:
struct LocationExtraData;
struct LocationStaticStorage
{
LocationExtraData** ppExtra; //< implementation specific data
const char* name; //< region name (function name or other custom name)
const char* filename; //< source code filename
int line; //< source code line
int flags; //< flags (implementation code path: Plain, IPP, OpenCL)
};
Region(const LocationStaticStorage& location);
inline ~Region()
{
if (implFlags != 0)
destroy();
CV_DbgAssert(implFlags == 0);
CV_DbgAssert(pImpl == NULL);
}
class Impl;
Impl* pImpl; // NULL if current region is not active
int implFlags; // see RegionFlag, 0 if region is ignored
bool isActive() const { return pImpl != NULL; }
void destroy();
private:
Region(const Region&); // disabled
Region& operator= (const Region&); // disabled
};
//! Specify region flags
enum RegionLocationFlag {
REGION_FLAG_FUNCTION = (1 << 0), //< region is function (=1) / nested named region (=0)
REGION_FLAG_APP_CODE = (1 << 1), //< region is Application code (=1) / OpenCV library code (=0)
REGION_FLAG_SKIP_NESTED = (1 << 2), //< avoid processing of nested regions
REGION_FLAG_IMPL_IPP = (1 << 16), //< region is part of IPP code path
REGION_FLAG_IMPL_OPENCL = (2 << 16), //< region is part of OpenCL code path
REGION_FLAG_IMPL_OPENVX = (3 << 16), //< region is part of OpenVX code path
REGION_FLAG_IMPL_MASK = (15 << 16),
REGION_FLAG_REGION_FORCE = (1 << 30),
REGION_FLAG_REGION_NEXT = (1 << 31), //< close previous region (see #CV_TRACE_REGION_NEXT macro)
ENUM_REGION_FLAG_FORCE_INT = INT_MAX
};
struct CV_EXPORTS TraceArg {
public:
struct ExtraData;
ExtraData** ppExtra;
const char* name;
int flags;
};
/** @brief Add meta information to current region (function)
* See CV_TRACE_ARG macro
* @param arg argument information structure (global static cache)
* @param value argument value (can by dynamic string literal in case of string, static allocation is not required)
*/
CV_EXPORTS void traceArg(const TraceArg& arg, const char* value);
//! @overload
CV_EXPORTS void traceArg(const TraceArg& arg, int value);
//! @overload
CV_EXPORTS void traceArg(const TraceArg& arg, int64 value);
//! @overload
CV_EXPORTS void traceArg(const TraceArg& arg, double value);
#define CV__TRACE_LOCATION_VARNAME(loc_id) CVAUX_CONCAT(CVAUX_CONCAT(__cv_trace_location_, loc_id), __LINE__)
#define CV__TRACE_LOCATION_EXTRA_VARNAME(loc_id) CVAUX_CONCAT(CVAUX_CONCAT(__cv_trace_location_extra_, loc_id) , __LINE__)
#define CV__TRACE_DEFINE_LOCATION_(loc_id, name, flags) \
static CV_TRACE_NS::details::Region::LocationExtraData* CV__TRACE_LOCATION_EXTRA_VARNAME(loc_id) = 0; \
static const CV_TRACE_NS::details::Region::LocationStaticStorage \
CV__TRACE_LOCATION_VARNAME(loc_id) = { &(CV__TRACE_LOCATION_EXTRA_VARNAME(loc_id)), name, CV_TRACE_FILENAME, __LINE__, flags};
#define CV__TRACE_DEFINE_LOCATION_FN(name, flags) CV__TRACE_DEFINE_LOCATION_(fn, name, ((flags) | CV_TRACE_NS::details::REGION_FLAG_FUNCTION))
#define CV__TRACE_OPENCV_FUNCTION() \
CV__TRACE_DEFINE_LOCATION_FN(CV__TRACE_FUNCTION, 0); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_OPENCV_FUNCTION_NAME(name) \
CV__TRACE_DEFINE_LOCATION_FN(name, 0); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_APP_FUNCTION() \
CV__TRACE_DEFINE_LOCATION_FN(CV__TRACE_FUNCTION, CV_TRACE_NS::details::REGION_FLAG_APP_CODE); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_APP_FUNCTION_NAME(name) \
CV__TRACE_DEFINE_LOCATION_FN(name, CV_TRACE_NS::details::REGION_FLAG_APP_CODE); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_OPENCV_FUNCTION_SKIP_NESTED() \
CV__TRACE_DEFINE_LOCATION_FN(CV__TRACE_FUNCTION, CV_TRACE_NS::details::REGION_FLAG_SKIP_NESTED); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_OPENCV_FUNCTION_NAME_SKIP_NESTED(name) \
CV__TRACE_DEFINE_LOCATION_FN(name, CV_TRACE_NS::details::REGION_FLAG_SKIP_NESTED); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_APP_FUNCTION_SKIP_NESTED() \
CV__TRACE_DEFINE_LOCATION_FN(CV__TRACE_FUNCTION, CV_TRACE_NS::details::REGION_FLAG_SKIP_NESTED | CV_TRACE_NS::details::REGION_FLAG_APP_CODE); \
const CV_TRACE_NS::details::Region __region_fn(CV__TRACE_LOCATION_VARNAME(fn));
#define CV__TRACE_REGION_(name_as_static_string_literal, flags) \
CV__TRACE_DEFINE_LOCATION_(region, name_as_static_string_literal, flags); \
CV_TRACE_NS::details::Region CVAUX_CONCAT(__region_, __LINE__)(CV__TRACE_LOCATION_VARNAME(region));
#define CV__TRACE_REGION(name_as_static_string_literal) CV__TRACE_REGION_(name_as_static_string_literal, 0)
#define CV__TRACE_REGION_NEXT(name_as_static_string_literal) CV__TRACE_REGION_(name_as_static_string_literal, CV_TRACE_NS::details::REGION_FLAG_REGION_NEXT)
#define CV__TRACE_ARG_VARNAME(arg_id) CVAUX_CONCAT(__cv_trace_arg_ ## arg_id, __LINE__)
#define CV__TRACE_ARG_EXTRA_VARNAME(arg_id) CVAUX_CONCAT(__cv_trace_arg_extra_ ## arg_id, __LINE__)
#define CV__TRACE_DEFINE_ARG_(arg_id, name, flags) \
static CV_TRACE_NS::details::TraceArg::ExtraData* CV__TRACE_ARG_EXTRA_VARNAME(arg_id) = 0; \
static const CV_TRACE_NS::details::TraceArg \
CV__TRACE_ARG_VARNAME(arg_id) = { &(CV__TRACE_ARG_EXTRA_VARNAME(arg_id)), name, flags };
#define CV__TRACE_ARG_VALUE(arg_id, arg_name, value) \
CV__TRACE_DEFINE_ARG_(arg_id, arg_name, 0); \
CV_TRACE_NS::details::traceArg((CV__TRACE_ARG_VARNAME(arg_id)), value);
#define CV__TRACE_ARG(arg_id) CV_TRACE_ARG_VALUE(arg_id, #arg_id, (arg_id))
} // namespace
#ifndef OPENCV_DISABLE_TRACE
#undef CV_TRACE_FUNCTION
#undef CV_TRACE_FUNCTION_SKIP_NESTED
#if __OPENCV_TRACE
#define CV_TRACE_FUNCTION CV__TRACE_OPENCV_FUNCTION
#define CV_TRACE_FUNCTION_SKIP_NESTED CV__TRACE_OPENCV_FUNCTION_SKIP_NESTED
#else
#define CV_TRACE_FUNCTION CV__TRACE_APP_FUNCTION
#define CV_TRACE_FUNCTION_SKIP_NESTED CV__TRACE_APP_FUNCTION_SKIP_NESTED
#endif
#undef CV_TRACE_REGION
#define CV_TRACE_REGION CV__TRACE_REGION
#undef CV_TRACE_REGION_NEXT
#define CV_TRACE_REGION_NEXT CV__TRACE_REGION_NEXT
#undef CV_TRACE_ARG_VALUE
#define CV_TRACE_ARG_VALUE(arg_id, arg_name, value) \
if (__region_fn.isActive()) \
{ \
CV__TRACE_ARG_VALUE(arg_id, arg_name, value); \
}
#undef CV_TRACE_ARG
#define CV_TRACE_ARG CV__TRACE_ARG
#endif // OPENCV_DISABLE_TRACE
#ifdef OPENCV_TRACE_VERBOSE
#define CV_TRACE_FUNCTION_VERBOSE CV_TRACE_FUNCTION
#define CV_TRACE_REGION_VERBOSE CV_TRACE_REGION
#define CV_TRACE_REGION_NEXT_VERBOSE CV_TRACE_REGION_NEXT
#define CV_TRACE_ARG_VALUE_VERBOSE CV_TRACE_ARG_VALUE
#define CV_TRACE_ARG_VERBOSE CV_TRACE_ARG
#else
#define CV_TRACE_FUNCTION_VERBOSE(...)
#define CV_TRACE_REGION_VERBOSE(...)
#define CV_TRACE_REGION_NEXT_VERBOSE(...)
#define CV_TRACE_ARG_VALUE_VERBOSE(...)
#define CV_TRACE_ARG_VERBOSE(...)
#endif
//! @endcond
//! @}
}}} // namespace
#endif // OPENCV_TRACE_HPP