Still having issues with ostreams, streambuf, filebufs etc etc etc..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Still having issues with ostreams, streambuf, filebufs etc etc etc..

  1. #1
    Join Date
    Nov 2006
    Posts
    103

    Still having issues with ostreams, streambuf, filebufs etc etc etc..

    This is a simple example of a log file class I'm trying to develop.

    Debug.h
    Code:
    #ifndef DEBUG_H
    #define DEBUG_H
    
    #include <iostream>
    #include <fstream>
    
    class dbgBuf : public std::streambuf {
    private:
    	std::streambuf* buffer;
    public:
    	dbgBuf(std::streambuf * output_buffer);
    };
    
    class dbgFile : public std::ostream {
    
    private:
    
    	std::filebuf output_buffer;
    	dbgBuf *indent_buffer;
    public:
    	dbgFile(void);
    	dbgFile(const char *path);
    	~dbgFile(void);
    };
    #endif
    Debug.cpp
    Code:
    #include "Debug.h"
    
    dbgBuf::dbgBuf(std::streambuf * output_buffer) : buffer(output_buffer) {
    }
    
    dbgFile::dbgFile(void) {
    }
    	
    dbgFile::dbgFile(const char *path) {
    		
    	if (output_buffer.is_open()) {
    		output_buffer.close();
    	}
    	if (output_buffer.open(path, std::ios_base::out)) {
    		rdbuf(indent_buffer);
    	}
    }
    	
    dbgFile::~dbgFile(void) {
    	if (output_buffer.is_open())
    		output_buffer.close();
    }
    main.cpp
    Code:
    int main (int argc, char * const argv[]) {
    	try
    	{
    		dbgFile dbg("/tmp/test.log");
    		dbg << "Hello, World!" << std::endl;
    	}
    	catch (...)
    	{
    		std::cerr << "Caught an exception" << std::endl;
    	}
        return 0;
    }

    This crashes on the dbg << "Hello world" line. Why I don't know...
    Last edited by JustSomeGuy; August 11th, 2009 at 03:40 PM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Still having issues with ostreams, streambuf, filebufs etc etc etc..

    You never point indent_buffer at any valid dbgBuf object.

    And I'd be extremely weary about inheriting from std::streambuf.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center