Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief This examples show how Elektra’s KDBException can be changed in a way so that it has user defined output.
5 : *
6 : *
7 : * It works -- because of binary compatibility -- if only the receiver
8 : * of the message (where it is catched) redefines the IO functions
9 : * printError and printWarnings. They need to be defined with the
10 : * same signature and in either global or kdb namespace.
11 : *
12 : * The output operators of Key and KeySet can be redefined without any
13 : * macro by simply not including \<keyio.hpp\> and \<keysetio.hpp\>.
14 : *
15 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
16 : */
17 :
18 : #define USER_DEFINED_IO
19 :
20 : #include <iomanip>
21 : #include <iostream>
22 :
23 : #include <key.hpp>
24 : #include <keyset.hpp>
25 :
26 0 : inline std::ostream & printError (std::ostream & os, kdb::Key const & error, bool printVerbose, bool printDebug)
27 : {
28 0 : os << "User defined IO (errors)" << std::endl;
29 :
30 0 : try
31 : {
32 0 : if (!error.getMeta<const kdb::Key> ("error"))
33 : {
34 : // no error available
35 : return os;
36 : }
37 0 : os << "Sorry, module " << error.getMeta<std::string> ("error/module") << " issued the error "
38 0 : << error.getMeta<std::string> ("error/number") << ":" << std::endl;
39 0 : os << error.getMeta<std::string> ("error/description") << ": " << error.getMeta<std::string> ("error/reason") << std::endl;
40 :
41 0 : if (printVerbose)
42 : {
43 0 : os << "Mountpoint: " << error.getMeta<std::string> ("error/mountpoint") << std::endl;
44 0 : os << "Configfile: " << error.getMeta<std::string> ("error/configfile") << std::endl;
45 : }
46 0 : if (printDebug)
47 : {
48 0 : os << "At: " << error.getMeta<std::string> ("error/file") << ":" << error.getMeta<std::string> ("error/line")
49 0 : << std::endl;
50 : }
51 : }
52 0 : catch (kdb::KeyTypeConversion const & e)
53 : {
54 0 : os << "Error metadata is not set correctly by a plugin" << std::endl;
55 : }
56 :
57 : return os;
58 : }
59 :
60 0 : inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error, bool printVerbose, bool printDebug)
61 : {
62 0 : os << "User defined IO (warnings)" << std::endl;
63 :
64 0 : try
65 : {
66 : // TODO: use C++ binding version of keyMeta
67 0 : kdb::KeySet meta (ckdb::ksDup (ckdb::keyMeta (error.getKey ())));
68 0 : kdb::Key parent ("meta:/warnings", KEY_END);
69 0 : auto warnings = meta.cut (parent);
70 :
71 0 : if (warnings.size () == 0)
72 : {
73 0 : return os;
74 : }
75 0 : else if (warnings.size () == 1)
76 : {
77 0 : os << "1 Warning was issued:" << std::endl;
78 : }
79 : else
80 : {
81 0 : os << warnings.size () << " Warnings were issued:" << std::endl;
82 : }
83 :
84 0 : for (auto it = warnings.begin () + 1; it != warnings.end (); ++it)
85 : {
86 0 : auto name = it->getName ();
87 0 : if (it->isDirectBelow (parent))
88 : {
89 0 : os << "\tSorry, module " << warnings.get<std::string> (name + "/module") << " issued the warning "
90 0 : << warnings.get<std::string> (name + "/number") << ":" << std::endl;
91 0 : os << "\t" << warnings.get<std::string> (name + "/description") << ": "
92 0 : << warnings.get<std::string> (name + "/reason") << std::endl;
93 : // os << "\t" << name << ": " << warnings.get<std::string>(name) << std::endl;
94 0 : if (printVerbose)
95 : {
96 0 : os << "\tMountpoint: " << warnings.get<std::string> (name + "/mountpoint") << std::endl;
97 0 : os << "\tConfigfile: " << warnings.get<std::string> (name + "/configfile") << std::endl;
98 : }
99 0 : if (printDebug)
100 : {
101 0 : os << "\tAt: " << warnings.get<std::string> (name + "/file") << ":"
102 0 : << warnings.get<std::string> (name + "/line") << std::endl;
103 : }
104 : }
105 : }
106 : }
107 0 : catch (kdb::KeyTypeConversion const & e)
108 : {
109 0 : os << "Warnings metadata not set correctly by a plugin" << std::endl;
110 : }
111 :
112 : return os;
113 : }
114 :
115 : #include <kdb.hpp>
116 :
117 : #include <kdbio.hpp>
118 : #include <keyio.hpp>
119 : #include <keysetio.hpp>
120 :
121 0 : int main ()
122 : {
123 0 : kdb::Key k ("user:/sw/MyApp", KEY_END);
124 0 : std::cout << k << std::endl;
125 :
126 0 : kdb::KeySet ks;
127 0 : ks.append (k);
128 0 : std::cout << ks;
129 :
130 0 : try
131 : {
132 0 : kdb::KDB kdb (k);
133 0 : kdb.get (ks, k);
134 :
135 0 : std::cout << ks;
136 :
137 0 : kdb.set (ks, k);
138 0 : kdb.close (k);
139 0 : printWarnings (std::cout, k, false, false);
140 : }
141 0 : catch (kdb::KDBException const & e)
142 : {
143 0 : std::cout << e.what (); // will print user defined IO
144 : }
145 0 : }
|