Best way to go about a debug.txt system?

This is a discussion on Best way to go about a debug.txt system? within the C++ Programming forums, part of the General Programming Boards category; So basically, I want a text file that displays all the processes the program runs up until the time it ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Best way to go about a debug.txt system?

    So basically, I want a text file that displays all the processes the program runs up until the time it is closed.. Basically a list of all the functions executed during runtime..

    What is the best way about going about this? Should I just make each function when executed write a line to the file?

    Say function foo gets executed..

    In the debug.txt file it would look like

    Running foo
    Foo successfully executed..

    if it doesn't get to the end of foo

    Running foo
    Foo failed to execute

    (generally if foo fails the program will crash, but this will let me know exactly what went wrong.)


    Any suggestions?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    I just implemented something like this you can create a generic write function and then #include it in the files you want it used. I have no idea if this is a great idea but it works.
    Code:
    #include "logger.h"
    
    #ifdef DEBUG
        std::ofstream out;
        void Logger(const char *write, bool error)
        {        
            if(error)
            {
                out.open("Error.txt",std::ios::app);
            }
            else
            {            
                out.open("Log.txt",std::ios::app);
            }
            out<<write<<std::endl<<std::endl;
            
            out.close();
            
        }
    #endif//DEBUG
    
    #ifndef DEBUG
        void Logger(const char *write, bool error)
        {        
        }
    #endif//DEBUG
    So on a non debug build the code basically does nothing.
    Woop?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > Any suggestions?
    Use a debugger.

    Logging, as described by prog-bman is more useful in the field, when you want to get some clue (any clue) as to what is going wrong with your program.

    But if you're busy writing and testing, a debugger has got to be a better choice, because you can examine much more detail of the internal state of the program.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Here is my system. Essentially the same idea except w/o the debug checks.

    Code:
    #ifndef CLOG
    #define CLOG
    
    
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    
    class CLog
    {
      
      public:
        ofstream outFile;
        CString strFilename;
        CString strDate;
        CString strTime;
    
        CLog(void):strFilename(""),strDate(""),strTime("") {}
    
        
        virtual ~CLog(void)
        {
          outFile.flush();
          outFile.close();
        }
    
        void Open(CString _filename)
        {
          strFilename=_filename+".txt";
    
          outFile.open((LPCSTR)strFilename);
    
          WriteHeader();
        
        }
    
        void Close(void)
        {
          outFile << endl << "End of file - closing file" << endl;
          outFile.close();
        }
    
        void WriteHeader(void)
        {
          CTime time=CTime::GetCurrentTime();
    
          strTime=time.Format("%H:%M:%S %p");
          strDate=time.Format("%a %b %d, %Y");
          outFile << (LPCSTR)strFilename << endl;
          outFile << (LPCSTR)strDate << endl; 
          outFile << (LPCSTR)strTime << endl;
          outFile << "****************************************************" << endl;
        }
    
    };
    
    #endif
    I make the stream public so you can write just about anything you want to it by simply directly accessing the stream. Not much different but these are extremely invaluable classes to have on hand. Simple and yet so powerful. Has helped me track down numerous bugs, leaks, etc.

    It uses MFC CString but you can alter that.
    Last edited by VirtualAce; 01-08-2006 at 04:52 AM.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Thanks bubba, looks pretty nifty.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 06:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 04:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 06:12 AM

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