Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
logging_redirect.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
10#include "momentum/common/log.h"
11
12#if defined(MOMENTUM_WITH_XR_LOGGER)
13#include <logging/BrokerExtension.h>
14#endif
15
16#include <rerun.hpp>
17
18#include <bit>
19
20#if !defined(MOMENTUM_WITH_XR_LOGGER)
21#include <iostream>
22#endif
23
24namespace momentum {
25
26namespace {
27
28#if defined(MOMENTUM_WITH_XR_LOGGER)
29
30arvr::logging::LogResult callback(
31 int logLevel,
32 const char* /* channelName */,
33 size_t /* channelNameSizeInBytes */,
34 const char* message,
35 size_t /* messageSizeInBytes */,
36 bool /*isFatalLog*/,
37 arvr::logging::CustomUserData userData) {
38 const auto* rec = std::bit_cast<const rerun::RecordingStream*>(userData);
39
40 rerun::TextLogLevel level;
41 switch (logLevel) {
42 case static_cast<int>(arvr::logging::Level::Error):
43 level = rerun::TextLogLevel::Error;
44 break;
45 case static_cast<int>(arvr::logging::Level::Warning):
46 level = rerun::TextLogLevel::Warning;
47 break;
48 case static_cast<int>(arvr::logging::Level::Info):
49 level = rerun::TextLogLevel::Info;
50 break;
51 case static_cast<int>(arvr::logging::Level::Debug):
52 level = rerun::TextLogLevel::Debug;
53 break;
54 case static_cast<int>(arvr::logging::Level::Trace):
55 level = rerun::TextLogLevel::Trace;
56 break;
57 default: {
58 return arvr::logging::LogResult::Filtered;
59 }
60 }
61
62 // TODO: Make logging channel configurable
63 rec->log("momentum", rerun::TextLog(message).with_level(level));
64
65 return arvr::logging::LogResult::Accepted;
66}
67
68#endif
69
70} // namespace
71
72bool redirectLogsToRerun(const rerun::RecordingStream& rec) {
73#if defined(MOMENTUM_WITH_XR_LOGGER)
74 static arvr::logging::LogSinkHandle handle = 0;
75 if (handle != 0) {
76 // If handle is not 0, it means the log sink has already been added, so return true to indicate
77 // that the log is already being redirected.
78 return true;
79 }
80
81 handle = arvr::logging::addSink(
82 callback, reinterpret_cast<arvr::logging::CustomUserData>(&rec), "Rerun logger");
83 return (handle != 0);
84#else
85 (void)rec;
86 MT_LOGW("Log redirection is not supported.\n");
87 return false;
88#endif
89}
90
91} // namespace momentum
#define MT_LOGW(...)
Definition log.h:180
bool redirectLogsToRerun(const rerun::RecordingStream &rec)
Redirects logs from the XR_LOG framework to the Rerun logger.