Skip to content

Commit

Permalink
Merge branch 'config-script' into 'master'
Browse files Browse the repository at this point in the history
Update README.md to reflect proper path include

See merge request altairLab/optcontrol/libmpc!40
  • Loading branch information
nicolapiccinelli committed Jan 30, 2024
2 parents 4b74b0f + f685c51 commit 53c065f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 89 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [0.4.1] - 2024-01-30
### Changed
- Removed coloured output for the integrated logger
- Configure script now can be used to avoid the installation of the test suite

### Fixed
- Fixed dockerfile to use the 0.6.3 version of the OSQP solver

## [0.4.0] - 2023-05-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ RUN git clone https://github.com/stevengj/nlopt /tmp/nlopt \
##############################
# OSQP Solver
##############################
RUN git clone --recursive https://github.com/osqp/osqp /tmp/osqp \
RUN git clone --depth 1 --branch v0.6.3 --recursive https://github.com/osqp/osqp /tmp/osqp \
&& cd /tmp/osqp \
&& mkdir build \
&& cd build \
Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ If you are a developer, to setup the debug environment you also need to install:
The latest version of libmpc++ is available from GitHub https://github.com/nicolapiccinelli/libmpc/releases and does not require any
installation process other than the one required by its dependecies.

### System wide usage on an Ubuntu Linux
If you're a developer run the `configure.sh` script with superuser privileges to install the external dependencies
```
sudo ./configure.sh
```
or `configure.sh --disable-test` if you do not need the test-suite
```
sudo ./configure.sh --disable-test
```
Then, after installing all the dependencies configure and compile the library using CMake. From the folder containing
the CMakeLists.txt file compile the library by doing
```
mkdir build
cd build
cmake ..
make
```
Finally, install the library with superuser privileges
```
sudo make install
```
This will allow you to install the MPC++ library on your system. Using the include
```
#include <mpc/LMPC.hpp>
```
in your package will be enough to add this library to your project.

### CMakelists.txt example
This is an example of a CMakeLists.txt file to use libmpc++ in your project.

