ch3-DynamicDebugFlags

Chapter_3     Exercise_3-24 StringizingExpressions     Exercise_3-25







DynamicDebugFlags     TCP1, p. 209-210  (dynamic_debug_flags.c,  DynamicDebugFlags.cpp)




dynamic_debug_flags.c         download


#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strcmp()

typedef enum {FALSE, TRUE} bool; // user-defined typename
// Debug flags aren't necessarily global:
bool debug = FALSE;

int main(int argc, char* argv[])
{
int i;

for(i = 0; i < argc; i++)
{
if(strcmp(argv[i], "--debug=on") == 0)
{
debug = TRUE;
break; // out of for()
}
}

bool go = TRUE;
char reply[10];

while(go)
{
if(debug)
{
// Debugging code here
printf("Debugger is now on!\n");
}
else
{
printf("Debugger is now off.\n");
}

printf("Turn debugger [on/off/quit]: ");
scanf("%s", reply);
if(strcmp(reply, "on") == 0) {debug = TRUE;} // Turn it on
else if(strcmp(reply, "off") == 0) {debug = FALSE;} // Off
else if(strcmp(reply, "quit") == 0) {break;} // Out of while(), end program
}

return 0;
}
/*
// g++ dynamic_debug_flags.c // error: redefining built-in type `bool'
// gcc OK with used-defined typename `bool':
gcc dynamic_debug_flags.c -o dynamic_debug_flags
./dynamic_debug_flags
Debugger is now off.
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: q
Debugger is now off.
Turn debugger [on/off/quit]: quit

./dynamic_debug_flags --debug=on
Debugger is now on!
Turn debugger [on/off/quit]: q
Debugger is now on!
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: quit
*/





Note:  See also Debug_C_Program on TheGeekStuff, as well as ch1-debug on the blog Advanced_Linux_Programming, Chapter_1.











DynamicDebugFlags.cpp         download


#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;

// 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;
break; // out of for()
}
}

bool go = true;
string reply;

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]: ";
cin >> reply;
if(reply == "on") {debug = true;} // Turn it on
else if(reply == "off") {debug = false;} // Off
else if(reply == "quit") {break;} // Out of while(), end program
}

return 0;
}
/*
g++ DynamicDebugFlags.cpp -o DynamicDebugFlags
./DynamicDebugFlags
Debugger is now off.
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: q
Debugger is now off.
Turn debugger [on/off/quit]: quit

./DynamicDebugFlags --debug=on
Debugger is now on!
Turn debugger [on/off/quit]: q
Debugger is now on!
Turn debugger [on/off/quit]: on
Debugger is now on!
Turn debugger [on/off/quit]: off
Debugger is now off.
Turn debugger [on/off/quit]: quit
*/









Chapter_3     Exercise_3-24 BACK_TO_TOP StringizingExpressions     Exercise_3-25



Comments

Popular posts from this blog

Contents