Error handling and file ouput

This is a discussion on Error handling and file ouput within the C++ Programming forums, part of the General Programming Boards category; I have a workaround to a problem that really feels like a kludge to me and I'd appreciate any recommendations ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Error handling and file ouput

    I have a workaround to a problem that really feels like a kludge to me and I'd appreciate any recommendations on how it might be rendered more "conceptually clean".

    I implemented a class exception handler, but it ouputs to two different streams. One goes out to stderr and just tells the user that an exception has occurred, and another goes out to a file handle with a very specific format (actually .dot format for graphviz) so that the user can later turn the error output into a postscript document.

    I thought of creating a separate error file for everytime my program throws an exception, but in the end decided to dump it all into one file. So here's how I implemented it in the header:
    Code:
    class A {
       static ofstream* os;
       void close();
    };
    Then in the source code:
    Code:
    static ofstream* A::os = new ofstream(XXX);
    
    A::close()
    {
       os->close();
       delete os;
    }
    So the program repeatedly instantiates A, and when there's an exception, it just dumps things into "os".

    Then, when main terminates, you have to instantiate a class, call close on it, and exit main. Obviously, this last step isn't necessary since if main terminates, everything's going to be wiped clean, but just to maintain best practices.

    Or maybe you could just
    Code:
    int main() {
       A::os->close();
       delete A::os;
    }
    Haven't tried it. But maybe it'll work.

    All of this seems very ugly to me. So any suggestions for improvement are welcome.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    I think you could use the singleton pattern here. Unfortunately I have not grasped just what is the best practice solution for implementing the singleton pattern in C++, so I cannot help you further.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks for the pointer. Looked up "singleton pattern" and it seems it would be a solution to making my implementation cleaner while making it safer for later users.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21