Skip to content

Commit 859efe1

Browse files
committed
c api supports GLOG redirect
1 parent ecda11d commit 859efe1

1 file changed

Lines changed: 181 additions & 7 deletions

File tree

build/patches/04-v2-capi.patch

Lines changed: 181 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
diff --git a/paddle/fluid/inference/capi_exp/pd_common.h b/paddle/fluid/inference/capi_exp/pd_common.h
2-
index 6581d09fab..a9200aa696 100644
2+
index 6581d09fab..07bd731644 100644
33
--- a/paddle/fluid/inference/capi_exp/pd_common.h
44
+++ b/paddle/fluid/inference/capi_exp/pd_common.h
5-
@@ -80,3 +80,30 @@ PD_ENUM(PD_DataType){
5+
@@ -59,6 +59,13 @@ typedef int8_t PD_Bool;
6+
#define TRUE 1
7+
#define FALSE 0
8+
9+
+typedef void (*PD_GlogRedirectCallback)(int severity,
10+
+ const char* file,
11+
+ int line,
12+
+ const char* message,
13+
+ size_t message_len,
14+
+ void* user_data);
15+
+
16+
#define PD_ENUM(type) \
17+
typedef int32_t type; \
18+
enum
19+
@@ -80,3 +87,56 @@ PD_ENUM(PD_DataType){
620
PD_DATA_UINT8,
721
PD_DATA_INT8,
822
};
@@ -30,6 +44,32 @@ index 6581d09fab..a9200aa696 100644
3044
+///
3145
+PADDLE_CAPI_EXPORT extern const char* PD_GetLastError();
3246
+
47+
+///
48+
+/// \brief Set the minimum emitted glog severity level for the current process.
49+
+/// Valid levels are 0 (INFO), 1 (WARNING), 2 (ERROR), and 3 (FATAL).
50+
+/// On invalid input, the previous level is preserved and the last C API error
51+
+/// is updated.
52+
+///
53+
+PADDLE_CAPI_EXPORT extern void PD_SetGlogMinLogLevel(int level);
54+
+
55+
+///
56+
+/// \brief Get the current process-wide glog minimum severity level.
57+
+///
58+
+PADDLE_CAPI_EXPORT extern int PD_GetGlogMinLogLevel();
59+
+
60+
+///
61+
+/// \brief Redirect process-wide glog output to a single C callback.
62+
+/// Passing nullptr unregisters the current callback. While a callback is
63+
+/// registered, Paddle best-effort disables the original stderr console output
64+
+/// and forwards log messages through the callback instead.
65+
+///
66+
+/// The callback is invoked synchronously from glog. The message buffer is only
67+
+/// valid for the duration of the callback, so the consumer should copy it if
68+
+/// it needs to keep the contents.
69+
+///
70+
+PADDLE_CAPI_EXPORT extern void PD_SetGlogRedirectCallback(
71+
+ PD_GlogRedirectCallback callback, void* user_data);
72+
+
3373
+#ifdef __cplusplus
3474
+} // extern "C"
3575
+#endif
@@ -175,24 +215,91 @@ index f17d78f915..7d5101fb4e 100644
175215
/// \brief Clear the intermediate tensors of the predictor
176216
///
177217
diff --git a/paddle/fluid/inference/capi_exp/pd_utils.cc b/paddle/fluid/inference/capi_exp/pd_utils.cc
178-
index 95458ec204..651d7b28ed 100644
218+
index 95458ec204..e60df9b278 100644
179219
--- a/paddle/fluid/inference/capi_exp/pd_utils.cc
180220
+++ b/paddle/fluid/inference/capi_exp/pd_utils.cc
181-
@@ -14,10 +14,49 @@
221+
@@ -11,12 +11,183 @@
222+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
223+
// See the License for the specific language governing permissions and
224+
// limitations under the License.
225+
+#include <glog/logging.h>
226+
+
227+
+#include <mutex>
182228
#include <string>
183229

