topical media & game development

talk show tell print

professional-program-20-Asserts-AssertDebug.c

? / professional-program-20-Asserts-AssertDebug.c


  // STDebug.cpp
  include <exception>
  include <fstream>
  include <iostream>
  include <cassert>
  using namespace std;
  
  ofstream debugOstr;
  bool debug = false;
  
  const char* debugFileName = "debugfile.out";
  
  class ComplicatedClass
  {
  public:
    ComplicatedClass() {}
    ~ComplicatedClass() {}
  };
  
  class UserCommand
  {
  public:
    UserCommand() {}
  };
  
  bool isDebugSet(int argc, char** argv);
  ostream& operator<<(ostream& ostr, const ComplicatedClass& src);
  ostream& operator<<(ostream& ostr, const UserCommand& src);
  UserCommand getNextCommand(ComplicatedClass* obj);
  void processUserCommand(UserCommand& cmd);
  void trickyFunction(ComplicatedClass* obj) throw(exception);
  
  int main(int argc, char** argv)
  {
  
    debug = isDebugSet(argc, argv);
    if (debug) {
      // open the output stream
      debugOstr.open(debugFileName);
      if (debugOstr.fail()) {
        cout << "Unable to open debug file!\n";
        return (1);
      }
  
      // Print the command-line arguments
      for (int i = 0; i < argc; i++) {
        debugOstr << argv[i] << " ";
        debugOstr << endl;
      }
    }
  
    ComplicatedClass obj;
    trickyFunction(NULL);
  
    // Rest of the function not shown
    return (0);
  }
  
  bool isDebugSet(int argc, char** argv)
  {
    for (int i = 0; i < argc; i++) {
      if (strcmp(argv[i], "-d") == 0) {
        return (true);
      }
    }
    return (false);
  }
  
  ostream& operator<<(ostream& ostr, const ComplicatedClass& src)
  {
    ostr << "ComplicatedClass";
    return (ostr);
  }
  
  ostream& operator<<(ostream& ostr, const UserCommand& src)
  {
    ostr << "UserCommand";
    return (ostr);
  }
  
  UserCommand getNextCommand(ComplicatedClass* obj)
  {
    UserCommand cmd;
    return (cmd);
  }
  
  void processUserCommand(UserCommand& cmd)
  {
    // details omitted for brevity
  }
  
  void trickyFunction(ComplicatedClass* obj) throw(exception)
  {
    assert(obj != NULL);
  
    if (debug) {
      // If in debug mode, print the values with which this function starts
      debugOstr << "trickyFunction(): given argument: " << *obj << endl;
    }
  
    while (true) {
      UserCommand cmd = getNextCommand(obj);
      if (debug) {
        debugOstr << "trickyFunction(): retrieved cmd " << cmd << endl;
      }
      try {
        processUserCommand(cmd);
      } catch (exception& e) {
        if (debug) {
          debugOstr << "trickyFunction(): "
                    << " received exception from procesUserCommand(): "
                    << e.what() << endl;
        }
        throw;
      }
    }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.