Online Book Reader

Home Category

Professional C__ - Marc Gregoire [457]

By Root 1448 0
msLoggingEnabled; }

template

static void log(const Args&... args)

{

if (!msLoggingEnabled)

return;

ofstream ofs(msDebugFileName, ios_base::app);

if (ofs.fail()) {

cerr << "Unable to open debug file!" << endl;

return;

}

logHelper(ofs, args...);

ofs << endl;

}

protected:

template

static void logHelper(ofstream& ofs, const T1& t1)

{

ofs << t1;

}

template

static void logHelper(ofstream& ofs, const T1& t1, const Tn&... args)

{

ofs << t1;

logHelper(ofs, args...);

}

static const char* msDebugFileName;

static bool msLoggingEnabled;

};

const char* Logger::msDebugFileName = "debugfile.out";

bool Logger::msLoggingEnabled = false;

#define log(...) Logger::log(__func__, "(): ", __VA_ARGS__)

class ComplicatedClass

{

public:

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[])

{

Logger::enableLogging(isDebugSet(argc, argv));

if (Logger::isLoggingEnabled()) {

// Print the command-line arguments to the trace

for (int i = 0; i < argc; i++) {

log(argv[i]);

}

}

ComplicatedClass obj;

trickyFunction(&obj);

// 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)

{

log("given argument: ", *obj);

for (size_t i = 0; i < 100; ++i) {

UserCommand cmd = getNextCommand(obj);

log("retrieved cmd ", i, ": ", cmd);

try {

processUserCommand(cmd);

} catch (const exception& e) {

log("received exception from processUserCommand(): ", e.what());

throw;

}

}

}

Code snippet from StartTimeDebugMode\STDebug.cpp

There are two ways to run this application:

> STDebug

> STDebug -d

Debug mode will be activated only when the -d argument is specified on the command line.

Run-Time Debug Mode

The most flexible way to provide a debug mode is to allow it to be enabled or disabled at run time. One way to provide this feature is to supply an asynchronous interface that controls debug mode on-the-fly. In a GUI program, this interface could take the form of a menu command. In a CLI (Command Line Interface) program, this interface could be an asynchronous command that makes an interprocess call into the program (using sockets, signals, or remote procedure calls for example). C++ provides no standard way to perform interprocess communication or GUIs, so an example of this technique is not shown.

Ring Buffers

Debug mode is useful for debugging reproducible problems and for running tests. However, bugs often appear when the program is running in nondebug mode, and by the time you or the customer enables debug mode, it is too late to gain any information about the bug. One solution to this problem is to enable tracing in your program at all times. You usually need only the most recent traces to debug a program, so you should store only the most recent traces at any point in a program’s execution. One way to provide this limitation is through careful use of log file rotations.

However, in order to avoid the problems with logging traces described earlier in the “Error Logging” section, it is better if your program doesn’t log these traces continuously to disk; instead it should store them in memory.

Return Main Page Previous Page Next Page

®Online Book Reader