Latest Code Coverage Report (master branch)
- Framework to standardize ros and glog output at compile time.
- Uses ros/glog's native log calls depending on selected mode.
- Optional Python bindings with a method-based logger API.
- Customize log output
- All log calls are thread-safe.
- Lightweight with performance in mind
- Header only for C++
- GCC 8 or later required
- Recommended to use with C++17/C++20, but also works with C++11/C++14.
src/demo.cpp
#include <log++.h>
int main(int argc, char **argv) {
LOG_INIT(argv[0]);
int foo = 5;
LOG(I, "Foo: " << foo); //Log++ syntax
ROS_INFO_STREAM("Foo: " << foo); //ROS syntax
LOG(INFO) << "Foo: " << foo; //Glog syntax
return 0;
}- MODE_DEFAULT:
INFO Foo: 5
[ INFO] [1664445448.151401926]: Foo: 5
I0929 11:57:28.151427 1360708 main.cpp:8] Foo: 5- MODE_LPP:
INFO Foo: 5
INFO Foo: 5
INFO Foo: 5- MODE_GLOG:
I0929 11:57:50.238454 1360823 main.cpp:6] Foo: 5
I0929 11:57:50.238514 1360823 main.cpp:7] Foo: 5
I0929 11:57:50.238536 1360823 main.cpp:8] Foo: 5- MODE_ROSLOG:
[ INFO] [1664445486.584182057]: Foo: 5
[ INFO] [1664445486.584212352]: Foo: 5
[ INFO] [1664445486.584240476]: Foo: 5-
Copy the header file
include/log++.hto your project. -
Add the following line to
CMakeLists.txtto select the desired mode for...
- a project:
# Valid modes are: MODE_LPP MODE_GLOG MODE_ROSLOG MODE_SYSD MODE_DEFAULT MODE_NOLOG
# Use add_definitions(-DMODE_LPP) if cmake version <= 3.11
add_compile_definitions(MODE_LPP)- a specific executable or library:
target_compile_definitions(my_executable PRIVATE MODE_LPP) - Add Log++ as a submodule with following command:
$ git submodule add git@github.com:ethz-asl/lpp.git- Add following lines in
CMakeLists.txt
add_subdirectory(my_submodule_dir/lpp)
add_executable(my_executable main.cpp)
target_link_libraries(my_executable lpp)- To update Log++ to the latest commit, execute the following commands:
From submodule directory
$ git pull origin masterFrom parent directory
$ git add path/to/submodule_dir- Clone the package into the catkin workspace
$ git clone git@github.com:ethz-asl/lpp.git- Build package
$ catkin build lppThe Python bindings are built only through scikit-build. Normal CMake and catkin builds do not require Python or nanobind.
$ uv pip install .or build a wheel:
$ uv build- MODE_LPP Log++ Logging output.
- MODE_GLOG: Google Logging output. Calls abort() if it logs a fatal error.
- MODE_ROSLOG: ROS Logging output.
- MODE_SYSD: systemd journal output. Logs can be viewed with
journalctl -t <identifier>, or followed live withjournalctl -f -t <identifier>. The identifier is the systemd journalSYSLOG_IDENTIFIERfield; by default it is the executable name passed toLOG_INIT(argv[0]), with any path stripped. - MODE_DEFAULT: Disables Logging standardization. Messages are logged according to their framework.
- MODE_NOLOG: Disables Logging completely. Useful for unittests or in some cases for release builds.
- Debug: Everything too verbose during normal execution and can be ignored.
- Info: Everything that could be of interest during a normal execution, but could also be ignored.
- Warning: Things that may need attention, but are probably not dangerous.
- Error: Things that need attention and probably are dangerous if not handled.
- Fatal: Everything that should stop execution immediately.
Note for glog:
When using with MODE_ROSLOG or MODE_LPP, the macros DLOG(severity) and VLOG() will get converted to debug severity.
When using with MODE_GLOG, the Log++ and Roslog debug macros LOG(D) and ROS_DEBUG() will get converted to DLOG(INFO).
Fatal log messages call abort(). (glog only)
Log++ provides its own logging functions, if you want to use Log++ as the base logging framework
int foo = 1;
int bar = 3;
LOG(I, "Values: " << foo << " " << bar);int foo = 1;
int bar = 3;
LOG(I, foo != bar, "Values: " << foo << " " << bar);int foo = 1;
int bar = 3;
for (int i = 0; i < 50; i++) {
LOG_EVERY(I, 20, "Values: " << foo << " " << bar) // Log every 20 calls.
}int foo = 1;
int bar = 3;
for (int i = 0; i < 10; i++) {
LOG_FIRST(I, 5, "Values: " << foo << " " << bar) //Log first 5 calls.
}Log++ also allows you to customize the log output with a callback function when using MODE_LPP:
void logCallback(BaseSeverity severity, const std::string& str) {
std::cout << str << std::endl;
}
int main(int argc, char **argv) {
LOG_INIT(argv[0], logCallback);
...
}The Python API provides a low-boilerplate Logger() helper for short scripts.
It returns a standard logging.Logger configured with an LppHandler and
logging.INFO level.
from lpp import LogMode, Logger
logger = Logger("foo", LogMode.MODE_LPP)
logger.info("started")
logger.warning("many retries")
logger.critical("cannot continue")LogMode.MODE_LPP: Log++ stdout-style output. This mode also supports a Python callback.LogMode.MODE_SYSD: systemd journal output. Passidentifier="my-service"to setSYSLOG_IDENTIFIER. Logs can be viewed withjournalctl -t my-service, or followed live withjournalctl -f -t my-service. If no identifier is provided, the Python binding uses the process basename fromsys.argv[0].LogMode.MODE_NOLOG: accepts log calls and emits nothing.LogMode.MODE_DEFAULT: uses Log++ stdout-style output for the Python handler.LogMode.MODE_GLOGandLogMode.MODE_ROSLOG: enum values are exported, but construction raisesRuntimeErrorunless these backends are implemented for Python.
import logging
from lpp import LogMode, Logger
logger = Logger("my-service", LogMode.MODE_SYSD, identifier="my-service")
logger.error("failed to connect")Use the standard Python logging API:
logger.debug("details")
logger.info("ready")
logger.info("value=%s", value)
logger.warning("retrying")
logger.error("failed")
logger.critical("cannot continue")critical() maps to Log++ fatal severity. Python logging handles formatting,
filters, levels, propagation, and handlers before the record reaches LppHandler.
Log++ policy helpers such as LOG_EVERY are intentionally not part of the
Python API; use standard logging.Filter patterns for Python-side filtering or
throttling.
For advanced logging setup, attach LppHandler manually:
import logging
from lpp import LogMode, LppHandler
logger = logging.getLogger("foo")
logger.addHandler(LppHandler(LogMode.MODE_LPP))
logger.setLevel(logging.INFO)MODE_LPP can also route output through a callback, which is useful for tests:
events = []
logger = Logger("test", callback=lambda severity, message: events.append((severity, message)))
logger.warning("captured")| Method | Log++ | Glog | ROS |
|---|---|---|---|
| Default logging | LOG(I, str) | LOG(INFO) << str | ROS_INFO(str) |
| Conditional logging | LOG(I, cond, str) | LOG_IF(INFO, cond) << str | ROS_INFO_COND(cond, str) |
| Occasional logging | LOG_EVERY(I, n, str) | LOG_EVERY_N(INFO, n) << str | - |
| Conditional occasional logging | - | LOG_IF_EVERY_N(INFO, cond, n) << str | - |
| Timed logging | LOG_TIMED(I, t, str) | LOG_EVERY_T(INFO, t) << str | ROS_INFO_THROTTLE(t, str) |
| First N occurrences | LOG_FIRST(I, n, str) | LOG_FIRST_N(INFO, n) << str | ROS_INFO_ONCE(str) (only 1) |
| Log to std::vector<string> | - | LOG_STRING(INFO, &vec) << str | - |
- Implement ROS_INFO_NAMED() etc.
- ROS2 support
The following information should be considered when contributing to Log++.
Unittests can be built by setting the LPP_BUILD_TESTS flag.
If building with catkin, the tests can be built with the following command:
$ catkin build lpp -DLPP_BUILD_TESTS=1Python binding tests are written with pytest. Run with:
uv run --with pytest pytest test/python- All modes (default, glog, lpp, roslog, nolog) have a separate test suite. All tests should run with each mode.
- Test all severity levels (Debug, Info, Warning, Error, Fatal)
- Test if the functionality of a logging function works, not only the logging format (Default, Conditional, Occasional, Timed, First N occurrences)
- Tests must run in debug mode in order to test debug log output
- Python tests mirror the C++ behavior groups for the method-based binding API.
Naming Convention:
TEST(<mode>_<LoggingMethod>, <mode>_syntax_severity_<severity>) {
//Test logic
}