473,807 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 " << getErrorCategor y() << " 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(WARN ING, "An error " << getErrorCategor y() << " 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(WARN ING) << "An error " << getErrorCategor y() << " 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 1617
On Wed, 13 Jul 2005 14:52:10 +0400, Thomas Lorenz <Lo******@gmx.d e> 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 " << getErrorCategor y() << " 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_L OG(lg, lvl, msg...) \
do { if((lg).does_ac cept(logging::l vl)) (lg)(__FILE__, __LINE__, msg);
} while(0)

#define LOG_DBG(lg, msg...) LOG_DETAIL_DO_L OG(lg, log_dbg, msg)

--
Maxim Yegorushkin
<fi************ ****@gmail.com>
Jul 23 '05 #2
"Maxim Yegorushkin" <fi************ ****@gmail.com> wrote in
news:op******** *******@localho st.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_L OG(lg, lvl, msg...) \
do { if((lg).does_ac cept(logging::l vl)) (lg)(__FILE__, __LINE__,
msg);
} while(0)

#define LOG_DBG(lg, msg...) LOG_DETAIL_DO_L OG(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::ostringstr eam __o; \
__o << out; \
dbg::debug(leve l, __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.d e> 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
2700
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 partially? Something like: fileA.c fileB.c And I can compile fileA.c with debug info and compile fileB.c without debug info?
4
4533
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
2919
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 using the following code in Page_Load event handler. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim intcount As Integer intcount = 0 For intcount = 0 To 4
9
2002
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 file? Is the .pdb necessary for the release version? Thanks a bunch. Dara
6
4123
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 make it work in Release build? Bill
6
3666
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 I am in debugging mode as indicated by a preprocessor directive: #define DEBUG In other words, I need code which says "Whenever I write debug <<
6
9144
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++ project. I then add a class library, and add a reference to this project in the first project. When I do a release build, I see the following in the output from the DLL compile: /OUT:"C:\Documents and Settings\Andrew\My Documents\Visual Studio
3
3272
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 other system include files. The one I am including is <map> . The errors I am getting are: /opt/hp-gcc-4.2.1/lib/gcc/ia64-hp-hpux11.23/4.2.1/../../../../include/c++/4.2.1/debug/debug.h: At global scope:...
19
3318
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 the same. What are the things I need to keep in my mind when doing this process.
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.