184230
#include "paddle/fluid/inference/api/paddle_inference_api.h"
185231
+#include "paddle/fluid/inference/capi_exp/pd_error_internal.h"
186232
#include "paddle/fluid/inference/capi_exp/pd_utils.h"
187233
#include "paddle/fluid/inference/capi_exp/utils_internal.h"
188234
#include "paddle/fluid/platform/enforce.h"
189-
235+
+#include "paddle/fluid/platform/init.h"
236+
+
190237
+namespace paddle_infer {
191238
+namespace capi_exp {
192239
+
193240
+namespace {
194241
+thread_local int g_last_error_code = PD_STATUS_OK;
195242
+thread_local std::string g_last_error_message;
243+
+
244+
+constexpr int kMaxGlogMinLogLevel = 3;
245+
+constexpr int kDisabledStderrThreshold = 4;
246+
+
247+
+struct GlogRedirectState {
248+
+ std::mutex mutex;
249+
+ PD_GlogRedirectCallback callback = nullptr;
250+
+ void* user_data = nullptr;
251+
+ bool sink_registered = false;
252+
+ bool flags_saved = false;
253+
+ bool saved_logtostderr = false;
254+
+ int saved_stderrthreshold = 0;
255+
+};
256+
+
257+
+GlogRedirectState& GetGlogRedirectState();
258+
+
259+
+class CApiLogSink : public google::LogSink {
260+
+ public:
261+
+ void send(google::LogSeverity severity,
262+
+ const char* full_filename,
263+
+ const char* /*base_filename*/,
264+
+ int line,
265+
+ const tm* /*time*/,
266+
+ const char* message,
267+
+ size_t message_len) override {
268+
+ PD_GlogRedirectCallback callback = nullptr;
269+
+ void* user_data = nullptr;
270+
+ {
271+
+ std::lock_guard<std::mutex> lock(GetGlogRedirectState().mutex);
272+
+ callback = GetGlogRedirectState().callback;
273+
+ user_data = GetGlogRedirectState().user_data;
274+
+ }
275+
+ if (callback != nullptr) {
276+
+ callback(static_cast<int>(severity),
277+
+ full_filename == nullptr ? "" : full_filename,
278+
+ line,
279+
+ message,
280+
+ message_len,
281+
+ user_data);
282+
+ }
283+
+ }
284+
+};
285+
+
286+
+GlogRedirectState& GetGlogRedirectState() {
287+
+ static auto* state = new GlogRedirectState();
288+
+ return *state;
289+
+}
290+
+
291+
+CApiLogSink& GetGlogRedirectSink() {
292+
+ static auto* sink = new CApiLogSink();
293+
+ return *sink;
294+
+}
295+
+
296+
+void EnsureGlogInitialized() {
297+
+ paddle::framework::InitGLOG("paddle_inference_c_api");
298+
+}
299+
+
300+
+bool IsValidGlogMinLogLevel(int level) {
301+
+ return level >= 0 && level <= kMaxGlogMinLogLevel;
302+
+}
196303
+} // namespace
197304
+
198305
+void ClearLastError() {
@@ -223,8 +330,75 @@ index 95458ec204..651d7b28ed 100644
223330
+ return paddle_infer::capi_exp::GetLastErrorMessage();
224331
+}
225332
+
226-
+} // extern "C"
333+
+void PD_SetGlogMinLogLevel(int level) {
334+
+ paddle_infer::capi_exp::ClearLastError();
335+
+ if (!paddle_infer::capi_exp::IsValidGlogMinLogLevel(level)) {
336+
+ paddle_infer::capi_exp::SetLastError(
337+
+ PD_STATUS_INVALID_ARGUMENT,
338+
+ "glog minimum severity level must be in range [0, 3]");
339+
+ return;
340+
+ }
341+
+ paddle_infer::capi_exp::EnsureGlogInitialized();
342+
+ FLAGS_minloglevel = level;
343+
+}
344+
+
345+
+int PD_GetGlogMinLogLevel() {
346+
+ paddle_infer::capi_exp::ClearLastError();
347+
+ paddle_infer::capi_exp::EnsureGlogInitialized();
348+
+ return FLAGS_minloglevel;
349+
+}
350+
+
351+
+void PD_SetGlogRedirectCallback(PD_GlogRedirectCallback callback,
352+
+ void* user_data) {
353+
+ paddle_infer::capi_exp::ClearLastError();
354+
+ paddle_infer::capi_exp::EnsureGlogInitialized();
355+
+
356+
+ auto& state = paddle_infer::capi_exp::GetGlogRedirectState();
357+
+ bool should_add_sink = false;
358+
+ bool should_remove_sink = false;
227359
+
360+
+ {
361+
+ std::lock_guard<std::mutex> lock(state.mutex);
362+
+
363+
+ if (callback == nullptr) {
364+
+ state.callback = nullptr;
365+
+ state.user_data = nullptr;
366+
+ if (state.sink_registered) {
367+
+ state.sink_registered = false;
368+
+ should_remove_sink = true;
369+
+ }
370+
+ if (state.flags_saved) {
371+
+ FLAGS_logtostderr = state.saved_logtostderr;
372+
+ FLAGS_stderrthreshold = state.saved_stderrthreshold;
373+
+ state.flags_saved = false;
374+
+ }
375+
+ } else {
376+
+ if (!state.sink_registered) {
377+
+ state.saved_logtostderr = FLAGS_logtostderr;
378+
+ state.saved_stderrthreshold = FLAGS_stderrthreshold;
379+
+ state.flags_saved = true;
380+
+ state.sink_registered = true;
381+
+ should_add_sink = true;
382+
+ }
383+
+
384+
+ state.callback = callback;
385+
+ state.user_data = user_data;
386+
+
387+
+ FLAGS_logtostderr = false;
388+
+ FLAGS_stderrthreshold =
389+
+ paddle_infer::capi_exp::kDisabledStderrThreshold;
390+
+ }
391+
+ }
392+
+
393+
+ if (should_remove_sink) {
394+
+ google::RemoveLogSink(&paddle_infer::capi_exp::GetGlogRedirectSink());
395+
+ }
396+
+ if (should_add_sink) {
397+
+ google::AddLogSink(&paddle_infer::capi_exp::GetGlogRedirectSink());
398+
+ }
399+
+}
400+
+
401+
+} // extern "C"
402+
228403
#define DESTROY_ONE_DIM_ARRAY(type) \
229404
void PD_OneDimArray##type##Destroy(__pd_take PD_OneDimArray##type* array) { \
230-
if (array != nullptr) { \

0 commit comments

Comments
 (0)