Skip to content

Commit

Permalink
Merge pull request #2293 from mavlink/pr-log-callback-example
Browse files Browse the repository at this point in the history
examples: add log_callback demo
  • Loading branch information
julianoes authored May 1, 2024
2 parents d68204a + 0e62ade commit 9860fd7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_subdirectory(ftp_server)
add_subdirectory(geofence)
add_subdirectory(gimbal)
add_subdirectory(gimbal_device_tester)
add_subdirectory(log_callback)
add_subdirectory(logfile_download)
add_subdirectory(log_streaming)
add_subdirectory(manual_control)
Expand Down
22 changes: 22 additions & 0 deletions examples/log_callback/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.10.2)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(log_callback)

add_executable(log_callback
log_callback.cpp
)

find_package(MAVSDK REQUIRED)

target_link_libraries(log_callback
MAVSDK::mavsdk
)

if(NOT MSVC)
add_compile_options(log_callback PRIVATE -Wall -Wextra)
else()
add_compile_options(log_callback PRIVATE -WX -W2)
endif()
30 changes: 30 additions & 0 deletions examples/log_callback/log_callback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Example how to customize MAVSDK log messages.
//

#include <mavsdk/mavsdk.h>
#include <mavsdk/log_callback.h>

#include <iostream>

using namespace mavsdk;

int main(int, char**)
{
// Set our own custom log function.
log::subscribe(
[](log::Level level, const std::string& message, const std::string& file, int line) {
std::cout << "My custom print function: " << message << std::endl;

// Prevent unused warnings.
(void)level;
(void)file;
(void)line;
return true;
});

// Instantiate Mavsdk after. This will just print the version.
Mavsdk mavsdk{Mavsdk::Configuration{Mavsdk::ComponentType::GroundStation}};

return 0;
}

0 comments on commit 9860fd7

Please sign in to comment.