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

  • SEARCH
  • Home
  • 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 3307126
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:22:10+00:00 2026-05-17T21:22:10+00:00

I have a logger class. Call it MyLogger . I may use it in

  • 0

I have a logger class. Call it MyLogger. I may use it in a function like this:

void MyFunc(MyLogger& oLogger)
{
   //Do stuff
   oLogger.Log("In MyFunc : Some Error");
   //Do something else
   oLogger.Log("In MyFunc : Some other error");
}

Now, I want to prepend "In MyFunc" to the logs if the log comes from inside MyFunc. Similarly for other functions…

Because this is tiresome, I tried something like this:

void MyLogger::PushPrependString(const char*)
{
   //Store prepend string in stack and set it as current prepend string.
}

void MyLogger::PopPrependString()
{
   //Pop the most recent prepend string.
}

Now, I can use these two functions like this:

void MyFunc(MyLogger& oLogger)
{
   oLogger.PushPrependString("In MyFunc : ");
   //Do stuff
   oLogger.Log("Some Error");
   //Do something else
   oLogger.Log("Some other error");
   oLogger.PopPrependString();
}

The trouble is, if there are multiple returns in a function, this becomes ugly. Is there any way around this? Is this a common problem? Is there any preprocessor macro like __FILE__ or __LINE__ for getting the name of the function a line appears in? Any comments would be appreciated. Thanks.

  • 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-17T21:22:11+00:00Added an answer on May 17, 2026 at 9:22 pm

    RAII – Resource Acquisition Is Initialization.

    In this case, you create an object on entry to the function that identifies the current function to the logging system; when the function exits (by any return or by exception thrown or by exception not caught), the object will be destroyed, and the destructor changes what is printed in future by the logging system.

    In C99, and maybe in some C++ compilers such as G++, there is a predefined variable, __func__ containing the function name. The C++ equivalent is more complex, I believe.

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

Sidebar

Related Questions

I have a generic Logger class that looks like this: class Logger { ...
the situation is like this: im creating a Logger class that can write to
I have 4 classes: Server.cpp Logger.cpp Util.cpp Crypto.cpp The class Server use the object
I have code like this: class Debug(object): ... @property def ThreadAwareLogger(self): if not self.logger_instance_for_current_thread:
We have a class LogManager in our Java project which looks like this: public
I have code like: @notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>deliver_by >= '#{Date.today.to_s(:db)}') logger.info @upcoming_reminders[0].inspect
Currently I have a static class that I use as my logging module. I’ve
I have a class Logger which, among other things has a method Log .
I have a class Logger with a number of static methods for various user
For a given class I would like to have tracing functionality i.e. I would

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.