⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 assertdebug.cpp

📁 C++高级编程这本书所附的源代码
💻 CPP
字号:
// 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;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -