From 085f7ccdedc24b302a4eb46c4d1f38ee32d27a8c Mon Sep 17 00:00:00 2001 From: homuler Date: Sun, 29 Nov 2020 14:42:10 +0900 Subject: [PATCH] feat: add Glog APIs for debugging --- Assets/MediaPipe/SDK/Scripts/External/Glog.cs | 32 +++++++++++++++++++ .../NativeMethods/External/Glog_Unsafe.cs | 15 +++++++++ C/mediapipe_api/BUILD | 1 + C/mediapipe_api/external/BUILD | 11 +++++++ C/mediapipe_api/external/glog.cc | 21 ++++++++++++ C/mediapipe_api/external/glog.h | 18 +++++++++++ 6 files changed, 98 insertions(+) create mode 100644 C/mediapipe_api/external/glog.cc create mode 100644 C/mediapipe_api/external/glog.h diff --git a/Assets/MediaPipe/SDK/Scripts/External/Glog.cs b/Assets/MediaPipe/SDK/Scripts/External/Glog.cs index bf8b318c9..dcbf83c8d 100644 --- a/Assets/MediaPipe/SDK/Scripts/External/Glog.cs +++ b/Assets/MediaPipe/SDK/Scripts/External/Glog.cs @@ -1,5 +1,12 @@ namespace Mediapipe { public class Glog { + public enum Severity : int { + INFO = 0, + WARNING = 1, + ERROR = 2, + FATAL = 3, + } + public static void Initialize(string name, string dir) { UnsafeNativeMethods.google_InitGoogleLogging__PKc(name, dir).Assert(); } @@ -7,5 +14,30 @@ public static void Initialize(string name, string dir) { public static void Shutdown() { UnsafeNativeMethods.google_ShutdownGoogleLogging().Assert(); } + + public static void Log(Severity severity, string str) { + switch (severity) { + case Severity.INFO: { + UnsafeNativeMethods.glog_LOG_INFO__PKc(str); + break; + } + case Severity.WARNING: { + UnsafeNativeMethods.glog_LOG_WARNING__PKc(str); + break; + } + case Severity.ERROR: { + UnsafeNativeMethods.glog_LOG_ERROR__PKc(str); + break; + } + case Severity.FATAL: { + UnsafeNativeMethods.glog_LOG_FATAL__PKc(str); + break; + } + } + } + + public static void FlushLogFiles(Severity severity = Severity.INFO) { + UnsafeNativeMethods.google_FlushLogFiles(severity); + } } } diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/External/Glog_Unsafe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/External/Glog_Unsafe.cs index 2c3338cfd..c42b99a7c 100644 --- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/External/Glog_Unsafe.cs +++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/External/Glog_Unsafe.cs @@ -7,5 +7,20 @@ internal static partial class UnsafeNativeMethods { [DllImport (MediaPipeLibrary, ExactSpelling = true)] public static extern MpReturnCode google_ShutdownGoogleLogging(); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode glog_LOG_INFO__PKc(string str); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode glog_LOG_WARNING__PKc(string str); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode glog_LOG_ERROR__PKc(string str); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode glog_LOG_FATAL__PKc(string str); + + [DllImport (MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode google_FlushLogFiles(Glog.Severity severity); } } diff --git a/C/mediapipe_api/BUILD b/C/mediapipe_api/BUILD index 904810642..5ebe953ae 100644 --- a/C/mediapipe_api/BUILD +++ b/C/mediapipe_api/BUILD @@ -5,6 +5,7 @@ cc_library( name = "mediapipe_c", deps = [ ":calculators", + "//mediapipe_api/external:glog", "//mediapipe_api/external:protobuf", "//mediapipe_api/external:stdlib", "//mediapipe_api/framework:calculator", diff --git a/C/mediapipe_api/external/BUILD b/C/mediapipe_api/external/BUILD index 9cc93f46a..e850a06cd 100644 --- a/C/mediapipe_api/external/BUILD +++ b/C/mediapipe_api/external/BUILD @@ -1,3 +1,14 @@ +cc_library( + name = "glog", + srcs = ["glog.cc"], + hdrs = ["glog.h"], + deps = [ + "//mediapipe_api:common", + ], + visibility = ["//visibility:public"], + alwayslink = True, +) + cc_library( name = "protobuf", srcs = ["protobuf.cc"], diff --git a/C/mediapipe_api/external/glog.cc b/C/mediapipe_api/external/glog.cc new file mode 100644 index 000000000..e025c45e4 --- /dev/null +++ b/C/mediapipe_api/external/glog.cc @@ -0,0 +1,21 @@ +#include "mediapipe_api/external/glog.h" + +void glog_LOG_INFO__PKc(const char* str) { + LOG(INFO) << str; +} + +void glog_LOG_WARNING__PKc(const char* str) { + LOG(WARNING) << str; +} + +void glog_LOG_ERROR__PKc(const char* str) { + LOG(ERROR) << str; +} + +void glog_LOG_FATAL__PKc(const char* str) { + LOG(FATAL) << str; +} + +void google_FlushLogFiles(google::LogSeverity severity) { + google::FlushLogFiles(severity); +} diff --git a/C/mediapipe_api/external/glog.h b/C/mediapipe_api/external/glog.h new file mode 100644 index 000000000..37339d663 --- /dev/null +++ b/C/mediapipe_api/external/glog.h @@ -0,0 +1,18 @@ +#ifndef C_MEDIAPIPE_API_EXTERNAL_GLOG_H_ +#define C_MEDIAPIPE_API_EXTERNAL_GLOG_H_ + +#include +#include "mediapipe_api/common.h" + +extern "C" { + +MP_CAPI(void) glog_LOG_INFO__PKc(const char* str); +MP_CAPI(void) glog_LOG_WARNING__PKc(const char* str); +MP_CAPI(void) glog_LOG_ERROR__PKc(const char* str); +MP_CAPI(void) glog_LOG_FATAL__PKc(const char* str); + +MP_CAPI(void) google_FlushLogFiles(google::LogSeverity severity); + +} // extern "C" + +#endif // C_MEDIAPIPE_API_EXTERNAL_GLOG_H_