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 318955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:37:02+00:00 2026-05-12T08:37:02+00:00

what is good practice for generating verbose output? currently, i have a function bool

  • 0

what is good practice for generating verbose output? currently, i have a function

bool verbose;
int setVerbose(bool v)
{
    errormsg = "";
    verbose = v;
    if (verbose == v)
        return 0;
    else
        return -1;
}

and whenever i want to generate output, i do something like

if (debug)
     std::cout << "deleting interp" << std::endl;

however, i don’t think that’s very elegant. so i wonder what would be a good way to implement this verbosity switch?

  • 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-12T08:37:02+00:00Added an answer on May 12, 2026 at 8:37 am

    The simplest way is to create small class as follows(here is Unicode version, but you can easily change it to single-byte version):

    #include <sstream>
    #include <boost/format.hpp>
    #include <iostream>
    using namespace std;
    
    enum log_level_t {
        LOG_NOTHING,
        LOG_CRITICAL,
        LOG_ERROR,
        LOG_WARNING,
        LOG_INFO,
        LOG_DEBUG
    };
    
    namespace log_impl {
    class formatted_log_t {
    public:
        formatted_log_t( log_level_t level, const wchar_t* msg ) : fmt(msg), level(level) {}
        ~formatted_log_t() {
            // GLOBAL_LEVEL is a global variable and could be changed at runtime
            // Any customization could be here
            if ( level <= GLOBAL_LEVEL ) wcout << level << L" " << fmt << endl;
        }        
        template <typename T> 
        formatted_log_t& operator %(T value) {
            fmt % value;
            return *this;
        }    
    protected:
        log_level_t     level;
        boost::wformat      fmt;
    };
    }//namespace log_impl
    // Helper function. Class formatted_log_t will not be used directly.
    template <log_level_t level>
    log_impl::formatted_log_t log(const wchar_t* msg) {
        return log_impl::formatted_log_t( level, msg );
    }
    

    Helper function log was made template to get nice call syntax. Then it could be used in the following way:

    int main ()
    {
        // Log level is clearly separated from the log message
        log<LOG_DEBUG>(L"TEST %3% %2% %1%") % 5 % 10 % L"privet";
        return 0;
    }
    

    You could change verbosity level at runtime by changing global GLOBAL_LEVEL variable.

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

Sidebar

Related Questions

No related questions found

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.