[mrpt-system]

Overview

An OS abstraction layer and other miscellaneous functionality: filesystem, date and time, memory handling and much more.

Library mrpt-system

This C++ library is part of MRPT and can be installed in Debian-based systems with:

sudo apt install libmrpt-system-dev

Read also how to import MRPT into your CMake scripts.

Overview

mrpt-system gathers portable (Linux, Windows, macOS) utilities that most MRPT programs need:

Example: profiling a code block

#include <mrpt/system/CTimeLogger.h>
#include <mrpt/system/datetime.h>
#include <mrpt/system/filesystem.h>

mrpt::system::CTimeLogger profiler;  // prints its stats when destroyed

{
  // RAII: measures the time until the end of this scope
  mrpt::system::CTimeLoggerEntry tle(profiler, "prepare_output");

  const std::string dir = mrpt::system::pathJoin({mrpt::system::getcwd(), "out"});
  if (!mrpt::system::directoryExists(dir))
  {
    mrpt::system::createDirectory(dir);
  }
}

const mrpt::system::TTimeStamp t = mrpt::Clock::now();
std::cout << "Now: " << mrpt::system::dateTimeLocalToString(t) << "\n";

Examples

  • system_datetime_example: timestamps and date/time formatting.

  • system_dirs_files_manipulation: creating, renaming and deleting files and directories.

  • Example: system_directory_explorer_example, system_file_system_watcher: listing and watching directories.

  • system_control_rate_timer_example: running a loop at a fixed rate.

  • system_progress_bar: a console progress bar.

  • Python: mrpt_system_example.py.

Library contents

// structs

struct mrpt::system::CTimeLoggerEntry;
struct mrpt::system::CTimeLoggerSaveAtDtor;

// classes

class mrpt::system::CConsoleRedirector;
class mrpt::system::CControlledRateTimer;
class mrpt::system::CDirectoryExplorer;
class mrpt::system::CFileSystemWatcher;
class mrpt::system::CObservable;
class mrpt::system::CObserver;
class mrpt::system::COutputLogger;
class mrpt::system::CRateTimer;
class mrpt::system::CTicTac;
class mrpt::system::CTimeLogger;
class mrpt::system::mrptEvent;
class mrpt::system::mrptEventOnDestroy;

// global functions

std::string mrpt::system::hyperlink(
    const std::string& text,
    const std::string& uri,
    bool force_use_format = false,
    bool show_uri_anyway = false
    );

std::string mrpt::system::progress(
    const double progressRatio0to1,
    const std::size_t barLength,
    bool encloseInSquareBrackets = true
    );

void mrpt::system::thread_name(const std::string& name, std::thread& theThread);
void mrpt::system::thread_name(const std::string& name);
std::string mrpt::system::thread_name(std::thread& theThread);
std::string mrpt::system::thread_name();

Global Functions

std::string mrpt::system::hyperlink(
    const std::string& text,
    const std::string& uri,
    bool force_use_format = false,
    bool show_uri_anyway = false
    )

Build an text with a hyperlink, if printed to a terminal that supports it (e.g.

Ubuntu >=21.04), or the plain text otherwise.

(New in MRPT 2.11.8)

See also:

See discussion in, for example, https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda

std::string mrpt::system::progress(
    const double progressRatio0to1,
    const std::size_t barLength,
    bool encloseInSquareBrackets = true
    )

Build an text incremental progress bar with UNICODE block elements, for example:

50%  => "[████    ]"

See example: system_progress_bar/main.cpp

#include <mrpt/core/exceptions.h>
#include <mrpt/system/progress.h>

#include <chrono>
#include <iostream>
#include <thread>

void TestProgressBar()
{
  const int end = 400;
  const size_t barWidth = 30;

  for (int i = 0; i <= end; i++)
  {
    double p = double(i) / end;

    std::cout << "Progress: " << mrpt::system::progress(p, barWidth)
              << mrpt::format(" %5.02f%%", 100 * p) << "\r";
    std::cout.flush();
    std::this_thread::sleep_for(std::chrono::milliseconds(5));
  }
  std::cout << "\n";
}

(New in MRPT 2.3.0)

void mrpt::system::thread_name(const std::string& name, std::thread& theThread)

Sets the name of the given thread; useful for debuggers.

New in MRPT 2.0.4

void mrpt::system::thread_name(const std::string& name)

Sets the name of the current thread; useful for debuggers.

New in MRPT 2.0.4

std::string mrpt::system::thread_name(std::thread& theThread)

Gets the name of the given thread; useful for debuggers.

New in MRPT 2.0.4

std::string mrpt::system::thread_name()

Gets the name of the current thread; useful for debuggers.

New in MRPT 2.0.4