Page 1 of 1

[RCF::Logging] How logging to StdOut and File ?

Posted: Sat Nov 23, 2013 7:42 am
by acDev
Logging to StdOut only:

Code: Select all

#define MYLOGFMT  "%C (%D) %X"
#define MYLOGNAME 8
#define RCFLOG3()  UTIL_LOG(MYLOGNAME, RCF::LogLevel_3) << "[MyLog] "

std::string myLogFormat = MYLOGFMT;
RCF::enableLogging(RCF::LogToStdout(), 3, myLogFormat);
RCF::LoggerPtr myLogPtr(new RCF::Logger(MYLOGNAME, 3, RCF::LogToStdout(), myLogFormat));
myLogPtr->activate();

RCFLOG3() << "Server started!";
It's work fine.

Logging to File only:

Code: Select all

#define MYLOGFMT  "%C (%D) %X"
#define MYLOGNAME 8
#define RCFLOG3()  UTIL_LOG(MYLOGNAME, RCF::LogLevel_3) << "[MyLog] "

std::string myLogFormat = MYLOGFMT;
RCF::enableLogging(RCF::LogToFile("srv.log"), 3, myLogFormat);
RCF::LoggerPtr myLogPtr(new RCF::Logger(MYLOGNAME, 3, RCF::LogToFile("srv.log"), myLogFormat));
myLogPtr->activate();

RCFLOG3() << "Server started!";
String "Server started!" not logged!

How logging to StdOut and File together?

Re: [RCF::Logging] How logging to StdOut and File ?

Posted: Wed Nov 27, 2013 2:43 am
by jarl
The reason the string isn't appearing in the file is because it is being buffered... There is an extra parameter in the LogToFile constructor, which you can set to true, to force a flush after each logging call :

Code: Select all

RCF::LoggerPtr log2Ptr(new RCF::Logger(MYLOGNAME, 3, RCF::LogToFile("srv.log", true), myLogFormat));
The following code will get your log statements appearing both in the file and on stdout:

Code: Select all

#define MYLOGFMT  "%C (%D) %X"
#define MYLOGNAME 8
#define RCFLOG3()  UTIL_LOG(MYLOGNAME, RCF::LogLevel_3) << "[MyLog] "

	RCF::LoggerPtr log1Ptr(new RCF::Logger(MYLOGNAME, 3, RCF::LogToStdout(), myLogFormat));
	log1Ptr->activate();

	RCF::LoggerPtr log2Ptr(new RCF::Logger(MYLOGNAME, 3, RCF::LogToFile("srv.log", true), myLogFormat));
	log2Ptr->activate();

	RCFLOG3() << "Server started!";