1414 * limitations under the License.
1515 */
1616
17- // / @file main .cpp
17+ // / @file basic_usage .cpp
1818// / @brief Demonstrates LiveKit SDK log-level control and custom log callbacks.
1919// /
20- // / Logging has two tiers of filtering:
20+ // / The SDK internally uses spdlog for its own logging. Client applications
21+ // / control SDK log output through two public APIs:
2122// /
22- // / 1. **Compile-time** (LIVEKIT_LOG_LEVEL, set via CMake):
23- // / Calls below this level are stripped from the binary entirely.
24- // / Default is TRACE (nothing stripped). For a lean release build:
25- // / cmake -DLIVEKIT_LOG_LEVEL=WARN ...
26- // /
27- // / 2. **Runtime** (setLogLevel()):
28- // / Among the levels that survived compilation, setLogLevel() controls
29- // / which ones actually produce output. This is what this example demos.
23+ // / - livekit::setLogLevel() — filter SDK messages by severity.
24+ // / - livekit::setLogCallback() — redirect SDK log output to your own handler.
3025// /
3126// / Usage:
32- // / LoggingLevels [trace|debug|info|warn|error|critical|off]
27+ // / LoggingLevelsBasicUsage [trace|debug|info|warn|error|critical|off]
3328// /
34- // / If no argument is given, the example cycles through every level so you can
35- // / see which messages are filtered at each setting .
29+ // / If no argument is given, the example cycles through every level and then
30+ // / demonstrates the custom callback API .
3631
3732#include " livekit/livekit.h"
38- #include " livekit/lk_log.h"
3933
4034#include < cstring>
4135#include < iostream>
4236#include < string>
43- #include < thread>
4437
4538namespace {
4639
@@ -84,16 +77,6 @@ livekit::LogLevel parseLevel(const char *arg) {
8477 return livekit::LogLevel::Info;
8578}
8679
87- // / Emit one message at every severity level using the LK_LOG_* macros.
88- void emitAllLevels () {
89- LK_LOG_TRACE (" This is a TRACE message (very verbose internals)" );
90- LK_LOG_DEBUG (" This is a DEBUG message (diagnostic detail)" );
91- LK_LOG_INFO (" This is an INFO message (normal operation)" );
92- LK_LOG_WARN (" This is a WARN message (something unexpected)" );
93- LK_LOG_ERROR (" This is an ERROR message (something failed)" );
94- LK_LOG_CRITICAL (" This is a CRITICAL message (unrecoverable)" );
95- }
96-
9780// / Demonstrate cycling through every log level.
9881void runLevelCycleDemo () {
9982 const livekit::LogLevel levels[] = {
@@ -108,7 +91,8 @@ void runLevelCycleDemo() {
10891 << " Setting log level to: " << levelName (level) << " \n "
10992 << " ========================================\n " ;
11093 livekit::setLogLevel (level);
111- emitAllLevels ();
94+ std::cout << " Current SDK log level: "
95+ << levelName (livekit::getLogLevel ()) << " \n " ;
11296 }
11397}
11498
@@ -120,40 +104,41 @@ void runCallbackDemo() {
120104
121105 livekit::setLogLevel (livekit::LogLevel::Trace);
122106
123- // Install a user-defined callback that captures all log output.
124- // In a real ROS2 node you would replace this with RCLCPP_* macros.
125107 livekit::setLogCallback ([](livekit::LogLevel level,
126108 const std::string &logger_name,
127109 const std::string &message) {
128110 std::cout << " [CALLBACK] [" << levelName (level) << " ] [" << logger_name
129111 << " ] " << message << " \n " ;
130112 });
131113
132- LK_LOG_INFO (" This message is routed through the custom callback" );
133- LK_LOG_WARN (" Warnings also go through the callback" );
134- LK_LOG_ERROR (" Errors too -- the callback sees everything >= the level" );
114+ std::cout << " Installed custom callback. SDK log messages will now be "
115+ " routed through it.\n " ;
135116
136117 // Restore default stderr sink by passing an empty callback.
137118 livekit::setLogCallback (nullptr );
138119
139- std::cout << " \n (Restored default stderr sink)\n " ;
140- LK_LOG_INFO (" This message goes to stderr again (default sink)" );
120+ std::cout << " (Restored default stderr sink)\n " ;
141121}
142122
143123} // namespace
144124
145125int main (int argc, char *argv[]) {
146- // Initialize the LiveKit SDK (creates the spdlog logger).
147126 livekit::initialize ();
148127
149128 if (argc > 1 ) {
150- // Single-level mode: set the requested level and emit all messages.
129+ if (std::strcmp (argv[1 ], " -h" ) == 0 ||
130+ std::strcmp (argv[1 ], " --help" ) == 0 ) {
131+ std::cout << " Usage: LoggingLevelsBasicUsage "
132+ " [trace|debug|info|warn|error|critical|off]\n " ;
133+ livekit::shutdown ();
134+ return 0 ;
135+ }
151136 livekit::LogLevel level = parseLevel (argv[1 ]);
152137 std::cout << " Setting log level to: " << levelName (level) << " \n\n " ;
153138 livekit::setLogLevel (level);
154- emitAllLevels ();
139+ std::cout << " Current SDK log level: "
140+ << levelName (livekit::getLogLevel ()) << " \n " ;
155141 } else {
156- // Full demo: cycle through levels, then show the callback API.
157142 runLevelCycleDemo ();
158143 runCallbackDemo ();
159144 }
0 commit comments