Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef ELEKTRA_COLOREDKDB_IO_HPP
10 : #define ELEKTRA_COLOREDKDB_IO_HPP
11 :
12 : /*
13 : * @brief See examples/cpp_example_userio.cpp for how to use
14 : * USER_DEFINED_IO
15 : */
16 : #define USER_DEFINED_IO
17 :
18 : /* Do not include `keyio.hpp` and `keysetio.hpp` */
19 : #ifndef ELEKTRA_KDB_IO_HPP
20 : #define ELEKTRA_KDB_IO_HPP
21 :
22 : #include "ansicolors.hpp"
23 :
24 : #include <key.hpp>
25 :
26 : #include <iomanip>
27 : #include <ostream>
28 :
29 : namespace kdb
30 : {
31 :
32 1690 : inline std::ostream & printError (std::ostream & os, kdb::Key const & error, bool printVerbose, bool printDebug)
33 : {
34 : try
35 : {
36 11830 : if (!error.getMeta<const kdb::Key> ("error"))
37 : {
38 : // no error available
39 : return os;
40 : }
41 138 : os << "Sorry, module " << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::BLUE)
42 276 : << error.getMeta<std::string> ("error/module") << getErrorColor (ANSI_COLOR::RESET) << " issued the error "
43 322 : << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::RED) << error.getMeta<std::string> ("error/number")
44 644 : << getErrorColor (ANSI_COLOR::RESET) << ":" << std::endl;
45 598 : os << error.getMeta<std::string> ("error/description") << ": " << error.getMeta<std::string> ("error/reason") << std::endl;
46 :
47 46 : if (printVerbose)
48 : {
49 0 : os << getErrorColor (ANSI_COLOR::BOLD) << "Mountpoint: " << getErrorColor (ANSI_COLOR::RESET)
50 0 : << error.getMeta<std::string> ("error/mountpoint") << std::endl;
51 0 : os << getErrorColor (ANSI_COLOR::BOLD) << "Configfile: " << getErrorColor (ANSI_COLOR::RESET)
52 0 : << error.getMeta<std::string> ("error/configfile") << std::endl;
53 : }
54 :
55 46 : if (printDebug)
56 : {
57 0 : os << getErrorColor (ANSI_COLOR::BOLD) << "At: " << getErrorColor (ANSI_COLOR::RESET)
58 0 : << error.getMeta<std::string> ("error/file") << ":" << error.getMeta<std::string> ("error/line") << std::endl;
59 : }
60 : }
61 0 : catch (kdb::KeyTypeConversion const & e)
62 : {
63 0 : os << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::RED)
64 0 : << "Sorry, error metadata is not set correctly by a plugin: " << getErrorColor (ANSI_COLOR::RESET) << e.what ()
65 0 : << std::endl;
66 : }
67 :
68 : return os;
69 : }
70 :
71 3534 : inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error, bool printVerbose, bool printDebug)
72 : {
73 : try
74 : {
75 24738 : if (!error.getMeta<const kdb::Key> ("warnings"))
76 : {
77 : // no warnings were issued
78 : return os;
79 : }
80 :
81 12 : int nr = error.getMeta<int> ("warnings");
82 18 : os << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::MAGENTA) << " Sorry, " << nr + 1 << " warning"
83 21 : << (!nr ? " was" : "s were") << " issued ;(" << getErrorColor (ANSI_COLOR::RESET) << std::endl;
84 :
85 9 : for (int i = 0; i <= nr; i++)
86 : {
87 12 : std::ostringstream name;
88 24 : name << "warnings/#" << std::setfill ('0') << std::setw (2) << i;
89 : // os << "\t" << name.str() << ": " << error.getMeta<std::string>(name.str()) << std::endl;
90 18 : os << "\tSorry, module " << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::BLUE)
91 36 : << error.getMeta<std::string> (name.str () + "/module") << getErrorColor (ANSI_COLOR::RESET)
92 18 : << " issued the warning " << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::RED)
93 108 : << error.getMeta<std::string> (name.str () + "/number") << getErrorColor (ANSI_COLOR::RESET) << ":" << std::endl;
94 24 : os << "\t" << error.getMeta<std::string> (name.str () + "/description") << ": "
95 54 : << error.getMeta<std::string> (name.str () + "/reason") << std::endl;
96 6 : if (printVerbose)
97 : {
98 0 : os << getErrorColor (ANSI_COLOR::BOLD) << "\tMountpoint: " << getErrorColor (ANSI_COLOR::RESET)
99 0 : << error.getMeta<std::string> (name.str () + "/mountpoint") << std::endl;
100 0 : os << getErrorColor (ANSI_COLOR::BOLD) << "\tConfigfile: " << getErrorColor (ANSI_COLOR::RESET)
101 0 : << error.getMeta<std::string> (name.str () + "/configfile") << std::endl;
102 : }
103 6 : if (printDebug)
104 : {
105 0 : os << getErrorColor (ANSI_COLOR::BOLD) << "\tAt: " << getErrorColor (ANSI_COLOR::RESET)
106 0 : << error.getMeta<std::string> (name.str () + "/file") << ":"
107 0 : << error.getMeta<std::string> (name.str () + "/line") << std::endl;
108 : }
109 : }
110 : }
111 0 : catch (kdb::KeyTypeConversion const & e)
112 : {
113 0 : os << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::MAGENTA)
114 0 : << "Sorry, warnings metadata not set correctly by a plugin: " << getErrorColor (ANSI_COLOR::RESET) << e.what ()
115 0 : << std::endl;
116 : }
117 :
118 : return os;
119 : }
120 : } // namespace kdb
121 :
122 : #endif
123 : #endif
|