1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/LogControl.cc
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/LogControl.h"
18 #include "zypp/base/ProfilingFormater.h"
19 #include "zypp/base/String.h"
20 #include "zypp/Date.h"
21 #include "zypp/PathInfo.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 StdoutLineWriter::StdoutLineWriter()
34 : StreamLineWriter( std::cout )
37 StderrLineWriter::StderrLineWriter()
38 : StreamLineWriter( std::cerr )
41 FileLineWriter::FileLineWriter( const Pathname & file_r, mode_t mode_r )
43 if ( file_r == Pathname("-") )
51 // not filesystem::assert_file as filesystem:: functions log,
52 // and this FileWriter is not yet in place.
53 int fd = ::open( file_r.c_str(), O_CREAT|O_EXCL, mode_r );
57 // set unbuffered write
58 std::ofstream * fstr = 0;
59 _outs.reset( (fstr = new std::ofstream( file_r.asString().c_str(), std::ios_base::app )) );
60 fstr->rdbuf()->pubsetbuf(0,0);
65 /////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////
69 ///////////////////////////////////////////////////////////////////
71 { /////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////
75 ///////////////////////////////////////////////////////////////////
76 std::string LogControl::LineFormater::format( const std::string & group_r,
77 logger::LogLevel level_r,
81 const std::string & message_r )
83 static char hostname[1024];
84 static char nohostname[] = "unknown";
85 std::string now( Date::now().form( "%Y-%m-%d %H:%M:%S" ) );
86 return str::form( "%s <%d> %s(%d) [%s] %s(%s):%d %s",
88 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
91 file_r, func_r, line_r,
95 ///////////////////////////////////////////////////////////////////
97 { /////////////////////////////////////////////////////////////////
99 inline void putStream( const std::string & group_r, LogLevel level_r,
100 const char * file_r, const char * func_r, int line_r,
101 const std::string & buffer_r );
103 ///////////////////////////////////////////////////////////////////
105 // CLASS NAME : Loglinebuf
107 class Loglinebuf : public std::streambuf {
111 Loglinebuf( const std::string & group_r, LogLevel level_r )
121 if ( !_buffer.empty() )
126 void tagSet( const char * fil_r, const char * fnc_r, int lne_r )
135 virtual std::streamsize xsputn( const char * s, std::streamsize n )
136 { return writeout( s, n ); }
138 virtual int overflow( int ch = EOF )
148 virtual int writeout( const char* s, std::streamsize n )
150 //logger::putStream( _group, _level, _file, _func, _line, _buffer );
155 for ( int i = 0; i < n; ++i, ++c )
158 _buffer += std::string( s, c-s );
159 logger::putStream( _group, _level, _file, _func, _line, _buffer );
160 _buffer = std::string();
166 _buffer += std::string( s, c-s );
181 ///////////////////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////////////////
185 // CLASS NAME : Loglinestream
187 class Loglinestream {
191 Loglinestream( const std::string & group_r, LogLevel level_r )
192 : _mybuf( group_r, level_r )
193 , _mystream( &_mybuf )
197 { _mystream.flush(); }
201 std::ostream & getStream( const char * fil_r, const char * fnc_r, int lne_r )
203 _mybuf.tagSet( fil_r, fnc_r, lne_r );
209 std::ostream _mystream;
211 ///////////////////////////////////////////////////////////////////
213 ///////////////////////////////////////////////////////////////////
215 // CLASS NAME : LogControlImpl
217 /** LogControl implementation (Singleton).
219 * \note There is a slight difference in using the _lineFormater and _lineWriter!
220 * \li \c _lineFormater must not be NULL (create default LogControl::LineFormater)
221 * \li \c _lineWriter is NULL if no logging is performed, this way we can pass
222 * _no_stream as logstream to the application, and avoid unnecessary formating
223 * of logliles, which would then be discarded when passed to some dummy
226 struct LogControlImpl
230 { return _excessive; }
232 void excessive( bool onOff_r )
233 { _excessive = onOff_r; }
235 /** NULL _lineWriter indicates no loggin. */
236 void setLineWriter( const shared_ptr<LogControl::LineWriter> & writer_r )
237 { _lineWriter = writer_r; }
239 shared_ptr<LogControl::LineWriter> getLineWriter() const
240 { return _lineWriter; }
242 /** Assert \a _lineFormater is not NULL. */
243 void setLineFormater( const shared_ptr<LogControl::LineFormater> & format_r )
246 _lineFormater = format_r;
248 _lineFormater.reset( new LogControl::LineFormater );
251 void logfile( const Pathname & logfile_r, mode_t mode_r = 0640 )
253 if ( logfile_r.empty() )
254 setLineWriter( shared_ptr<LogControl::LineWriter>() );
255 else if ( logfile_r == Pathname( "-" ) )
256 setLineWriter( shared_ptr<LogControl::LineWriter>(new log::StderrLineWriter) );
258 setLineWriter( shared_ptr<LogControl::LineWriter>(new log::FileLineWriter(logfile_r, mode_r)) );
262 std::ostream _no_stream;
265 shared_ptr<LogControl::LineFormater> _lineFormater;
266 shared_ptr<LogControl::LineWriter> _lineWriter;
269 /** Provide the log stream to write (logger interface) */
270 std::ostream & getStream( const std::string & group_r,
278 if ( level_r == E_XXX && !_excessive )
281 if ( !_streamtable[group_r][level_r] )
283 _streamtable[group_r][level_r].reset( new Loglinestream( group_r, level_r ) );
285 return _streamtable[group_r][level_r]->getStream( file_r, func_r, line_r );
288 /** Format and write out a logline from Loglinebuf. */
289 void putStream( const std::string & group_r,
294 const std::string & message_r )
297 _lineWriter->writeOut( _lineFormater->format( group_r, level_r,
298 file_r, func_r, line_r,
303 typedef shared_ptr<Loglinestream> StreamPtr;
304 typedef std::map<LogLevel,StreamPtr> StreamSet;
305 typedef std::map<std::string,StreamSet> StreamTable;
306 /** one streambuffer per group and level */
307 StreamTable _streamtable;
311 * No logging per default, unless enabled via $ZYPP_LOGFILE.
315 , _excessive( getenv("ZYPP_FULLLOG") )
316 , _lineFormater( new LogControl::LineFormater )
318 if ( getenv("ZYPP_LOGFILE") )
319 logfile( getenv("ZYPP_LOGFILE") );
321 if ( getenv("ZYPP_PROFILING") )
323 shared_ptr<LogControl::LineFormater> formater(new ProfilingFormater);
324 setLineFormater(formater);
334 /** The LogControlImpl singleton
335 * \note As most dtors log, it is inportant that the
336 * LogControlImpl instance is the last static variable
337 * destructed. At least destucted after all statics
338 * which log from their dtor.
340 static LogControlImpl & instance();
342 ///////////////////////////////////////////////////////////////////
344 // 'THE' LogControlImpl singleton
345 inline LogControlImpl & LogControlImpl::instance()
347 static LogControlImpl _instance;
351 ///////////////////////////////////////////////////////////////////
353 /** \relates LogControlImpl Stream output */
354 inline std::ostream & operator<<( std::ostream & str, const LogControlImpl & obj )
356 return str << "LogControlImpl";
359 ///////////////////////////////////////////////////////////////////
361 // Access from logger::
363 ///////////////////////////////////////////////////////////////////
365 std::ostream & getStream( const char * group_r,
371 return LogControlImpl::instance().getStream( group_r,
378 /** That's what Loglinebuf calls. */
379 inline void putStream( const std::string & group_r, LogLevel level_r,
380 const char * file_r, const char * func_r, int line_r,
381 const std::string & buffer_r )
383 LogControlImpl::instance().putStream( group_r, level_r,
384 file_r, func_r, line_r,
389 { return LogControlImpl::instance().isExcessive(); }
391 /////////////////////////////////////////////////////////////////
392 } // namespace logger
393 ///////////////////////////////////////////////////////////////////
395 ///////////////////////////////////////////////////////////////////
397 // CLASS NAME : LogControl
398 // Forward to LogControlImpl singleton.
400 ///////////////////////////////////////////////////////////////////
402 using logger::LogControlImpl;
404 void LogControl::logfile( const Pathname & logfile_r )
405 { LogControlImpl::instance().logfile( logfile_r ); }
407 void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r )
408 { LogControlImpl::instance().logfile( logfile_r, mode_r ); }
410 shared_ptr<LogControl::LineWriter> LogControl::getLineWriter() const
411 { return LogControlImpl::instance().getLineWriter(); }
413 void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r )
414 { LogControlImpl::instance().setLineWriter( writer_r ); }
416 void LogControl::setLineFormater( const shared_ptr<LineFormater> & formater_r )
417 { LogControlImpl::instance().setLineFormater( formater_r ); }
419 void LogControl::logNothing()
420 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>() ); }
422 void LogControl::logToStdErr()
423 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) ); }
425 ///////////////////////////////////////////////////////////////////
427 // LogControl::TmpExcessive
429 ///////////////////////////////////////////////////////////////////
430 LogControl::TmpExcessive::TmpExcessive()
431 { LogControlImpl::instance().excessive( true ); }
432 LogControl::TmpExcessive::~TmpExcessive()
433 { LogControlImpl::instance().excessive( false ); }
435 /******************************************************************
437 ** FUNCTION NAME : operator<<
438 ** FUNCTION TYPE : std::ostream &
440 std::ostream & operator<<( std::ostream & str, const LogControl & obj )
442 return str << LogControlImpl::instance();
445 /////////////////////////////////////////////////////////////////
447 ///////////////////////////////////////////////////////////////////
448 /////////////////////////////////////////////////////////////////
450 ///////////////////////////////////////////////////////////////////