gbase
Loading...
Searching...
No Matches
test_gbase.cc
Go to the documentation of this file.
1
21#include "gbase.h"
22
23constexpr const char* G1_LOGGER = "hello1";
24constexpr const char* G2_LOGGER = "hello2";
25
26// derived with its own logger
27class g1 : public GBase<g1>
28{
29public:
30 int object1 = 2;
31
32 explicit g1(const std::shared_ptr<GOptions>& gopt) : GBase(gopt, G1_LOGGER) {
33 log->info(0, "hello derived class ", SFUNCTION_NAME);
34 }
35
36 void doSomething([[maybe_unused]] int a = 0) {
37 log->info(0, "doing something ");
38 log->debug(NORMAL, FUNCTION_NAME, "debug message ");
39 log->warning(FUNCTION_NAME, "warning message ");
40 }
41
42 ~g1() = default;
43};
44
45// derived with shared logger
46class g2 : public GBase<g2>
47{
48public:
49 int object1 = 2;
50
51 explicit g2(const std::shared_ptr<GLogger>& log) : GBase(log) {
52 log->info(0, "hello derived class ", SFUNCTION_NAME);
53 }
54
55 void doSomething([[maybe_unused]] int a = 0) {
56 log->info(0, "doing something ");
57 log->debug(NORMAL, FUNCTION_NAME, "debug message ");
58 log->warning(FUNCTION_NAME, "warning message ");
59 }
60
61 ~g2() = default;
62};
63
64
65// returns this example options
69
70 // command line switch
71 goptions.defineSwitch("light", "a switch, this is just an example.");
72
73 return goptions;
74}
75
76int main(int argc, char* argv[]) {
77 auto gopts = std::make_shared<GOptions>(argc, argv, defineOptions());
78 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, G2_LOGGER);
79
80 g1 obj1(gopts);
81 g2 obj2(log);
82
83
84 obj1.doSomething();
85 obj2.doSomething();
86
87 return EXIT_SUCCESS;
88}
CRTP base class that provides logging facilities to the derived class.
Definition gbase.h:116
std::shared_ptr< GLogger > log
Shared logger used by the derived class for emitting messages.
Definition gbase.h:256
void warning(Args &&... args) const
void debug(debug_type type, Args &&... args) const
void info(int level, Args &&... args) const
void doSomething(int a=0)
Definition test_gbase.cc:36
int object1
Definition test_gbase.cc:30
~g1()=default
g1(const std::shared_ptr< GOptions > &gopt)
Definition test_gbase.cc:32
~g2()=default
int object1
Definition test_gbase.cc:49
void doSomething(int a=0)
Definition test_gbase.cc:55
g2(const std::shared_ptr< GLogger > &log)
Definition test_gbase.cc:51
Lightweight CRTP base class that provides a pre-configured logger to derived types.
#define FUNCTION_NAME
#define SFUNCTION_NAME
NORMAL
int main(int argc, char *argv[])
GOptions defineOptions()
Definition test_gbase.cc:66
constexpr const char * G2_LOGGER
Definition test_gbase.cc:24
constexpr const char * G1_LOGGER
Definition test_gbase.cc:23