473,325 Members | 2,792 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,325 software developers and data experts.

templatized logging

I'd like to include some very simple logging in my app - removable at
compile time.

Avoiding macros .. I want to make sure this nonsense would be
optimized away by the compiler if I don't need it.
template<bool b>
void
log(const char* msg)
{
if (b == true)
{
std::cout << msg << std::endl;
}
}

....

const bool is_log_on = false;

int
main(int argc, char* argv)
{
...
log<is_log_on>("I am here.");
...
}

Since "is_log_on" evaluates to false at compile time, I'm hoping the
compiler optimizes it away so there is absolutely no affect on
performance. Is there a well-known problem with this approach or
extending it into a full blown out logging template class?

<offtopic>
Google and my ISPs newsgroup servers are so slow. Can anyone recommend
a free or relatively inexpensive good newgroup server to use?
</offtopic>

Thanks,

-Luther
Jul 22 '05 #1
4 1377
Luther Baker wrote:
I'd like to include some very simple logging in my app - removable at
compile time.
Why not run-time?
Avoiding macros .. I want to make sure this nonsense would be
optimized away by the compiler if I don't need it.
Once you get used to logging, you will always need it.
template<bool b>
void
log(const char* msg)
{
if (b == true)
{
std::cout << msg << std::endl;
}
}

...

const bool is_log_on = false;

int
main(int argc, char* argv)
{
...
log<is_log_on>("I am here.");
That doesn't _remove_ it. It just prevents it from outputting.
...
}

Since "is_log_on" evaluates to false at compile time, I'm hoping the
compiler optimizes it away so there is absolutely no affect on
performance.
If performance is your concern, you should simply turn the logging off by
default when running. And turn it on if needed.
Is there a well-known problem with this approach or
extending it into a full blown out logging template class?


A problem I know of is often poor knowledge of available solutions that
leads to spending too much time on re-inventing wheels making them square
or triangular. Look at

http://logging.apache.org/log4cxx/

Victor
Jul 22 '05 #2

"Luther Baker" <lu*********@yahoo.com> wrote in message
news:46**************************@posting.google.c om...
I'd like to include some very simple logging in my app - removable at
compile time.

Avoiding macros .. I want to make sure this nonsense would be
optimized away by the compiler if I don't need it.
template<bool b>
void
log(const char* msg)
{
if (b == true)
{
std::cout << msg << std::endl;
}
}

...

const bool is_log_on = false;

int
main(int argc, char* argv)
{
...
log<is_log_on>("I am here.");
...
}

Since "is_log_on" evaluates to false at compile time, I'm hoping the
compiler optimizes it away so there is absolutely no affect on
performance. Is there a well-known problem with this approach or
extending it into a full blown out logging template class?


If you use a template class then you can specialise it rather than using a
runtime if statement.

template <bool on>
class Logger
{
public:
static void log()
{
cout << "here\n";
}
};

template <>
class Logger<false>
{
public:
void log()
{
}
};

const bool logging = true;

int main()
{
X<logging>::f();
}

More chance of the compiler performing the necessary optimisation if you
give it a little help.

john
Jul 22 '05 #3
lu*********@yahoo.com (Luther Baker) wrote in message
I'd like to include some very simple logging in my app - removable at
compile time.

Avoiding macros .. I want to make sure this nonsense would be
optimized away by the compiler if I don't need it.
template<bool b>
void
log(const char* msg)
{
if (b == true)
{
std::cout << msg << std::endl;
}
}

...

const bool is_log_on = false;

int
main(int argc, char* argv)
{
...
log<is_log_on>("I am here.");
...
}

Since "is_log_on" evaluates to false at compile time, I'm hoping the
compiler optimizes it away so there is absolutely no affect on
performance. Is there a well-known problem with this approach or
extending it into a full blown out logging template class?


The optimization may happen on your compiler -- check the assembly to
be sure. To improve the chances of optimization, make the function
inline. If this doesn't work, provide a specialization of log<false>
which does nothing and dispense with the if statement inside
log<true>. Let us know your findings, what compiler you used,
compiler flags, etc.

Note one normally says,

log << "hello" << x+1;

So even if logging is off, the compiler may still evaluate "hello" and
x+1. If it knows that evaluating x+1 has no side effects and
operator<< does nothing if bool is_log_on = false, then it need not
evaluate x+1. But I don't think most compilers do this level of
optimization.

As a design issue, I'd declare const bool _log_on = true or false at
the top, and so in code I just write log("I am here."); instead of
log<is_log_on>("I am here.");. But that's your call.
Jul 22 '05 #4
John Harrison wrote:
"Luther Baker" <lu*********@yahoo.com> wrote in message
news:46**************************@posting.google.c om... ....

If you use a template class then you can specialise it rather than using a
runtime if statement.

template <bool on>
class Logger
{
public:
static void log()
{
cout << "here\n";
}
};

template <>
class Logger<false>
{
public:
void log()
{
}
};

const bool logging = true;

int main()
{
X<logging>::f();
}

More chance of the compiler performing the necessary optimisation if you
give it a little help.


Oh yes. I overlooked that completely - so much cleaner than a macro.

Thanks again,

-Luther
Jul 22 '05 #5

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

Similar topics

3
by: hall | last post by:
I have a problem with my design of a templatized class. I'm trying to figure out how to load and save the data inside it, but can't. My class looks like this ------------------------------------...
3
by: ma740988 | last post by:
Consider the template member function that generates a random number within a range. #include <cstdlib> #include <ctime> #include <iostream> template<typename T> T Random(T bottom, T top) {
1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
5
by: Joseph Turian | last post by:
In a class templatized by class T, I have the following two lines: hash_map<string, T> foo; hash_map<string, T>::const_iterator p; The first line compiles just fine. The second line gives the...
3
by: Bob Altman | last post by:
Hi all, This is the first time I've tried to create a templatized function, and I can't get past a linker error. I create a new, unmanaged C++ application and added a class named MyClass. In...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
1
by: tavianator | last post by:
If I had a class with a templatized constructor, like this: class foo { public: template<typename T> foo(); };
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
2
by: mrstephengross | last post by:
Hi folks! I'm trying to create a function pointer to a templatized, static, overloaded member function. My class (Mgr) has two functions with the same name: "go". The first takes a T reference (T...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.