Debian Codegraph @

src/pycolmap/util/logging.cc

raw
1 #include "colmap/util/logging.h"
2
3 #include "pycolmap/pybind11_extension.h"
4
5 #include <filesystem>
6
7 #include <glog/logging.h>
8 #include <pybind11/pybind11.h>
9
10 namespace py = pybind11;
11
12 struct Logging {
13 enum class LogSeverity {
14 GLOG_INFO = google::GLOG_INFO,
15 GLOG_WARNING = google::GLOG_WARNING,
16 GLOG_ERROR = google::GLOG_ERROR,
17 GLOG_FATAL = google::GLOG_FATAL,
18 };
19 }; // dummy class
20
21 std::pair<std::string, int> GetPythonCallFrame() {
22 const auto frame = py::module_::import("sys").attr("_getframe")(0);
23 const std::string file = py::str(frame.attr("f_code").attr("co_filename"));
24 const std::string function = py::str(frame.attr("f_code").attr("co_name"));
25 const int line = py::int_(frame.attr("f_lineno"));
26 return std::make_pair(file + ":" + function, line);
27 }
28
29 void BindLogging(py::module& m) {
30 py::classh<Logging> PyLogging(m, "logging", py::module_local());
31
32 py::enum_<Logging::LogSeverity>(PyLogging, "Level")
33 .value("INFO", Logging::LogSeverity::GLOG_INFO)
34 .value("WARNING", Logging::LogSeverity::GLOG_WARNING)
35 .value("ERROR", Logging::LogSeverity::GLOG_ERROR)
36 .value("FATAL", Logging::LogSeverity::GLOG_FATAL)
37 .export_values();
38
39 PyLogging.def_readwrite_static("minloglevel", &FLAGS_minloglevel)
40 .def_readwrite_static("stderrthreshold", &FLAGS_stderrthreshold)
41 .def_readwrite_static("log_dir", &FLAGS_log_dir)
42 .def_readwrite_static("logtostderr", &FLAGS_logtostderr)
43 .def_readwrite_static("alsologtostderr", &FLAGS_alsologtostderr)
44 .def_readwrite_static("verbose_level", &FLAGS_v)
45 .def_static(
46 "set_log_destination",
47 [](const Logging::LogSeverity severity,
48 const std::filesystem::path& path) {
49 google::SetLogDestination(
50 static_cast<google::LogSeverity>(severity),
51 path.string().c_str());
52 },
53 py::arg("level"),
54 py::arg("path"))
55 .def_static(
56 "verbose",
57 [](const int level, const std::string& msg) {
58 if (VLOG_IS_ON(level)) {
59 const auto frame = GetPythonCallFrame();
60 google::LogMessage(frame.first.c_str(), frame.second).stream()
61 << msg;
62 }
63 },
64 py::arg("level"),
65 py::arg("message"))
66 .def_static(
67 "info",
68 [](const std::string& msg) {
69 const auto frame = GetPythonCallFrame();
70 google::LogMessage(frame.first.c_str(), frame.second).stream()
71 << msg;
72 },
73 py::arg("message"))
74 .def_static(
75 "warning",
76 [](const std::string& msg) {
77 const auto frame = GetPythonCallFrame();
78 google::LogMessage(
79 frame.first.c_str(), frame.second, google::GLOG_WARNING)
80 .stream()
81 << msg;
82 },
83 py::arg("message"))
84 .def_static(
85 "error",
86 [](const std::string& msg) {
87 const auto frame = GetPythonCallFrame();
88 google::LogMessage(
89 frame.first.c_str(), frame.second, google::GLOG_ERROR)
90 .stream()
91 << msg;
92 },
93 py::arg("message"))
94 .def_static(
95 "fatal",
96 [](const std::string& msg) {
97 const auto frame = GetPythonCallFrame();
98 google::LogMessageFatal(frame.first.c_str(), frame.second).stream()
99 << msg;
100 },
101 py::arg("message"));
102
103 #if COLMAP_GLOG_HAS_STDOUT_SUPPORT
104 PyLogging.def_readwrite_static("logtostdout", &FLAGS_logtostdout)
105 .def_readwrite_static("colorlogtostdout", &FLAGS_colorlogtostdout);
106 #endif
107 #if COLMAP_GLOG_HAS_COLOR_SUPPORT
108 PyLogging.def_readwrite_static("colorlogtostderr", &FLAGS_colorlogtostderr);
109 #endif
110
111 #if defined(GLOG_VERSION_MAJOR) && \
112 (GLOG_VERSION_MAJOR > 0 || GLOG_VERSION_MINOR >= 6)
113 if (!google::IsGoogleLoggingInitialized())
114 #else
115 if (!py::module_::import("sys").attr("modules").contains("pyceres"))
116 #endif
117 {
118 #if !defined(COLMAP_NO_INIT_GOOGLE_LOGGING)
119 google::InitGoogleLogging("");
120 google::InstallFailureSignalHandler();
121 #endif
122 }
123 #if !defined(COLMAP_NO_INIT_GOOGLE_LOGGING)
124 FLAGS_alsologtostderr = true;
125 #endif
126 }