Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3273190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:54:21+00:00 2026-05-17T18:54:21+00:00

I would like to use a generic error msg handler so I can GetLastError

  • 0

I would like to use a generic error msg handler so I can GetLastError and SetError easily for any class. I came up with this scheme. But I have a couple of questions. Please note this implementation is just for testing. But I want to get the basic design right.

#include <iostream>
#include <stdarg.h>

class ErrorHandler
{
    public:
        virtual void SetError(const char* zFormat, ...)
        {
            va_list args;
            va_start (args, zFormat);
            vsnprintf (z_ErrorBuf, sz_MaxBufSize, zFormat, args);
            va_end (args);
        }

        const char* GetError() const
        {
            return z_ErrorBuf;
        }

        virtual ~ErrorHandler(){}

        explicit ErrorHandler(const size_t szMaxBufSize = 1024)
        {
            z_ErrorBuf = malloc(sizeof(char)*szMaxBufSize);
            sz_MaxBufSize = szMaxBufSize;
        }

        void ResizeBuffer(const size_t szMaxBufSize)
        {
            z_ErrorBuf = realloc(z_ErrorBuf, szMaxBufSize);
            sz_MaxBufSize = szMaxBufSize;
        }

    protected:
        char* z_ErrorBuf;
        size_t sz_MaxBufSize;
};

class MyClass;

//Worker can be just an interface if needed. So, can't use friend.
class Worker
{
    public:
        void Work(MyClass& oGod);
};

void Worker::Work(MyClass& oGod)
{
    //Work
    //OnError
    oGod.GetErrorHandler().SetError("Save me %s", "not");
}

class RecordingErrors
{
    public:
        const char* GetLastError() const
        {
            return oErrorHandler.GetError();
        }

        //GetErrorHandler is public
        ErrorHandler& GetErrorHandler()
        {
            return oErrorHandler;
        }

    private:
        ErrorHandler oErrorHandler;
};

class MyClass : public RecordingErrors
{
    public:
        bool GetThingsDone(Worker& me)
        {
            me.Work(*this);

            //on Error
            return false;
        }
};

int main()
{
    MyClass oL;
    Worker w;
    if(!oL.GetThingsDone(w))
    {
        std::cout << oL.GetLastError() << std::endl;
    }
}
  1. Can I override this function in a child class? virtual void SetError(const char* zFormat, ...).
  2. Can I do away with the RecordingErrors class? I feel that then MyClass would have to inherit from ErrorHandler which I don’t think is good. Am I right or wrong? Is this another question about composition over inheritence. EDIT: I don’t mean the name, but the idea?
  3. Any possible scenarios this approach would fail?
  4. is there a better way to implement error logging?(logging is not the right word here. What is it)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:54:22+00:00Added an answer on May 17, 2026 at 6:54 pm

    Error Handler and logger are two different entities. Error handler decides what to do with the errors. Should it be send to logger immediately, should it be saved in databases, or should it be simply saved in some buffer till someone asks.
    Logger decides how to log the given message. Should it be shown on the console or should it be saved in the disk file, in what format it should be shown.

    Keep in mind the features of the logger.
    1) It should be independent class. It’s behavior should not depend on the other classes.
    2) Logger most preferable should be singleton. You don’t need many objects floating around doing the same thing. But then singleton classes has their own headaches when come to multi-threading. So I know this point is debatable.
    3) It must and must have capabilities of asynchronous logging. This means producer and consumer implementation. (Logging is an i/o operation and hence expensive in nature. You don’t want your main processing hogging for this. But then again you may not want to use threads to scrap this one.)

    In you implementation of error logger I can’t see any logger. Also your error handler is saving a single error. You may need the vector of errors. Keep SetError with fixed arguments. Pass arguments like error id, error message and error buffer length. Let the caller create the error message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to build a generic class that I can use to implement
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
I am trying to use Delphi 2010's TObjectDictionary generic. I would like to pass
aka, Seeking generic Error Handler (ΟΚ to use commercially) I doubt that I am
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted
I would like to use Maven's password encryption such as it uses for nodes
I would like to use the logout function from Django but not sure how
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using
I would like to use the jenkins script console some more. Where do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.