C ++ Error Reporting Interface

I am developing an interface that can be used to report errors in C ++. (I work with an outdated system in which an exception is out of the question.) In my young naivety, I started with these lines when developing my API:

bool DoStuff(int amount, string* error);

A return value indicates success / failure, while an error is used to communicate a human-readable explanation. So far, so good. The subroutine requests followed the error pointer, and everything was hard.

I ran into the following problems with this design (for now):

  • Unable to report alerts.
  • Unsafe

Then I decided to go with the following interface instead of a simple line:

class Issues {
 public:
  void Error(const string& message);
  void Warning(const string& message);
  void Merge(const Issues& issues);
}

So, I can change my API as follows:

bool DoStuff(int amount, Issues* issues);

, / API, ? , .

. . , , , , . , , . , .

+3
2

, boost::signals .NET , ///. - , , , ( , , , , ..),.

- , . :

boost::signal<void(std::string const&)> logError;
boost::signal<void(std::string const&)> logWarning;

void routineWhichMayFail()
{
    ...
    if (answer != 42)
    {
        logError("Universal error");
        return;
    }
}

- logError logWarning:

void robustErrorHandler(std::string const& msg)
{
    std::cerr << "Error: " << msg << "\n";
    std::exit(EXIT_FAILURE);
}

void initializeMyProgram()
{
    logError.connect(&robustErrorHandler);
}

, ( , "" - this, RPC ). , , . , , , . .

+3
+2

Source: https://habr.com/ru/post/1786431/


All Articles