#include "zypp/base/Logger.h"
#include "zypp/base/LogControl.h"
+#include "zypp/base/ProfilingFormater.h"
#include "zypp/base/String.h"
#include "zypp/Date.h"
+#include "zypp/PathInfo.h"
using std::endl;
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ namespace log
+ { /////////////////////////////////////////////////////////////////
+
+ StdoutLineWriter::StdoutLineWriter()
+ : StreamLineWriter( std::cout )
+ {}
+
+ StderrLineWriter::StderrLineWriter()
+ : StreamLineWriter( std::cerr )
+ {}
+
+ FileLineWriter::FileLineWriter( const Pathname & file_r, mode_t mode_r )
+ {
+ if ( file_r == Pathname("-") )
+ {
+ _str = &std::cerr;
+ }
+ else
+ {
+ if ( mode_r )
+ {
+ // not filesystem::assert_file as filesystem:: functions log,
+ // and this FileWriter is not yet in place.
+ int fd = ::open( file_r.c_str(), O_CREAT|O_EXCL, mode_r );
+ if ( fd != -1 )
+ ::close( fd );
+ }
+ // set unbuffered write
+ std::ofstream * fstr = 0;
+ _outs.reset( (fstr = new std::ofstream( file_r.asString().c_str(), std::ios_base::app )) );
+ fstr->rdbuf()->pubsetbuf(0,0);
+ _str = &(*fstr);
+ }
+ }
+
+ /////////////////////////////////////////////////////////////////
+ } // namespace log
+ ///////////////////////////////////////////////////////////////////
+
///////////////////////////////////////////////////////////////////
namespace base
{ /////////////////////////////////////////////////////////////////
namespace logger
{ /////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////
- // LineWriter
- ///////////////////////////////////////////////////////////////////
- struct StdErrWriter : public LogControl::LineWriter
- {
- virtual void writeOut( const std::string & formated_r )
- {
- std::cerr << formated_r << endl;
- }
- };
- ///////////////////////////////////////////////////////////////////
- struct FileWriter : public LogControl::LineWriter
- {
- FileWriter( const Pathname & logfile_r )
- : _logfile( logfile_r )
- {}
- Pathname _logfile;
-
- virtual void writeOut( const std::string & formated_r )
- {
- std::ofstream outs( _logfile.asString().c_str(), std::ios_base::app );
- outs << formated_r << endl;
- }
- };
- ///////////////////////////////////////////////////////////////////
-
inline void putStream( const std::string & group_r, LogLevel level_r,
const char * file_r, const char * func_r, int line_r,
const std::string & buffer_r );
/** */
virtual int writeout( const char* s, std::streamsize n )
{
+ //logger::putStream( _group, _level, _file, _func, _line, _buffer );
+ //return n;
if ( s && n )
{
const char * c = s;
struct LogControlImpl
{
public:
+ bool isExcessive()
+ { return _excessive; }
+
void excessive( bool onOff_r )
{ _excessive = onOff_r; }
void setLineWriter( const shared_ptr<LogControl::LineWriter> & writer_r )
{ _lineWriter = writer_r; }
+ shared_ptr<LogControl::LineWriter> getLineWriter() const
+ { return _lineWriter; }
+
/** Assert \a _lineFormater is not NULL. */
void setLineFormater( const shared_ptr<LogControl::LineFormater> & format_r )
{
_lineFormater.reset( new LogControl::LineFormater );
}
- void logfile( const Pathname & logfile_r )
+ void logfile( const Pathname & logfile_r, mode_t mode_r = 0640 )
{
if ( logfile_r.empty() )
setLineWriter( shared_ptr<LogControl::LineWriter>() );
else if ( logfile_r == Pathname( "-" ) )
- setLineWriter( shared_ptr<LogControl::LineWriter>(new StdErrWriter) );
+ setLineWriter( shared_ptr<LogControl::LineWriter>(new log::StderrLineWriter) );
else
- setLineWriter( shared_ptr<LogControl::LineWriter>(new FileWriter(logfile_r)) );
+ setLineWriter( shared_ptr<LogControl::LineWriter>(new log::FileLineWriter(logfile_r, mode_r)) );
}
private:
{
if ( getenv("ZYPP_LOGFILE") )
logfile( getenv("ZYPP_LOGFILE") );
+
+ if ( getenv("ZYPP_PROFILING") )
+ {
+ shared_ptr<LogControl::LineFormater> formater(new ProfilingFormater);
+ setLineFormater(formater);
+ }
+ }
+
+ ~LogControlImpl()
+ {
+ _lineWriter.reset();
}
public:
* destructed. At least destucted after all statics
* which log from their dtor.
*/
- static LogControlImpl instance;
+ static LogControlImpl & instance();
};
///////////////////////////////////////////////////////////////////
// 'THE' LogControlImpl singleton
- LogControlImpl LogControlImpl::instance;
+ inline LogControlImpl & LogControlImpl::instance()
+ {
+ static LogControlImpl _instance;
+ return _instance;
+ }
///////////////////////////////////////////////////////////////////
- /** \relates LogControl::Impl Stream output */
+ /** \relates LogControlImpl Stream output */
inline std::ostream & operator<<( std::ostream & str, const LogControlImpl & obj )
{
return str << "LogControlImpl";
//
///////////////////////////////////////////////////////////////////
- /** That's what logger:: calls. */
std::ostream & getStream( const char * group_r,
LogLevel level_r,
const char * file_r,
const char * func_r,
const int line_r )
{
- return LogControlImpl::instance.getStream( group_r,
+ return LogControlImpl::instance().getStream( group_r,
level_r,
file_r,
func_r,
const char * file_r, const char * func_r, int line_r,
const std::string & buffer_r )
{
- LogControlImpl::instance.putStream( group_r, level_r,
+ LogControlImpl::instance().putStream( group_r, level_r,
file_r, func_r, line_r,
buffer_r );
}
+ bool isExcessive()
+ { return LogControlImpl::instance().isExcessive(); }
+
/////////////////////////////////////////////////////////////////
} // namespace logger
///////////////////////////////////////////////////////////////////
using logger::LogControlImpl;
void LogControl::logfile( const Pathname & logfile_r )
- { LogControlImpl::instance.logfile( logfile_r ); }
+ { LogControlImpl::instance().logfile( logfile_r ); }
+
+ void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r )
+ { LogControlImpl::instance().logfile( logfile_r, mode_r ); }
+
+ shared_ptr<LogControl::LineWriter> LogControl::getLineWriter() const
+ { return LogControlImpl::instance().getLineWriter(); }
void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r )
- { LogControlImpl::instance.setLineWriter( writer_r ); }
+ { LogControlImpl::instance().setLineWriter( writer_r ); }
+
+ void LogControl::setLineFormater( const shared_ptr<LineFormater> & formater_r )
+ { LogControlImpl::instance().setLineFormater( formater_r ); }
void LogControl::logNothing()
- { LogControlImpl::instance.setLineWriter( shared_ptr<LineWriter>() ); }
+ { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>() ); }
void LogControl::logToStdErr()
- { LogControlImpl::instance.setLineWriter( shared_ptr<LineWriter>( new logger::StdErrWriter ) ); }
+ { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) ); }
///////////////////////////////////////////////////////////////////
//
//
///////////////////////////////////////////////////////////////////
LogControl::TmpExcessive::TmpExcessive()
- { LogControlImpl::instance.excessive( true ); }
+ { LogControlImpl::instance().excessive( true ); }
LogControl::TmpExcessive::~TmpExcessive()
- { LogControlImpl::instance.excessive( false ); }
+ { LogControlImpl::instance().excessive( false ); }
/******************************************************************
**
*/
std::ostream & operator<<( std::ostream & str, const LogControl & obj )
{
- return str << LogControlImpl::instance;
+ return str << LogControlImpl::instance();
}
/////////////////////////////////////////////////////////////////