Hi,
I have C (printf) style debug functions
static void debugPrint(Silver::Strategy& s, unsigned level, const char
*format, ...)
{
if (s.verbosityLevel() >= level ) {
va_list args ;
va_start(args, format);
vfprintf(stdout, format, args);
}
}
debugPrint(s,1,"blabla\n");
Now I want the same facility, but using ostreams. I want to be able to
write
debugPrint(s,1) << "blabla" << std::endl;
static ostream& debugPrint(Silver::Strategy& s, unsigned level)
{
if (s.verbosityLevel() >= level ) return std::cout;
// Whats next?
return ??
How can I create a black-hole / void ostream object?
Any help I can get is greatly appreciated.
Cheers,
-L 5 4667
lars wrote:
Hi,
I have C (printf) style debug functions
static void debugPrint(Silver::Strategy& s, unsigned level, const char
*format, ...)
{
if (s.verbosityLevel() >= level ) {
va_list args ;
va_start(args, format);
vfprintf(stdout, format, args);
}
}
debugPrint(s,1,"blabla\n");
Now I want the same facility, but using ostreams. I want to be able to
write
debugPrint(s,1) << "blabla" << std::endl;
static ostream& debugPrint(Silver::Strategy& s, unsigned level)
{
if (s.verbosityLevel() >= level ) return std::cout;
// Whats next?
return ??
How can I create a black-hole / void ostream object?
Simply create one for yourself:
include <iostream>
#include <ostream>
#include <iomanip>
#include "stdarg.h"
// I couldn't figure out how to call the constructor of ostream
// without this using this namespace.
using namespace std;
static void debugPrint(unsigned level, const char *format, ...)
{
va_list args ;
va_start (args, format);
vfprintf (stdout, format, args);
}
class CBlackHoleStream : public ostream
{
public:
CBlackHoleStream ()
: ostream (NULL, false, false) {}
};
static CBlackHoleStream g_BlackHoleStream;
static ostream& debugPrint(unsigned level)
{
if (level 0)
return std::cout;
// Whats next?
return g_BlackHoleStream;
}
int main ()
{
debugPrint (0) << "Test\n" << "Test2\n";
debugPrint (1) << "blabla\n";
return 0;
}
Regards,
Stuart
Stuart Redmann wrote:
>
// I couldn't figure out how to call the constructor of ostream
// without this using this namespace.
using namespace std;
static void debugPrint(unsigned level, const char *format, ...)
{
va_list args ;
va_start (args, format);
vfprintf (stdout, format, args);
}
class CBlackHoleStream : public ostream
class CBlackHoleStream : public std::ostream
{
public:
CBlackHoleStream ()
: ostream (NULL, false, false) {}
: std::ostream (NULL, false, false) {}
};
static CBlackHoleStream g_BlackHoleStream;
static ostream& debugPrint(unsigned level)
static std::ostream& debugPrint(unsigned level)
{
if (level 0)
return std::cout;
// Whats next?
return g_BlackHoleStream;
}
int main ()
{
debugPrint (0) << "Test\n" << "Test2\n";
debugPrint (1) << "blabla\n";
return 0;
}
Tried these things?
On 27 Oct 2006 03:05:41 -0700, "lars" <la**@jasper-da.comwrote:
>I have C (printf) style debug functions
static void debugPrint(Silver::Strategy& s, unsigned level, const char *format, ...) {
if (s.verbosityLevel() >= level ) {
va_list args ;
va_start(args, format);
vfprintf(stdout, format, args);
} }
debugPrint(s,1,"blabla\n");
This is not optimal because your arguments are always evaluated, even
when nothing is printed.
>Now I want the same facility, but using ostreams. I want to be able to write
debugPrint(s,1) << "blabla" << std::endl;
static ostream& debugPrint(Silver::Strategy& s, unsigned level) {
if (s.verbosityLevel() >= level ) return std::cout;
// Whats next?
return ??
How can I create a black-hole / void ostream object?
You can but you also suffer from unnecessary evaluations (more a
problem in C++ than in C). C++ log libraries with ostream interface
usually have ugly looking macros to avoid that. The following sketch
of a solution uses templates instead of macros:
#ifndef LOGGER_H
#define LOGGER_H
#include <ostream>
template <typename T1>
bool log (std::ostream& os, const T1& t1) {
os << t1 << std::endl;
return true;
}
template <typename T1, typename T2>
bool log (std::ostream& os, const T1& t1, const T2& t2) {
os << t1 << " " << t2 << std::endl;
return true;
}
template <typename T1, typename T2, typename T3>
bool log (std::ostream& os, const T1& t1, const T2& t2, const T3& t3)
{
os << t1 << " " << t2 << " " << t3 << std::endl;
return true;
}
// and so on ...
enum LogLevel {
eTrace = 1, eError
};
class LogStrategy {
public:
LogStrategy(): level (0) {}
bool trace() { return level <= eTrace; }
bool error() { return level <= eError; }
void setLevel (LogLevel ll) { level = ll; }
private:
int level;
LogStrategy (const LogStrategy&);
LogStrategy& operator= (const LogStrategy&);
};
#endif // LOGGER_H
A test file:
#include <iostream>
#include "Logger.h"
using namespace std;
int main (int argc, char* argv[])
{
LogStrategy s;
s.setLevel (eTrace);
// s.setLevel (eError);
s.trace() && log (cout, "Hello, world!");
s.trace() && log (cout, "result", 3.14159265);
s.error() && log (cout, 123.456, "something wrong", 789);
}
Best wishes,
Roland Pibinger
: std::ostream (NULL, false, false) {}
error: no matching function for call to `std::basic_ostream<char,
std::char_traits<char::basic_ostream(const void**, NULL, bool, bool)
What is this constructor you are using? Is it standard? How do I get it
in scope?
Cheers,
-L
VJ wrote:
Stuart Redmann wrote:
// I couldn't figure out how to call the constructor of ostream
// without this using this namespace.
using namespace std;
static void debugPrint(unsigned level, const char *format, ...)
{
va_list args ;
va_start (args, format);
vfprintf (stdout, format, args);
}
class CBlackHoleStream : public ostream
class CBlackHoleStream : public std::ostream
{
public:
CBlackHoleStream ()
: ostream (NULL, false, false) {}
: std::ostream (NULL, false, false) {}
};
static CBlackHoleStream g_BlackHoleStream;
static ostream& debugPrint(unsigned level)
static std::ostream& debugPrint(unsigned level)
{
if (level 0)
return std::cout;
// Whats next?
return g_BlackHoleStream;
}
int main ()
{
debugPrint (0) << "Test\n" << "Test2\n";
debugPrint (1) << "blabla\n";
return 0;
}
Tried these things?
lars wrote:
>>: std::ostream (NULL, false, false) {}
error: no matching function for call to `std::basic_ostream<char,
std::char_traits<char::basic_ostream(const void**, NULL, bool, bool)
What is this constructor you are using? Is it standard? How do I get it
in scope?
Cheers,
-L
VJ wrote:
>>Stuart Redmann wrote:
>>>// I couldn't figure out how to call the constructor of ostream // without this using this namespace. using namespace std;
static void debugPrint(unsigned level, const char *format, ...) { va_list args ; va_start (args, format); vfprintf (stdout, format, args); }
class CBlackHoleStream : public ostream
class CBlackHoleStream : public std::ostream
>>>{ public: CBlackHoleStream () : ostream (NULL, false, false) {}
: std::ostream (NULL, false, false) {}
>>>};
static CBlackHoleStream g_BlackHoleStream;
static ostream& debugPrint(unsigned level)
static std::ostream& debugPrint(unsigned level)
>>>{ if (level 0) return std::cout;
// Whats next? return g_BlackHoleStream; }
int main () { debugPrint (0) << "Test\n" << "Test2\n"; debugPrint (1) << "blabla\n";
return 0; }
Tried these things?
ok, i wanted to do it fast, but here is copy/paste of the code that
works on my computer (linux / g++)
#include <iostream>
#include <iomanip>
#include "stdarg.h"
static void debugPrint(unsigned level, const char *format, ...)
{
va_list args ;
va_start (args, format);
vfprintf (stdout, format, args);
}
class CBlackHoleStream : public std::ostream
{
public:
CBlackHoleStream ()
: std::ostream (NULL) {}
};
static CBlackHoleStream g_BlackHoleStream;
static std::ostream& debugPrint(unsigned level)
{
if (level 0)
return std::cout;
// Whats next?
return g_BlackHoleStream;
}
int main ()
{
debugPrint (0) << "Test\n" << "Test2\n"<<std::endl;
debugPrint (1) << "blabla\n"<<std::endl;
return 0;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Victor Irzak |
last post by:
Hello,
I have an ABC.
it supports:
ostream & operator <<
I also have a derived class that supports this operator.
How can I call operator << of the base class for derived object??? Is it...
|
by: keit6736 |
last post by:
Hi, I'm using the Borland compiler and I've created two templated
classes in which I've overloaded the ostream << operator. However, when
I try and use the operator on objects of either class I...
|
by: Matt Garman |
last post by:
I'd like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter...
|
by: pembed2003 |
last post by:
Hi all,
Given something like:
std::ofstream out_file("path");
how do I extract the file descriptor from out_file? Is it possible?
What I want is to extract the file descriptor and then pass...
|
by: waitan |
last post by:
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <iomanip>
using namespace std;
|
by: bonham28c |
last post by:
hi,
i m trying to make a custom bool class.
anything is the same as bool except when it is printing, i want it to
print + if true and - if false.
but it seems like i cant subclass bool like...
|
by: silversurfer2025 |
last post by:
Hello,
I am currently trying to derive a class from ostream (which is giving
output to my GUI), such that I can give my methods either std::cout or
my own outputstream-class to be used as output...
|
by: syang8 |
last post by:
Any one can specify the problem of the following code? The compiling
error is on the friend function. If the base class is not inherited
from ostream, or I just remove the friend function from the...
|
by: rakesh.usenet |
last post by:
For a particular application of mine - I need a simulation of byte
array output stream.
* write data onto a stream
* getback the contiguous content as an array later for network
transport.
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
| |