473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Redesigning a debug API

Hello NG,

I'm working on a larger C++ project with it's own debug functionality. The
typical debug call looks like this:

DEBUG(WARNING, "An error " << getErrorCategory() << " occured.");

The debug API consists of some complex macros. The macro aquires a debug
stream and pipes the second argument into that stream.

For several reasons I'm trying to get rid of those macros and replace them
with ordinary functions.

The problem: I can't refactor all debug calls. So the function should b
able to take arguments exactly like the makro above:

dbg::debug(WARNING, "An error " << getErrorCategory() << " occured.");

Any chance to make this happen? Somehow I must convince the compiler to
convert "An error " into some kind of object before streaming the rest of
the expression.

Transforming the whole mess into something nicer like

dbg::debug(WARNING) << "An error " << getErrorCategory() << " occured.";

But this wouldn't work, because I have to lock the stream for the complete
write, free the lock after writing the last item and flush the stream...

Any ideas?

Thanks
Thomas
Jul 23 '05 #1
3 1599
On Wed, 13 Jul 2005 14:52:10 +0400, Thomas Lorenz <Lo******@gmx.de> wrote:
Hello NG,

I'm working on a larger C++ project with it's own debug functionality.
The
typical debug call looks like this:

DEBUG(WARNING, "An error " << getErrorCategory() << " occured.");

The debug API consists of some complex macros. The macro aquires a debug
stream and pipes the second argument into that stream.

For several reasons I'm trying to get rid of those macros and replace
them with ordinary functions.


There is a slight chance you really want to do that. For the most part one
wants to completely skip evaluation of trace output arguments if it's not
going to make it into a log. The fastest way to do this is to use an if
statement. Writing if statements all over is boring, so you'll probably
end up using macros, something like this:

#define LOG_DETAIL_DO_LOG(lg, lvl, msg...) \
do { if((lg).does_accept(logging::lvl)) (lg)(__FILE__, __LINE__, msg);
} while(0)

#define LOG_DBG(lg, msg...) LOG_DETAIL_DO_LOG(lg, log_dbg, msg)

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #2
"Maxim Yegorushkin" <fi****************@gmail.com> wrote in
news:op***************@localhost.localdomain:
There is a slight chance you really want to do that. For the most part
one wants to completely skip evaluation of trace output arguments if
it's not going to make it into a log. The fastest way to do this is
to use an if statement. Writing if statements all over is boring, so
you'll probably end up using macros, something like this:

#define LOG_DETAIL_DO_LOG(lg, lvl, msg...) \
do { if((lg).does_accept(logging::lvl)) (lg)(__FILE__, __LINE__,
msg);
} while(0)

#define LOG_DBG(lg, msg...) LOG_DETAIL_DO_LOG(lg, log_dbg, msg)


That's right - but log level evaluation can also happen in the debug
function. OK, that leaves the slight overhead of the function call.

But using functions would allow me to perform other optimizations. Because
of the convoluted streaming taking place everywhere in the code, the DEBUG
macro looks like this:

#define DEBUG(level,out) \
{ \
std::ostringstream __o; \
__o << out; \
dbg::debug(level, __o.str()); \
}

But lots of the debug statements are plain text output:

DEBUG(INFO, "I'm at function foo()");

So the stringstream would not be necessary if the second parameter is a
string or can be converted into a string. Since macros are not type aware I
can't otimize for simple cases :(

Thomas
Jul 23 '05 #3
On Wed, 13 Jul 2005 15:47:44 +0400, Thomas Lorenz <Lo******@gmx.de> wrote:

Thomas, you might like taking a look at boost logging library which is
being developed now to get some ideas. Google for boost logging.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Davy | last post by:
Hi all, I use VC and gcc/gdb to compile and debug C/C++ files. But I found some of the debug version of the compiled files are too large to be run in a small RAM. Can I compile C/C++ Debug...
4
by: emma middlebrook | last post by:
I have a question regarding asserting ... here's some code: string GetAssertMessage() { ... prepare a message string and return it... } void SomeMethod() { ...
7
by: Srinivasa Rao | last post by:
I have read in one article that when we compile the application in release mode, all the debug classes and properties will be automatically removed from the code. I tried to implement this thing by...
9
by: dee | last post by:
Hi I'm about to upload my site and I have switched to release version. Is that enough or do I still need to disable <compilation defaultLanguage="vb" debug="true" /> the debug="true" in the .pdb...
6
by: swartzbill2000 | last post by:
Hello, I have a VB 2005 Express project with a TraceListener-derived class to route Debug.Print output to a log file. It works fine for Debug builds. What is the correct combination of changes to...
6
by: pauldepstein | last post by:
To help me debug, I am writing a lot of information into a stream which I call debug. However, because of the large amount of time taken to print this information, I only want this printed while...
6
by: Andrew Rowley | last post by:
I am having trouble getting debug and release builds to work properly with project references using C++ .NET and Visual Studio 2003. I created a test solution, with a basic Windows form C++...
3
by: rorni | last post by:
Hi, I'm porting code from Windows to HP-UX 11, compiling with g++. I'm getting a compilation error on the system's debug.h include file, which is included very indirectly through a series of...
19
by: Ganesh J. Acharya | last post by:
Hi there, I want to redesign my website and make that look professional. I made this about 6 years ago with very little knowledge of internet. Today I am getting about 4000 visitors a day for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.