Skip to content

Commit

Permalink
Experimenting with spdlog (#615)
Browse files Browse the repository at this point in the history
* spdlog logging integration.

Signed-off-by: Michael Carroll <[email protected]>
Signed-off-by: Carlos Agüero <[email protected]>
Signed-off-by: Addisu Z. Taddese <[email protected]>
(cherry picked from commit 6cb5a53)
  • Loading branch information
mjcarroll authored and mergify[bot] committed Aug 29, 2024
1 parent 3f819f7 commit 3c221de
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 501 deletions.
2 changes: 2 additions & 0 deletions .github/ci/packages.apt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ libgdal-dev
libgz-cmake4-dev
libgz-math8-dev
libgz-utils3-dev
libgz-utils3-log-dev
libspdlog-dev
libswscale-dev
libtinyxml2-dev
pkg-config
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ set(GZ_MATH_VER ${gz-math8_VERSION_MAJOR})

#--------------------------------------
# Find gz-utils
gz_find_package(gz-utils3 REQUIRED)
gz_find_package(gz-utils3 REQUIRED COMPONENTS log)
set(GZ_UTILS_VER ${gz-utils3_VERSION_MAJOR})

#--------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ release will remove the deprecated code.

### Modifications

1. Logging has been heavily modified as we're relying on
[spdlog](https://github.com/gabime/spdlog). See
[example1](https://github.com/gazebosim/gz-utils/blob/gz-utils3/examples/log/main.cc)
and [example2]((https://github.com/gazebosim/gz-common/blob/gz-common6/examples/console.cc))
to learn how to access the internal spdlog logger and the global options.

The `gzdbg`, `gzmsg`, `gzlog`, `gzwarn` and `gzerr` macros should work the
same as previous versions but you now have two extra macros: `gzcrit` and
`gztrace` for logging critical errors and traces respectively.

`Console::SetVerbosity` only accepts values between 0 (critical errors only)
and 5 (all log messages). Other values don't have any effect.

1. Removed the `graphics` component's dependency on the GTS
(GNU Triangulated Surface) library which was used for doing triangulation
and CSG Boolean operation by the `MeshManager`. The Delaunay triangulation
Expand Down
45 changes: 33 additions & 12 deletions examples/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,43 @@
* limitations under the License.
*
*/
#include <gz/common.hh>
#include <filesystem>
#include <gz/common/Console.hh>

int main(int argc, char **argv)
{
// Default verbosity is 1, only error messages show
gzdbg << "This is a debug message" << std::endl;
gzmsg << "This is an informational message" << std::endl;
gzwarn << "This is a warning" << std::endl;
gzerr << "This is an error" << std::endl;
// Default verbosity is 3 (critical, error, warn and info messages show).
gztrace << "This is a trace message";
gzdbg << "This is a debug message";
gzmsg << "This is an informational message";
gzwarn << "This is a warning";
gzerr << "This is an error";
gzcrit << "This is a critical error";

// Change verbosity to level 4, all messages show
gz::common::Console::SetVerbosity(4);
gzdbg << "This is a debug message" << std::endl;
gzmsg << "This is an informational message" << std::endl;
gzwarn << "This is a warning" << std::endl;
gzerr << "This is an error" << std::endl;
// Change verbosity to level 5, all messages show.
gz::common::Console::SetVerbosity(5);
gz::common::Console::SetPrefix("My prefix. ");
gztrace << "This is a trace message";
gzdbg << "This is a debug message";
gzmsg << "This is an informational message";
gzwarn << "This is a warning";
gzerr << "This is an error";
gzcrit << "This is a critical error";

std::filesystem::path logDir = std::filesystem::temp_directory_path();
std::filesystem::path logFile = "my_log.txt";

gz::common::Console c("gz_tmp");
c.SetLogDestination(logDir / "tmp2" / logFile);
auto logger = c.RawLogger();
logger.log(spdlog::level::err, "Hello");

gz::common::Console::Init(logDir / "tmp3", logFile);
gzerr << "Error 1";
gzerr << "Error 2";
gzerr << "Directory: " << gz::common::Console::Directory();
gz::common::Console::Close();
gzerr << "Error 3";

return 0;
}
Loading

0 comments on commit 3c221de

Please sign in to comment.