Thread: Best way to go about a debug.txt system?

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

    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,820
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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 05:52 AM.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07: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, 05:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM