C++ API Example (Logger Module) - 5.2 English - 57404

AOCL User Guide (57404)

Document ID
57404
Release Date
2025-12-29
Version
5.2 English
#include <iostream>
#include <thread>
#include <vector>

#include "Au/Logger/LogManager.hh"

#include "Au/Logger/Macros.hh"

using namespace Au::Logger;

void
log_cpp_api()
{
   std::unique_ptr<ILogger> consoleLogger =
      LoggerFactory::createLogger("ConsoleLogger", "Main Console");

   // Set the logger to the singleton LogWriter
   LogWriter::setLogger(std::move(consoleLogger));

   // Get the singleton instance of LogWriter
   auto logWriter = LogWriter::getLogWriter();

   LogManager logger(logWriter);

   for (int i = 0; i < 10; ++i) {
      Message msg("This is a message " + std::to_string(i));
      logger.log(msg);
   }

   logger.flush();

   logger << Message("This is a message using operator '<<'");
   logger << "This is a string message using operator '<<'";

   logger.flush();
   logWriter->stop(); // Add this line to stop the logger thread
}

void
log_macro()
{
   AU_LOGGER_LOG_INFO("This is an info message using macro");
   AU_LOGGER_LOG_WARN("This is a warning message using macro");
   AU_LOGGER_LOG_ERROR("This is an error message using macro");
   AU_LOGGER_LOG_FATAL("This is a fatal message using macro");
   AU_LOGGER_LOG_PANIC("This is a panic message using macro");
   AU_LOGGER_LOG_TRACE("This is a trace message using macro");
   AU_LOGGER_LOG_DEBUG("This is a debug message using macro");
   AU_LOGGER_LOG_NOTICE("This is a notice message using macro");
}

int
main(int argc, char const* argv[])
{
   log_cpp_api();
   log_macro();
   return 0;
}

Expected Output

Run build/Release/cpuid_example_c on your system:

$ ./Release/cpuid_example_c
AMD CPU detected...

Run any of the examples in the examples folder similarly.

Integration from Command Line

Export the libaoclutils binaries path into LD_LIBRARY_PATH variable:

$ export LD_LIBRARY_PATH=<path of libaoclutils binaries>:${LD_LIBRARY_PATH}

Using Static Library:

# C application
$ gcc -std=gnu11 test_c_application.c -o test_c_application \
  -L<path of libaoclutils binaries> -l:libaoclutils.a -lstdc++ \
  -I<path of libaoclutils include directory>

# C++ application
$ g++ -std=gnu++17 test_cpp_application.cc -o test_cpp_application \
  -L<path of libaoclutils binaries> -l:libaoclutils.a \
  -I<path of libaoclutils include directory>

Using Dynamic/Shared Library:

# C application
$ gcc -std=gnu11 test_c_application.c -o test_c_application \
  -L<path of libaoclutils binaries> -l:libaoclutils.so \
  -I<path of libaoclutils include directory>

# C++ application
$ g++ -std=gnu++17 test_cpp_application.cc -o test_cpp_application \
  -L<path of libaoclutils binaries> -l:libaoclutils.so \
  -I<path of libaoclutils include directory>