IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main.cpp
Go to the documentation of this file.
1
3
4#include "irsol/assert.hpp"
5#include "irsol/logging.hpp"
6
7#include <math.h>
8
11const std::string
13{
14#ifndef PROGRAM_NAME
15#define PROGRAM_NAME "logging-and-asserting"
16#endif
17 return PROGRAM_NAME;
18}
19
20void
22{
23 IRSOL_LOG_TRACE("This is a trace message.");
24 IRSOL_LOG_DEBUG("This is a debug message.");
25 IRSOL_LOG_INFO("This is an info message.");
26 IRSOL_LOG_WARN("This is a warning message.");
27 IRSOL_LOG_ERROR("This is an error message.");
28 IRSOL_LOG_FATAL("This is a fatal message.");
29
30 IRSOL_NAMED_LOG_INFO("example", "This is an info message from a named logger '{}'.", "example");
31}
32
33void
35{
36 // Logging arguments has never been easier
38 "This is a log message with {} arguments: {} and {}", 2, "first-argument is a string", 0.5);
39 IRSOL_LOG_INFO("We can determine the precision for floating point numbers;");
40 IRSOL_LOG_INFO("PI: {}", M_PI);
41 IRSOL_LOG_INFO("PI: {:.2f}", M_PI);
42}
43
44void
46{
47 IRSOL_LOG_INFO("Testing assertions that pass...");
48
49 int value = 42;
50
51 IRSOL_LOG_TRACE("Running debug assertion");
52 IRSOL_ASSERT_DEBUG(value == 42, "Debug assertion passed: value is 42");
53 IRSOL_LOG_TRACE("Running error assertion");
54 IRSOL_ASSERT_ERROR(value == 42, "Error assertion passed: value is 42");
55 IRSOL_LOG_TRACE("Running fatal assertion");
56 IRSOL_ASSERT_FATAL(value == 42, "Fatal assertion passed: value is 42");
57
58 IRSOL_LOG_INFO("All passing assertions succeeded.");
59}
60
61void
63{
64 IRSOL_LOG_INFO("Testing assertions that fail...");
65
66 int value = 0;
67
68 IRSOL_LOG_INFO("Triggering Debug-level assertion failure. This should cause a warning message to "
69 "be printed to the console.");
70 IRSOL_ASSERT_DEBUG(value != 0, "Debug assertion failed: value should not be zero.");
71
72 IRSOL_LOG_INFO("Triggering Error-level assertion failure. This should cause an error message to "
73 "be printed to the console, and an irsol::AssertionException to be fired.");
74 try {
75 IRSOL_ASSERT_ERROR(value != 0, "Error assertion failed: value should not be zero");
76 } catch(const irsol::AssertionException& ex) {
77 IRSOL_LOG_ERROR("Caught AssertionException: {}", ex.what());
78 }
79
80 IRSOL_LOG_INFO("Continuing after catching Error-level assertion.");
81
82 IRSOL_LOG_INFO("Triggering Fatal-level assertion failure (program will terminate)...");
83 // Note: This will terminate the program. Uncomment to test.
84 // IRSOL_ASSERT_FATAL(value != 0, "Fatal assertion failed: value should not be zero");
85
86 IRSOL_LOG_INFO("This message will not be printed if Fatal assertion is triggered.");
87}
88
89int
91{
92 // Construct log file path based on program name
93 std::string logPath = "logs/" + getProgramName() + ".log";
94 // Initialize logging and assertion systems
95 irsol::initLogging(logPath.c_str());
97
98 IRSOL_LOG_INFO("Starting example application");
99
100 demoLogging();
102
105
106 return 0;
107}
Assertion macros and utilities based on the PPK_ASSERT library.
const std::string getProgramName()
Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time,...
Definition main.cpp:12
void demoPassingAssertions()
Definition main.cpp:45
#define PROGRAM_NAME
void demoLogging()
Definition main.cpp:21
void demoLoggingWithArguments()
Definition main.cpp:34
void demoFailingAssertions()
Definition main.cpp:62
int main()
Definition main.cpp:90
#define IRSOL_ASSERT_DEBUG
Debug-level assertion macro.
Definition assert.hpp:133
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
void initAssertHandler()
Initializes the assertion handler system.
Definition assert.cpp:14
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135
#define IRSOL_LOG_FATAL(...)
Logs a fatal (critical) message using the default logger.
Definition logging.hpp:95
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
Definition logging.hpp:176
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
#define IRSOL_LOG_WARN(...)
Logs a warning-level message using the default logger.
Definition logging.hpp:93
#define IRSOL_LOG_DEBUG(...)
Logs a debug-level message using the default logger.
Definition logging.hpp:91
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
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
Logging utilities and configuration for the irsol library.
ppk::assert::AssertionException AssertionException
Definition assert.hpp:149
auto value