scootermaster
Platinum Member
So I'm using a tweaked version of the simple logging library provided here:
http://www.drdobbs.com/cpp/201804215;jsessionid=IGOKPTDNZB0JBQE1GHPCKH4ATMY32JVN?pgno=3
On the example code, they instantiate a logging object like this, using a macro given below (in main):
Other relevant parts of the code are the class definition:
and the macro that's above from main
And finally, this (which I'm not even sure what it does, other than defined a weirdly unnamed class)
The "get" function just builds a stream object (that is then printed somewhere) and the Output2File class has functions that send it to the screen (or, after much tinkering, to an output file. Another question I had was how to paramterize that, but I ended up just making 2 separate classes and creating two objects).
So my question is this: So far this works wonderfully in main. But how can I get this to work globally across a number of different files? I guess it's not super essential (I could just instantiate a new logging thingee for each file/class I'm using, seeing as the file descriptor to the screen (stderr) will be the same, and if I open the same file each time, it [should] just interleave the logging statements, but that seems ugly. So is there a way to access a single logging object from, say, test.h or test.cc (within class objects in those files)?
Is there a way of doing this? Thanks!
http://www.drdobbs.com/cpp/201804215;jsessionid=IGOKPTDNZB0JBQE1GHPCKH4ATMY32JVN?pgno=3
On the example code, they instantiate a logging object like this, using a macro given below (in main):
Code:
FILELog::ReportingLevel() = FILELog::FromString(argv[1] ? argv[1] : "DEBUG1");
Other relevant parts of the code are the class definition:
Code:
template <typename T>
class Log
{
public:
Log();
virtual ~Log();
std::ostringstream& Get(TLogLevel level = logINFO); // Sets up actual logging string, defaults level to logINFO
public:
static TLogLevel& ReportingLevel();
static std::string ToString(TLogLevel level);
static TLogLevel FromString(const std::string& level);
protected:
std::ostringstream os;
private:
Log(const Log&);
Log& operator =(const Log&);
};
and the macro that's above from main
Code:
#define FILE_LOG(level) \
if (level > FILELOG_MAX_LEVEL) ;\
else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
else FILELog().Get(level)
And finally, this (which I'm not even sure what it does, other than defined a weirdly unnamed class)
Code:
class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {};
The "get" function just builds a stream object (that is then printed somewhere) and the Output2File class has functions that send it to the screen (or, after much tinkering, to an output file. Another question I had was how to paramterize that, but I ended up just making 2 separate classes and creating two objects).
So my question is this: So far this works wonderfully in main. But how can I get this to work globally across a number of different files? I guess it's not super essential (I could just instantiate a new logging thingee for each file/class I'm using, seeing as the file descriptor to the screen (stderr) will be the same, and if I open the same file each time, it [should] just interleave the logging statements, but that seems ugly. So is there a way to access a single logging object from, say, test.h or test.cc (within class objects in those files)?
Is there a way of doing this? Thanks!