LCOV - code coverage report
Current view: top level - src/bindings/cpp/examples - cpp_example_userio.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 51 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 1 0.0 %

          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             : 
      25           0 : inline std::ostream & printError (std::ostream & os, kdb::Key const & error, bool printVerbose, bool printDebug)
      26             : {
      27           0 :         os << "User defined IO (errors)" << std::endl;
      28             : 
      29             :         try
      30             :         {
      31           0 :                 if (!error.getMeta<const kdb::Key> ("error"))
      32             :                 {
      33             :                         // no error available
      34             :                         return os;
      35             :                 }
      36           0 :                 os << "Sorry, module " << error.getMeta<std::string> ("error/module") << " issued the error "
      37           0 :                    << error.getMeta<std::string> ("error/number") << ":" << std::endl;
      38           0 :                 os << error.getMeta<std::string> ("error/description") << ": " << error.getMeta<std::string> ("error/reason") << std::endl;
      39             : 
      40           0 :                 if (printVerbose)
      41             :                 {
      42           0 :                         os << "Mountpoint: " << error.getMeta<std::string> ("error/mountpoint") << std::endl;
      43           0 :                         os << "Configfile: " << error.getMeta<std::string> ("error/configfile") << std::endl;
      44             :                 }
      45           0 :                 if (printDebug)
      46             :                 {
      47           0 :                         os << "At: " << error.getMeta<std::string> ("error/file") << ":" << error.getMeta<std::string> ("error/line")
      48           0 :                            << std::endl;
      49             :                 }
      50             :         }
      51           0 :         catch (kdb::KeyTypeConversion const & e)
      52             :         {
      53           0 :                 os << "Error metadata is not set correctly by a plugin" << std::endl;
      54             :         }
      55             : 
      56             :         return os;
      57             : }
      58             : 
      59           0 : inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error, bool printVerbose, bool printDebug)
      60             : {
      61           0 :         os << "User defined IO (warnings)" << std::endl;
      62             : 
      63             :         try
      64             :         {
      65           0 :                 if (!error.getMeta<const kdb::Key> ("warnings"))
      66             :                 {
      67             :                         // no warnings were issued
      68             :                         return os;
      69             :                 }
      70             : 
      71           0 :                 int nr = error.getMeta<int> ("warnings");
      72           0 :                 if (!nr)
      73             :                 {
      74           0 :                         os << "1 Warning was issued:" << std::endl;
      75             :                 }
      76             :                 else
      77             :                 {
      78           0 :                         os << nr + 1 << " Warnings were issued:" << std::endl;
      79             :                 }
      80             : 
      81           0 :                 for (int i = 0; i <= nr; i++)
      82             :                 {
      83           0 :                         std::ostringstream name;
      84           0 :                         name << "warnings/#" << std::setfill ('0') << std::setw (2) << i;
      85             :                         // os << "\t" << name.str() << ": " << error.getMeta<std::string>(name.str()) << std::endl;
      86           0 :                         os << "\tSorry, module " << error.getMeta<std::string> (name.str () + "/module") << " issued the warning "
      87           0 :                            << error.getMeta<std::string> (name.str () + "/number") << ":" << std::endl;
      88           0 :                         os << "\t" << error.getMeta<std::string> (name.str () + "/description") << ": "
      89           0 :                            << error.getMeta<std::string> (name.str () + "/reason") << std::endl;
      90           0 :                         if (printVerbose)
      91             :                         {
      92           0 :                                 os << "\tMountpoint: " << error.getMeta<std::string> (name.str () + "/mountpoint") << std::endl;
      93           0 :                                 os << "\tConfigfile: " << error.getMeta<std::string> (name.str () + "/configfile") << std::endl;
      94             :                         }
      95           0 :                         if (printDebug)
      96             :                         {
      97           0 :                                 os << "\tAt: " << error.getMeta<std::string> (name.str () + "/file") << ":"
      98           0 :                                    << error.getMeta<std::string> (name.str () + "/line") << std::endl;
      99             :                         }
     100             :                 }
     101             :         }
     102           0 :         catch (kdb::KeyTypeConversion const & e)
     103             :         {
     104           0 :                 os << "Warnings metadata not set correctly by a plugin" << std::endl;
     105             :         }
     106             : 
     107             :         return os;
     108             : }
     109             : 
     110             : #include <kdb.hpp>
     111             : 
     112             : #include <kdbio.hpp>
     113             : #include <keyio.hpp>
     114             : #include <keysetio.hpp>
     115             : 
     116           0 : int main ()
     117             : {
     118           0 :         kdb::Key k ("user/sw/MyApp", KEY_END);
     119           0 :         std::cout << k << std::endl;
     120             : 
     121           0 :         kdb::KeySet ks;
     122           0 :         ks.append (k);
     123           0 :         std::cout << ks;
     124             : 
     125             :         try
     126             :         {
     127           0 :                 kdb::KDB kdb (k);
     128           0 :                 kdb.get (ks, k);
     129             : 
     130           0 :                 std::cout << ks;
     131             : 
     132           0 :                 kdb.set (ks, k);
     133           0 :                 kdb.close (k);
     134           0 :                 printWarnings (std::cout, k, false, false);
     135             :         }
     136           0 :         catch (kdb::KDBException const & e)
     137             :         {
     138           0 :                 std::cout << e.what (); // will print user defined IO
     139             :         }
     140           0 : }

Generated by: LCOV version 1.13