CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  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.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured