diff --git a/mediapipe/framework/BUILD b/mediapipe/framework/BUILD index 2930cf68d7..a49cf27084 100644 --- a/mediapipe/framework/BUILD +++ b/mediapipe/framework/BUILD @@ -358,6 +358,7 @@ cc_library( ":thread_pool_executor_cc_proto", ":timestamp", ":validated_graph_config", + ":vlog_overrides", "//mediapipe/framework/port:core_proto", "//mediapipe/framework/port:integral_types", "//mediapipe/framework/port:logging", @@ -1926,3 +1927,19 @@ cc_library( ":memory_manager", ], ) + +cc_library( + name = "vlog_overrides", + srcs = ["vlog_overrides.cc"], + hdrs = ["vlog_overrides.h"], + visibility = ["//visibility:private"], + deps = [ + "//mediapipe/framework/deps:no_destructor", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/log:globals", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/strings:string_view", + ], +) diff --git a/mediapipe/framework/calculator_graph.cc b/mediapipe/framework/calculator_graph.cc index a64c295ac9..3fe9f8d933 100644 --- a/mediapipe/framework/calculator_graph.cc +++ b/mediapipe/framework/calculator_graph.cc @@ -76,6 +76,7 @@ #include "mediapipe/framework/tool/validate.h" #include "mediapipe/framework/tool/validate_name.h" #include "mediapipe/framework/validated_graph_config.h" +#include "mediapipe/framework/vlog_overrides.h" #include "mediapipe/gpu/gpu_service.h" #include "mediapipe/gpu/graph_support.h" #include "mediapipe/util/cpu_util.h" @@ -145,6 +146,7 @@ CalculatorGraph::CalculatorGraph(CalculatorContext* cc) // TODO b/368015341- Use factory method to avoid CHECK in constructor. ABSL_CHECK_OK(DisallowServiceDefaultInitialization()); } + SetVLogOverrides(); } CalculatorGraph::CalculatorGraph(CalculatorGraphConfig config) diff --git a/mediapipe/framework/vlog_overrides.cc b/mediapipe/framework/vlog_overrides.cc new file mode 100644 index 0000000000..169f276f47 --- /dev/null +++ b/mediapipe/framework/vlog_overrides.cc @@ -0,0 +1,55 @@ +#include "mediapipe/framework/vlog_overrides.h" + +// Template to temporary enable VLOG overrides in code: +// #define MEDIAPIPE_VLOG_VMODULE "calculator_graph*=5,southbound*=5" +// #define MEDIAPIPE_VLOG_V 1 + +#if defined(MEDIAPIPE_VLOG_V) || defined(MEDIAPIPE_VLOG_VMODULE) + +#include +#include +#include + +#include "absl/log/absl_check.h" +#include "absl/log/absl_log.h" +#include "absl/log/globals.h" +#include "absl/strings/str_format.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "mediapipe/framework/deps/no_destructor.h" + +#endif // defined(MEDIAPIPE_VLOG_V) || defined(MEDIAPIPE_VLOG_VMODULE) + +namespace mediapipe { + +void SetVLogOverrides() { +#if defined(MEDIAPIPE_VLOG_V) + ABSL_LOG(INFO) << absl::StrFormat("Setting global VLOG level: %d", + MEDIAPIPE_VLOG_V); + absl::SetGlobalVLogLevel(MEDIAPIPE_VLOG_V); +#endif // defined(MEDIAPIPE_VLOG_V) + +#if defined(MEDIAPIPE_VLOG_VMODULE) + static NoDestructor>> kVModuleMapping( + []() { + constexpr absl::string_view kVModule = MEDIAPIPE_VLOG_VMODULE; + std::vector parts = + absl::StrSplit(kVModule, absl::ByAnyChar(",=")); + ABSL_CHECK_EQ(parts.size() % 2, 0) + << "Invalid MEDIAPIPE_VLOG_VMODULE: " << kVModule; + std::vector> result; + for (int i = 0; i < parts.size(); i += 2) { + result.push_back({parts[i], std::stoi(parts[i + 1])}); + } + return result; + }()); + + ABSL_LOG(INFO) << "Setting VLOG levels..."; + for (const auto& [key, value] : *kVModuleMapping) { + ABSL_LOG(INFO) << absl::StrFormat("Setting [%s] to level: %d", key, value); + absl::SetVLogLevel(key, value); + } +#endif // defined(MEDIAPIPE_VLOG_VMODULE) +} + +} // namespace mediapipe diff --git a/mediapipe/framework/vlog_overrides.h b/mediapipe/framework/vlog_overrides.h new file mode 100644 index 0000000000..82168088a5 --- /dev/null +++ b/mediapipe/framework/vlog_overrides.h @@ -0,0 +1,32 @@ +#ifndef MEDIAPIPE_FRAMEWORK_VLOG_OVERRIDES_H_ +#define MEDIAPIPE_FRAMEWORK_VLOG_OVERRIDES_H_ + +namespace mediapipe { + +// If possible, rely on --v / --vmodule to set VLOG level and modules. +// +// However, in cases when --v / --vmodule cannot be used (e.g. running an +// Android app and enabling VLOGs), MediaPipe allows to set VLOG --v / --vmodule +// overrides for debugging purposes which are applied when `CalculatorGraph` is +// created. +// +// Overrides: +// - MEDIAPIPE_VLOG_V (define and provide value you provide for --v) +// - MEDIAPIPE_VLOG_VMODULE (define and provide value you provide for --vmodule) +// +// You can set overrides by adding: +// ``` +// --copt=-DMEDIAPIPE_VLOG_VMODULE=\"*calculator*=5\" +// ``` +// with your desired module patterns and VLOG levels (see more details for +// --vmodule) to your build command. +// +// IMPORTANT: mind that adding the above to your build command will trigger +// rebuild of the whole binary including dependencies. So, considering vlog +// overrides exist for debugging purposes only, it is faster to simply modify +// `vlog_overrides.cc` adding MEDIAPIPE_VLOG_V/VMODULE at the very top. +void SetVLogOverrides(); + +} // namespace mediapipe + +#endif // MEDIAPIPE_FRAMEWORK_VLOG_OVERRIDES_H_