C++编程思想 第1卷 第3章 调试技巧 调试标记 运行期调试标记

执行期间打开或关闭调试标记会方便
可以使用命令行在启动程序时设置它们
如果只是为了插入调试代码重新编译一次会乏味


为自动打开和关闭调试代码,建立一个bool标记

//: C03:DynamicDebugFlags.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include <iostream>
#include <string>
using namespace std;
// Debug flags aren't necessarily global:
bool debug = false;

int main(int argc, char* argv[]) {
  for(int i = 0; i < argc; i++)
    if(string(argv[i]) == "--debug=on")
      debug = true;
  bool go = true;
  while(go) {
    if(debug) {
      // Debugging code here
      cout << "Debugger is now on!" << endl;
    } else {
      cout << "Debugger is now off." << endl;
    }  
    cout << "Turn debugger [on/off/quit]: ";
    string reply;
    cin >> reply;
    if(reply == "on") debug = true; // Turn it on
    if(reply == "off") debug = false; // Off
    if(reply == "quit") break; // Out of 'while'
  }
} ///:~
输入quit可以退出 
需要输入整个单词
启动是 可以用命令行参数打开调试
参数可以在任何地方 
参数测试的表达式 string(argv[i]) == "--debug=on"
argv[i]是一个字符串数组
用== 比较
查找字符串 --debug=on
也可以查找字符串 --debug= 看它后面有什么 以便选择
用小写字母书写变量,变量不是预处理器标记




命令行输入参数
--debug=on
输出
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: on
Debugger is now on!


猜你喜欢

转载自blog.csdn.net/eyetired/article/details/80782797
今日推荐