sazr
sazr

Reputation: 25928

Printf inside constructor not showing in console

I have something weird happening where printf commands (inside a class constructor) are not being output to the console window.

Do you know why this would happen?

Some relevant information:

Do you know why this would happen?

My code:

// Public Static Class Variables //
const tstring TVManager::TASK_NAME          = _T("TV Manager");
const tstring TVManager::TASK_TIME_STAMP    = _T("2012-03-22T13:46:00");

// Private constructor
TVManager::TVManager(HWND hwnd)
{
    mainHwnd = hwnd;

    bool res = isTVManagerTaskScheduled();
    std::cout << "Res: " << res << endl; // does not print to console
    _tprintf(_T("RES: %d\n"), res);      // does not print to console

    if (!res) {
        _tprintf(_T("hit\n"));
        EasyTaskScheduler::ScheduleTaskAtLogon(TASK_NAME, CPP_Utilities::getProcessPath(), TASK_TIME_STAMP);
    }
}

// Public Static function //
bool TVManager::isTVManagerTaskScheduled()
{
    std::vector <tstring> curTasks = EasyTaskScheduler::RetrieveScheduledTasks();
    tstring defTaskName            = CPP_Utilities::toLower( TASK_NAME );

    for (int i=0; i<=curTasks.size(); i++) {
        tstring task = CPP_Utilities::toLower(curTasks.at(i));
        // The following printf doesn't get printed to console
        _tprintf(_T("size %d, Task %d: %s\n"), curTasks.size(), i, task.c_str());
        if (task.find( defTaskName ) != npos) {
            _tprintf(_T("returning true\n"));
            return true;
        }
    }

    _tprintf(_T("returning false\n"));
    return false;
}

// Public static function
TVManager* TVManager::getInstance(HWND hwnd)
{
    static TVManager instance(hwnd);
    return &instance;
}

// Usage: Inside main window proceedure
case WM_CREATE:
{
    CPP_Utilities::openConsoleWindow();
    tvManager = TVManager::getInstance(hwnd);
}
break;

Upvotes: 2

Views: 1821

Answers (2)

sazr
sazr

Reputation: 25928

The problem was I was accessing an invalid vector element here:

for (int i=0; i<=curTasks.size(); i++) {
   tstring task = CPP_Utilities::toLower(curTasks.at(i));

It should be:

for (int i=0; i<curTasks.size(); i++) {

The weird thing is that it never threw a runtime error or crashed. It just continue working except it didn't output anything to the console.

Upvotes: 1

Chris Desjardins
Chris Desjardins

Reputation: 2720

The absolute easiest, and most portable solution is to just open an output file with fopen or ofstream, and write to it, you can also output to the MSVC output console:

How do I print to the debug output window in a Win32 app?

but obviously that limits you to debugging with MSVC.

Upvotes: 0

Related Questions