Expand All @@ -36,7 +63,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
find_package(mpc++ CONFIG REQUIRED)
# # Declare a C++ library
# Declare a C++ library
include_directories(${mpc++_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} mpc++)
Expand All @@ -52,4 +79,4 @@ sudo sh get-docker.sh
You can build the development image with the command below. Note that the image will be tagged with the name specified after the '-t' option.
```console
docker build -t dev_image .
```
```
35 changes: 26 additions & 9 deletions configure.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/usr/bin/env bash

# read the parameter --disable-test from the command line
# to choose if you want to disable the test suite installation
# default is to install the test suite
while test $# -gt 0; do
case "$1" in
--disable-test)
shift
DISABLE_TEST=1
;;
*)
break
;;
esac
done

# install eigen3 from apt
apt-get install -y libeigen3-dev

Expand Down Expand Up @@ -32,12 +47,14 @@ cmake --build . --target install
# this is the test suite
#########################

# install catch2 from master branch >= 3.x
cd ../../
git clone https://github.com/catchorg/Catch2.git
cd Catch2
mkdir build
cd build
cmake ..
make -j
make install
if [ -z ${DISABLE_TEST+x} ]; then
# install catch2 from master branch >= 3.x
cd ../../
git clone https://github.com/catchorg/Catch2.git
cd Catch2
mkdir build
cd build
cmake ..
make -j
make install
fi
2 changes: 1 addition & 1 deletion include/mpc/IComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace mpc
{
if (!isInitialized)
{
Logger::instance().log(Logger::log_type::ERROR) << RED << "MPC library is not initialized, quitting..." << RESET << std::endl;
Logger::instance().log(Logger::log_type::ERROR) << "MPC library is not initialized, quitting..." << std::endl;
exit(-1);
}
}
Expand Down
19 changes: 0 additions & 19 deletions include/mpc/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,6 @@
#include <ostream>
#include <string>

//the following are UBUNTU/LINUX, and MacOS ONLY terminal color codes.
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */

#ifdef ERROR
#undef ERROR
#endif
Expand Down
143 changes: 86 additions & 57 deletions test/test_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,117 @@
#include "basic.hpp"
#include <catch2/catch_test_macros.hpp>

TEST_CASE("Checking logger prefix", "[logging]")
TEST_CASE("Logger outputs log messages based on log level", "[Logger]")
{
std::stringstream ss;
mpc::Logger::instance().
setStream(&ss).
setPrefix("test").
setLevel(mpc::Logger::log_level::NORMAL).
log(mpc::Logger::log_type::INFO) << "a";

REQUIRE(ss.str().find("test") != std::string::npos);
}

TEST_CASE("Checking logger verbosity", "[logging]")
{
std::stringstream ss;
mpc::Logger::instance().reset().setStream(&ss);
// Set up the logger
mpc::Logger &logger = mpc::Logger::instance();
std::ostringstream oss;
logger.setStream(&oss);

SECTION("Level NONE")
SECTION("Log level set to NONE")
{
mpc::Logger::instance().setLevel(mpc::Logger::log_level::NONE);
mpc::Logger::instance().log(mpc::Logger::log_type::DETAIL) << "a";
REQUIRE(ss.str().size() == 0);

ss.str("");

mpc::Logger::instance().log(mpc::Logger::log_type::INFO) << "a";
REQUIRE(ss.str().size() == 0);
logger.setLevel(mpc::Logger::log_level::NONE);

ss.str("");
SECTION("Log messages are not output regardless of log type")
{
logger.log(mpc::Logger::log_type::DETAIL) << "Detail message";
logger.log(mpc::Logger::log_type::INFO) << "Info message";
logger.log(mpc::Logger::log_type::ERROR) << "Error message";

mpc::Logger::instance().log(mpc::Logger::log_type::ERROR) << "a";
REQUIRE(ss.str().size() == 0);
REQUIRE(oss.str().empty());
}
}

SECTION("Level DEEP")
SECTION("Log level set to DEEP")
{
mpc::Logger::instance().setLevel(mpc::Logger::log_level::DEEP);
mpc::Logger::instance().log(mpc::Logger::log_type::DETAIL) << "a";
REQUIRE(ss.str().size() > 0);
logger.setLevel(mpc::Logger::log_level::DEEP);

ss.str("");
SECTION("Log messages are output when log level allows")
{
logger.log(mpc::Logger::log_type::DETAIL) << "Detail message";
logger.log(mpc::Logger::log_type::INFO) << "Info message";
logger.log(mpc::Logger::log_type::ERROR) << "Error message";

mpc::Logger::instance().log(mpc::Logger::log_type::INFO) << "a";
REQUIRE(ss.str().size() > 0);
REQUIRE(oss.str() == "[MPC++] Detail message[MPC++] Info message[MPC++] Error message");
}
}

SECTION("Log level set to NORMAL")
{
logger.setLevel(mpc::Logger::log_level::NORMAL);

ss.str("");
SECTION("Log messages are output for INFO and ERROR log types")
{
logger.log(mpc::Logger::log_type::DETAIL) << "Detail message";
logger.log(mpc::Logger::log_type::INFO) << "Info message";
logger.log(mpc::Logger::log_type::ERROR) << "Error message";

mpc::Logger::instance().log(mpc::Logger::log_type::ERROR) << "a";
REQUIRE(ss.str().size() > 0);
REQUIRE(oss.str() == "[MPC++] Info message[MPC++] Error message");
}
}

SECTION("Level INFO")
SECTION("Log level set to ALERT")
{
mpc::Logger::instance().setLevel(mpc::Logger::log_level::NORMAL);
mpc::Logger::instance().log(mpc::Logger::log_type::DETAIL) << "a";
REQUIRE(ss.str().size() == 0);
logger.setLevel(mpc::Logger::log_level::ALERT);

SECTION("Log messages are output for ERROR log type")
{
logger.log(mpc::Logger::log_type::DETAIL) << "Detail message";
logger.log(mpc::Logger::log_type::INFO) << "Info message";
logger.log(mpc::Logger::log_type::ERROR) << "Error message";

ss.str("");
REQUIRE(oss.str() == "[MPC++] Error message");
}
}
}

mpc::Logger::instance().log(mpc::Logger::log_type::INFO) << "a";
REQUIRE(ss.str().size() > 0);
TEST_CASE("Logger sets prefix for log messages", "[Logger]")
{
// Set up the logger
mpc::Logger &logger = mpc::Logger::instance();
std::ostringstream oss;
logger.setLevel(mpc::Logger::log_level::NORMAL);
logger.setStream(&oss);

SECTION("Prefix is set")
{
logger.setPrefix("PREFIX");

ss.str("");
SECTION("Log messages include the prefix")
{
logger.log(mpc::Logger::log_type::INFO) << "Message";

mpc::Logger::instance().log(mpc::Logger::log_type::ERROR) << "a";
REQUIRE(ss.str().size() > 0);
REQUIRE(oss.str() == "[MPC++ PREFIX] Message");
}
}

SECTION("Level ERROR")
SECTION("Prefix is not set")
{
mpc::Logger::instance().setLevel(mpc::Logger::log_level::ALERT);
mpc::Logger::instance().log(mpc::Logger::log_type::DETAIL) << "a";
REQUIRE(ss.str().size() == 0);
logger.setPrefix("");

ss.str("");
SECTION("Log messages do not include the prefix")
{
logger.log(mpc::Logger::log_type::INFO) << "Message";

mpc::Logger::instance().log(mpc::Logger::log_type::INFO) << "a";
REQUIRE(ss.str().size() == 0);
REQUIRE(oss.str() == "[MPC++] Message");
}
}
}

ss.str("");
TEST_CASE("Logger reset function", "[Logger]")
{
// Set up the logger
mpc::Logger &logger = mpc::Logger::instance();
std::ostringstream oss;
logger.setStream(&oss);
logger.setPrefix("PREFIX");
logger.setLevel(mpc::Logger::log_level::DEEP);

SECTION("Logger is reset to default configuration")
{
logger.reset();
logger.log(mpc::Logger::log_type::DETAIL) << "Info message";

mpc::Logger::instance().log(mpc::Logger::log_type::ERROR) << "a";
REQUIRE(ss.str().size() > 0);
REQUIRE(oss.str().empty());
}
}

0 comments on commit 53c065f

Please sign in to comment.