IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main.cpp
Go to the documentation of this file.
1#define CATCH_CONFIG_RUNNER
2#include "irsol/logging.hpp"
3#include "irsol/macros.hpp"
4
5#include <catch2/catch_all.hpp>
6#include <iostream>
7#include <string>
8#include <vector>
9
10class LoggingListener : public Catch::EventListenerBase
11{
12public:
13 using Catch::EventListenerBase::EventListenerBase;
14
15 void testRunStarting(IRSOL_MAYBE_UNUSED Catch::TestRunInfo const& testRunInfo) override
16 {
17 irsol::setLoggerName("unit-tests");
19 IRSOL_LOG_INFO("Starting unittests.");
20 }
21 void testRunEnded(IRSOL_MAYBE_UNUSED Catch::TestRunStats const& testRunStats) override
22 {
23 irsol::setLoggerName("unit-tests");
24 IRSOL_LOG_INFO("Test run ended.");
25 }
26
27 void testCaseStarting(Catch::TestCaseInfo const& testInfo) override
28 {
29 std::string testCaseDescription = "";
30 for(const auto& tag : testInfo.tags) {
31 testCaseDescription += tag.original + "][";
32 }
33 if(!testCaseDescription.empty()) {
34 testCaseDescription.pop_back(); // remove trailing '['
35 testCaseDescription += "[" + testInfo.name;
36 } else {
37 testCaseDescription = testInfo.name;
38 }
39
40 irsol::setLoggerName(testCaseDescription.c_str());
41 IRSOL_LOG_INFO("Starting testcase: {}-{}.", testInfo.tagsAsString(), testInfo.name);
42 }
43 void testCaseEnded(IRSOL_MAYBE_UNUSED Catch::TestCaseStats const& testCaseStats) override
44 {
45 IRSOL_LOG_INFO("Testcase ended.");
46 }
47};
48
49CATCH_REGISTER_LISTENER(LoggingListener)
50
51std::vector<std::string>
52configureLogging(int argc, char** argv)
53{
54 std::vector<std::string> filteredArgs;
55
56 std::string logLevelStr = "";
57 bool shouldSkipNext = false;
58 for(int i = 0; i < argc; ++i) {
59 if(shouldSkipNext) {
60 shouldSkipNext = false;
61 continue;
62 }
63 std::string arg = argv[i];
64 if(arg == "--log-level") {
65 if(i + 1 >= argc) {
66 std::cerr << "Error: --log-level requires an argument\n";
67 std::exit(1);
68 }
69 logLevelStr = argv[i + 1];
70 shouldSkipNext = true;
71 } else {
72 filteredArgs.emplace_back(argv[i]);
73 }
74 }
75
76 spdlog::level::level_enum parsedLoglevel;
77 if(logLevelStr != "") {
78 parsedLoglevel = spdlog::level::from_str(logLevelStr);
79 if(parsedLoglevel == spdlog::level::off) {
80 std::cerr << "Error: Invalid log level provided: '" << logLevelStr << "'\n";
81 std::exit(1);
82 }
83 irsol::initLogging("logs/unit-tests.log", parsedLoglevel);
84 } else {
85 // Logging disabled
86 std::cout << "Logging disabled.\n";
87 irsol::initLogging("logs/unit-tests.log", spdlog::level::off);
88 }
89
90 return filteredArgs;
91}
92
93int
94main(int argc, char** argv)
95{
96 auto filteredArgs = configureLogging(argc, argv);
97 // Convert to char* array for Catch2
98 std::vector<char*> filteredArgsForCatch;
99 for(auto& s : filteredArgs) {
100 filteredArgsForCatch.push_back(s.data());
101 }
102
103 int result = Catch::Session().run(
104 static_cast<int>(filteredArgsForCatch.size()), filteredArgsForCatch.data());
105
106 return result;
107}
void testCaseStarting(Catch::TestCaseInfo const &testInfo) override
Definition main.cpp:27
void testCaseEnded(IRSOL_MAYBE_UNUSED Catch::TestCaseStats const &testCaseStats) override
Definition main.cpp:43
void testRunEnded(IRSOL_MAYBE_UNUSED Catch::TestRunStats const &testRunStats) override
Definition main.cpp:21
void testRunStarting(IRSOL_MAYBE_UNUSED Catch::TestRunInfo const &testRunInfo) override
Definition main.cpp:15
int main()
Definition main.cpp:90
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
void setLoggerName(const char *name)
Sets the name of the default logger.
Definition logging.cpp:77
void initLogging(const char *fileSinkFilename="logs/irsol.log", std::optional< spdlog::level::level_enum > minLogLevel=std::nullopt)
Initializes the irsol logging system.
Definition logging.cpp:108
void setLoggingFormat(LoggingFormat format=LoggingFormat::FILE, std::optional< std::shared_ptr< spdlog::logger > > logger=std::nullopt)
Sets the global logging format.
Definition logging.cpp:84
@ UNIT_TESTS
Format suitable for unit test frameworks.
Logging utilities and configuration for the irsol library.
Common portability and diagnostic macros for the irsol library.
#define IRSOL_MAYBE_UNUSED
Suppresses compiler warnings about unused variables or parameters.
Definition macros.hpp:39
auto result
std::vector< std::string > configureLogging(int argc, char **argv)
Definition main.cpp:52