Log File Program: problems btw compilers

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 04/30/04


Date: Fri, 30 Apr 2004 03:15:14 GMT

Is there anything technically wrong with the code below?
I compiled it with g++ 3.3.1 and it executes with no errors.
I compiled it with bcc32 5.6 and it executes with "This
program has performed an illegal operation."
I am running this on Windows 98 platform.

{If not, I'll post this issue to a Borland newsgroup.}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <streambuf>
using std::filebuf;
using std::streamsize;

class Log_Stream_Buffer : public std::filebuf
{
   public:
                                 Log_Stream_Buffer();
                                 Log_Stream_Buffer(const char * filename);
                                 ~Log_Stream_Buffer();

     void disable_writing(void);
     void enable_writing(void);
     bool writing_disabled(void) const;
     bool writing_enabled(void) const;

   protected:
     virtual int_type overflow (int_type c);
     virtual streamsize xsputn(const char * s, streamsize num);

   private:
                                 Log_Stream_Buffer(const
Log_Stream_Buffer& lb);
     bool enabled;
};

inline void Log_Stream_Buffer::disable_writing(void)
{
     enabled = false;
     return;
}

inline void Log_Stream_Buffer::enable_writing(void)
{
     enabled = true;
     return;
}

inline bool Log_Stream_Buffer::writing_disabled(void) const
{
     return !enabled;
}

inline bool Log_Stream_Buffer::writing_enabled(void) const
{
     return enabled;
}

Log_Stream_Buffer::Log_Stream_Buffer()
   : filebuf(),
     enabled(true)
{
     open("unnamed_log.txt", ios_base::out);
}

Log_Stream_Buffer::Log_Stream_Buffer(const char * filename)
   : filebuf(),
     enabled(true)
{
     open(filename, ios_base::out);
}

Log_Stream_Buffer::~Log_Stream_Buffer()
{
}

filebuf::int_type Log_Stream_Buffer::overflow(int_type c)
{
     if (enabled && (c != EOF))
     {
         filebuf::overflow(c);
     }
     return c;
}

streamsize Log_Stream_Buffer::xsputn(const char * s, streamsize num)
{
     if (enabled)
     {
         filebuf::xsputn(s, num);
     }
     return num;
}

class Log_File : public std::ostream
{
   public:
                                 Log_File();
                                 Log_File(const char * filename);
                                 ~Log_File();

     Log_Stream_Buffer * close(void);
     bool is_paused(void) const;
     void pause(void);
     void resume(void);
     Log_Stream_Buffer * open(const char * filename);

   private:
                                 Log_File(const Log_File& lf);
     static Log_Stream_Buffer log_buf;
};

inline Log_Stream_Buffer * Log_File::close(void)
{
     return dynamic_cast<Log_Stream_Buffer *>(log_buf.close());
}

inline bool Log_File::is_paused(void) const
{
     return log_buf.writing_enabled();
}

inline void Log_File::pause(void)
{
     log_buf.disable_writing();
     return;
}

inline void Log_File::resume(void)
{
     log_buf.enable_writing();
     return;
}

inline Log_Stream_Buffer * Log_File::open(const char * filename)
{
     return dynamic_cast<Log_Stream_Buffer *>
            (log_buf.open(filename, std::ios_base::out));
}

Log_Stream_Buffer Log_File::log_buf;

Log_File::Log_File()
   : ostream(&log_buf)
{
}

Log_File::Log_File(const char * filename)
   : ostream(&log_buf)
{
     log_buf.open(filename, std::ios_base::out);
}

Log_File::~Log_File()
{
}

Log_File test_log("log.txt");

int main(void)
{

     test_log << "This sentence should be in the file.\n";
     test_log.pause();
     test_log << "This sentence should NOT be in the file\n";
     test_log.resume();
     test_log << "The last entry.\n";
     test_log.flush();
     return EXIT_SUCCESS;
}

When I ran this through the Borland IDE, I got
an exception error in _ios.c in the
basic_ios<>::imbue function:
template <class _CharT, class _Traits>
locale basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
{
   locale __tmp = ios_base::imbue(__loc);

   if (_M_streambuf)
     _M_streambuf->pubimbue(__loc); //***** Error occurs here ******

   // no throwing here
   this->_M_cached_ctype = __loc._M_get_facet(ctype<char_type>::id) ;
   this->_M_cached_numpunct =
        __loc._M_get_facet(numpunct<char_type>::id) ;
   this->_M_cached_grouping =
((numpunct<char_type>*)_M_cached_numpunct)->grouping() ;
   return __tmp;
}

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
     http://www.josuttis.com  -- C++ STL Library book
     http://www.sgi.com/tech/stl -- Standard Template Library


Relevant Pages

  • Re: help
    ... > problem is it only executes the first if statement and nothing else. ... > int month; ... C++ Faq: http://www.parashift.com/c++-faq-lite ... http://www.josuttis.com -- C++ STL Library book ...
    (comp.lang.cpp)
  • Re: threads without threads
    ... > I have a counter loop which actually executes in a different thread. ... Does ANSI C provide any inherent function ... you _could_ write a wrapper function for your platform ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)
  • Re: LDAP Connection
    ... >In the above code, when it executes the first if condition, ... >always becoming true. ... MVP VC++ FAQ: http://www.mvps.org/vcfaq ...
    (microsoft.public.vc.language)
  • [PATCH V2 1/3] kernel.h: Add DO_ONCE statement expression macro
    ... that executes any arbitrary statement exactly once. ... container_of - cast a member of a structure out to the containing structure ... More majordomo info at http://vger.kernel.org/majordomo-info.html ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • Re: scripts wont execute
    ... > but one executes as expected, ... cgi-bin directory has been created with the right security context. ... You can take a look the faq here ... Rahul Sundaram -- fedora-list mailing list ...
    (Fedora)