Skip to content

Commit

Permalink
Add logging of source location (#730)
Browse files Browse the repository at this point in the history
* Add logging of source location
  • Loading branch information
RainerKuemmerle authored Oct 28, 2023
1 parent 1a45a65 commit 95a177f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions g2o/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
add_subdirectory(data_fitting)
add_subdirectory(dynamic_vertex)
add_subdirectory(bal)
add_subdirectory(logging)
add_subdirectory(target)
add_subdirectory(tutorial_slam2d)

Expand Down
5 changes: 5 additions & 0 deletions g2o/examples/logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(logging_application
logging.cpp
)
target_link_libraries(logging_application stuff)
set_target_properties(logging_application PROPERTIES OUTPUT_NAME logging_example)
34 changes: 34 additions & 0 deletions g2o/examples/logging/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// g2o - General Graph Optimization
// Copyright (C) 2011 G. Grisetti, R. Kuemmerle, W. Burgard
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "g2o/stuff/logger.h"

int main(int argc, char** argv) {
G2O_DEBUG("DEBUG Hello World");
G2O_WARN("WARN Hello World");
G2O_ERROR("ERROR Hello World");
G2O_INFO("INFO Hello World {}", 123);
}
31 changes: 25 additions & 6 deletions g2o/stuff/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define G2O_LOGGER_H

#include "g2o/config.h"
#include "g2o/stuff/g2o_stuff_api.h"

#ifdef G2O_HAVE_LOGGING
#include <spdlog/spdlog.h>
Expand All @@ -40,7 +41,7 @@ namespace internal {
/**
* @brief Interface class to the underlying logging library
*/
class LoggerInterface {
class G2O_STUFF_API LoggerInterface {
public:
LoggerInterface();

Expand Down Expand Up @@ -77,17 +78,35 @@ using Logger = internal::Singleton<internal::LoggerInterface>;

} // namespace g2o

#define G2O_INFO(...) g2o::Logger::get().console().info(__VA_ARGS__)
#define G2O_WARN(...) g2o::Logger::get().console().warn(__VA_ARGS__)
#define G2O_ERROR(...) g2o::Logger::get().console().error(__VA_ARGS__)
#define G2O_DEBUG(...) g2o::Logger::get().console().debug(__VA_ARGS__)
// TODO(Rainer): Switch to std::source_location with c++20
#define G2O_DEBUG(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
spdlog::level::debug, __VA_ARGS__)
#define G2O_INFO(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
spdlog::level::info, __VA_ARGS__)
#define G2O_WARN(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
spdlog::level::warn, __VA_ARGS__)
#define G2O_ERROR(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
spdlog::level::err, __VA_ARGS__)
#define G2O_CRITICAL(...) \
g2o::Logger::get().console().log( \
spdlog::source_loc(__FILE__, __LINE__, __FUNCTION__), \
spdlog::level::critical, __VA_ARGS__)

#else

#define G2O_DEBUG(...)
#define G2O_INFO(...)
#define G2O_WARN(...)
#define G2O_ERROR(...)
#define G2O_DEBUG(...)
#define G2O_CRITICAL(...)

#endif // G2O_HAVE_LOGGING

Expand Down

0 comments on commit 95a177f

Please sign in to comment.