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