IRSOL
C++ code implementing socket server for interacting with Baumer camera.
main.cpp File Reference

Demonstrates how to get log and assert within the irsol project. More...

#include "irsol/assert.hpp"
#include "irsol/logging.hpp"
#include <math.h>

Go to the source code of this file.

Macros

#define PROGRAM_NAME   "logging-and-asserting"
 

Functions

const std::string getProgramName ()
 Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time, returns "logging-and-asserting".
 
void demoLogging ()
 
void demoLoggingWithArguments ()
 
void demoPassingAssertions ()
 
void demoFailingAssertions ()
 
int main ()
 

Detailed Description

Demonstrates how to get log and assert within the irsol project.

Definition in file main.cpp.

Macro Definition Documentation

◆ PROGRAM_NAME

#define PROGRAM_NAME   "logging-and-asserting"

Function Documentation

◆ demoFailingAssertions()

void demoFailingAssertions ( )

Definition at line 62 of file main.cpp.

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}
#define IRSOL_ASSERT_DEBUG
Debug-level assertion macro.
Definition assert.hpp:133
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
ppk::assert::AssertionException AssertionException
Definition assert.hpp:149
auto value

◆ demoLogging()

void demoLogging ( )

Definition at line 21 of file main.cpp.

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}
#define IRSOL_LOG_FATAL(...)
Logs a fatal (critical) message using the default logger.
Definition logging.hpp:95
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
Definition logging.hpp:176
#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

◆ demoLoggingWithArguments()

void demoLoggingWithArguments ( )

Definition at line 34 of file main.cpp.

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}

◆ demoPassingAssertions()

void demoPassingAssertions ( )

Definition at line 45 of file main.cpp.

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}
#define IRSOL_ASSERT_FATAL
Fatal-level assertion macro.
Definition assert.hpp:135

◆ getProgramName()

const std::string getProgramName ( )

Returns the program name, typically used for logging. If PROGRAM_NAME is not defined at compile time, returns "logging-and-asserting".

Definition at line 12 of file main.cpp.

13{
14#ifndef PROGRAM_NAME
15#define PROGRAM_NAME "logging-and-asserting"
16#endif
17 return PROGRAM_NAME;
18}
#define PROGRAM_NAME

◆ main()

int main ( )

Definition at line 90 of file main.cpp.

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}
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
void demoLogging()
Definition main.cpp:21
void demoLoggingWithArguments()
Definition main.cpp:34
void demoFailingAssertions()
Definition main.cpp:62
void initAssertHandler()
Initializes the assertion handler system.
Definition assert.cpp:14
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