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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:14:58+00:00 2026-06-17T09:14:58+00:00

consider my logging class void LogWriter::InitLogWriter(void) { wcout.flush(); wcerr.flush(); COUT_BACKUP = wcout.rdbuf(); // save

  • 0

consider my logging class

void LogWriter::InitLogWriter(void)
{
    wcout.flush();
    wcerr.flush();
    COUT_BACKUP = wcout.rdbuf(); // save original cout buffer
    CERR_BACKUP = wcerr.rdbuf(); //save original cerr buffer
    FileStreamOpen = false;
    switch(m_nTraceLvl)
    {
    case OffLevel:
        StopOutput();
        break;
    case ErrorLevel:
        OutputErrorsToFile(s_TraceFile);
        break;
    case DetailLevel:
        OutputAllToFile(s_TraceFile);
        break;
    default:
        StopOutput();
        break;
    }
    wcout << "Initialize Log Writer" << endl;
}
void LogWriter::OutputAllToFile(TCHAR* fileName)
{
    wstreambuf* fb = GetFileBufferStream(fileName);

    wcout.rdbuf(fb); // push wcout buffer to file
    wcerr.rdbuf(fb); // push cerr buffer to file

    FileStreamOpen = true;
}
void LogWriter::OutputErrorsToFile(TCHAR* fileName)
{
    wstreambuf* fb = GetFileBufferStream(fileName);
    wcerr.rdbuf(fb);
    FileStreamOpen = true;
    wofstream fout("/dev/null");
    wcout.rdbuf(fout.rdbuf()); // redirect 'cout' to a 'fout'
}
void LogWriter::StopOutput(void)
{
    wofstream fout("/dev/null");

    wcout.rdbuf(fout.rdbuf()); // redirect 'cout' to a 'fout'
    wcerr.rdbuf(fout.rdbuf()); // redirect cerr to fout
}
wstreambuf* LogWriter::GetFileBufferStream(TCHAR* fileName)
{
    filestr.open(fileName);
    return filestr.rdbuf();   // get file's streambuf
}
void LogWriter::Dispose()
{
    wcout << "Kill Log Writer" << endl;
    wcout.rdbuf(COUT_BACKUP); // restore the original stream buffer
    wcerr.rdbuf(CERR_BACKUP);

    if(FileStreamOpen)
    {
        filestr.close();
    }
}

Now if i set my Tracing to DetailLevel (2) all is fine. All my logging goes to the file. But if i choose OffLevel or ErrorLevel my program crashes.. my debugger doesn’t turn on so i added cout << “.” on every line (i hate doing that, but it works) and it points me to

wcout.rdbuf(fout.rdbuf()); // redirect 'cout' to a 'fout'

as being my culprit… I switched around redirecting wcout first with wcerr being redirected and it worked (but wcerr was not called yet)

so that is my question? am I supposed to dispose wcout first? I tried flushing first..that didn’t work.. so i don’t know. What am I doing wrong?

Also as a bonus.. how come all my output doesn’t go to the file until I close my LogWriter? IE LogWriter::Dispose()

  • 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-06-17T09:14:59+00:00Added an answer on June 17, 2026 at 9:14 am

    You crash because wofstream fout("/dev/null"); is created on the stack and is destroyed as soon as you leave the function.

    Treat /dev/null exactly like you treat the file. That is, call OutputErrorsToFile("/dev/null") inside StopOutput(). In fact you don’t even need /dev/null: just use wcout.rdbuf(nullptr), which should work faster too.

    To write into the file immediately call .flush() or use std::flush manipulator.

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

Sidebar

Related Questions

Consider the following class definitions: import com.codahale.logula.Logging abstract class A extends Logging { log.info(from
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker<File, Void>() { private
Consider the following code: Class Demo { Person person = new Person(foo); // assume
To paint a scenario, consider the following logic-omitted-Service public class Service { private Validator
Consider this code: import logging print print logging.error(log) I get: print ERROR:root:log now if
I have a class that implements Runnable. For logging reasons I want to know
Consider a following code: struct X { void MethodX() { ... } }; struct
consider the below class public class Player { private int id; public int getId()
When writing a new application or infrastructure, do you consider logging right from the

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.