Pogosim
Loading...
Searching...
No Matches
utils.cpp File Reference
#include <iostream>
#include <string>
#include <filesystem>
#include <algorithm>
#include "utils.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/basic_file_sink.h"

Functions

std::mt19937 rnd_gen (rd())
void init_logger (Configuration &config)
 Initializes the global loggers.
void loggers_add_file_sink (std::string const &filename)
 Adds a file sink to the global loggers.
bool string_to_bool (std::string const &str)
 Converts a string to a boolean value.
std::string to_lowercase (std::string const &str)
 Converts a string to lowercase.
void ensure_directories_exist (const std::string &filename)
 Ensures that the directory for a given file exists.
void delete_files_with_extension (const std::string &path, const std::string &extension, bool recursive)
 Deletes files with a specified extension in a given directory.
std::string resolve_path (const std::string &inputPath)
 Resolves an input file path to an absolute path.

Variables

std::shared_ptr< spdlog::logger > glogger
 Global logger for general logging.
std::shared_ptr< spdlog::logger > robotlogger
 Global logger for robot-specific logging.
std::random_device rd
 Random device used for seeding the random generator.

Function Documentation

◆ delete_files_with_extension()

void delete_files_with_extension ( const std::string & path,
const std::string & extension,
bool recursive = false )

Deletes files with a specified extension in a given directory.

This function deletes all regular files with the specified extension within the given directory. If the recursive flag is true, it searches subdirectories as well; otherwise, it only searches the specified directory. Deletion operations and any errors encountered are logged.

Parameters
pathThe directory path where files will be deleted.
extensionThe file extension (including the dot) to match for deletion.
recursiveIf true, deletion is performed recursively in subdirectories.

◆ ensure_directories_exist()

void ensure_directories_exist ( const std::string & filename)

Ensures that the directory for a given file exists.

This function checks the parent directory of the provided filename. If the directory does not exist, it attempts to create it. Successful creation is logged using glogger. If an error occurs during directory creation, the error is logged.

Parameters
filenameThe file path for which the directories should be ensured.

◆ init_logger()

void init_logger ( Configuration & config)

Initializes the global loggers.

This function creates a console sink with color support and attaches it to two loggers: one for global logging (glogger) and one for robot-specific logging (robotlogger). The log level for both loggers is set to info and a specific log message format is defined.

◆ loggers_add_file_sink()

void loggers_add_file_sink ( std::string const & filename)

Adds a file sink to the global loggers.

This function ensures that the directory for the given filename exists, then creates a file sink that logs messages with debug level and a specified format. The file sink is then added to both the global logger (glogger) and the robot logger (robotlogger).

Parameters
filenameThe file path where the log file will be created.

◆ resolve_path()

std::string resolve_path ( const std::string & inputPath)

Resolves an input file path to an absolute path.

This function attempts to resolve the provided input path by first checking if the file exists in the current directory. If not found and if the DATA_DIR macro is defined, it checks relative to DATA_DIR. If the file is found in either location, the absolute path is returned; otherwise, an exception is thrown.

Parameters
inputPathThe relative or absolute path of the file.
Returns
std::string The resolved absolute path of the file.
Exceptions
std::runtime_errorIf the file cannot be found in the current directory or DATA_DIR.

◆ rnd_gen()

std::mt19937 rnd_gen ( rd() )

◆ string_to_bool()

bool string_to_bool ( std::string const & str)

Converts a string to a boolean value.

The conversion is case-insensitive. The function returns true if the input string is "true" or "1", and false if it is "false" or "0". For any other input, an std::invalid_argument exception is thrown.

Parameters
strThe input string to convert.
Returns
true if the string represents a boolean true.
false if the string represents a boolean false.
Exceptions
std::invalid_argumentIf the input string is not a valid boolean representation.

◆ to_lowercase()

std::string to_lowercase ( std::string const & str)

Converts a string to lowercase.

This function creates and returns a lowercase copy of the input string.

Parameters
strThe string to convert.
Returns
std::string A new string where all characters have been converted to lowercase.

Variable Documentation

◆ glogger

std::shared_ptr<spdlog::logger> glogger

Global logger for general logging.

◆ rd

std::random_device rd

Random device used for seeding the random generator.

◆ robotlogger

std::shared_ptr<spdlog::logger> robotlogger

Global logger for robot-specific logging.