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("-") )
49 // set unbuffered write
50 std::ofstream * fstr = 0;
51 _outs.reset( (fstr = new std::ofstream( file_r.asString().c_str(), std::ios_base::app )) );
52 fstr->rdbuf()->pubsetbuf(0,0);
56 // not filesystem::chmod, as filesystem:: functions log,
57 // and this FileWriter is not yet in place.
58 ::chmod( file_r.asString().c_str(), mode_r );
63 /////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////
69 { /////////////////////////////////////////////////////////////////
71 ///////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////
74 std::string LogControl::LineFormater::format( const std::string & group_r,
75 logger::LogLevel level_r,
79 const std::string & message_r )
81 static char hostname[1024];
82 static char nohostname[] = "unknown";
83 std::string now( Date::now().form( "%Y-%m-%d %H:%M:%S" ) );
84 return str::form( "%s <%d> %s(%d) [%s] %s(%s):%d %s",
86 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
89 file_r, func_r, line_r,
93 ///////////////////////////////////////////////////////////////////
95 { /////////////////////////////////////////////////////////////////
97 inline void putStream( const std::string & group_r, LogLevel level_r,
98 const char * file_r, const char * func_r, int line_r,
99 const std::string & buffer_r );
101 ///////////////////////////////////////////////////////////////////
103 // CLASS NAME : Loglinebuf
105 class Loglinebuf : public std::streambuf {
109 Loglinebuf( const std::string & group_r, LogLevel level_r )
119 if ( !_buffer.empty() )
124 void tagSet( const char * fil_r, const char * fnc_r, int lne_r )
133 virtual std::streamsize xsputn( const char * s, std::streamsize n )
134 { return writeout( s, n ); }
136 virtual int overflow( int ch = EOF )
146 virtual int writeout( const char* s, std::streamsize n )
151 for ( int i = 0; i < n; ++i, ++c )
154 _buffer += std::string( s, c-s );
155 logger::putStream( _group, _level, _file, _func, _line, _buffer );
156 _buffer = std::string();
162 _buffer += std::string( s, c-s );
177 ///////////////////////////////////////////////////////////////////
179 ///////////////////////////////////////////////////////////////////
181 // CLASS NAME : Loglinestream
183 class Loglinestream {
187 Loglinestream( const std::string & group_r, LogLevel level_r )
188 : _mybuf( group_r, level_r )
189 , _mystream( &_mybuf )
193 { _mystream.flush(); }
197 std::ostream & getStream( const char * fil_r, const char * fnc_r, int lne_r )
199 _mybuf.tagSet( fil_r, fnc_r, lne_r );
205 std::ostream _mystream;
207 ///////////////////////////////////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////
211 // CLASS NAME : LogControlImpl
213 /** LogControl implementation (Singleton).
215 * \note There is a slight difference in using the _lineFormater and _lineWriter!
216 * \li \c _lineFormater must not be NULL (create default LogControl::LineFormater)
217 * \li \c _lineWriter is NULL if no logging is performed, this way we can pass
218 * _no_stream as logstream to the application, and avoid unnecessary formating
219 * of logliles, which would then be discarded when passed to some dummy
222 struct LogControlImpl
226 { return _excessive; }
228 void excessive( bool onOff_r )
229 { _excessive = onOff_r; }
231 /** NULL _lineWriter indicates no loggin. */
232 void setLineWriter( const shared_ptr<LogControl::LineWriter> & writer_r )
233 { _lineWriter = writer_r; }
235 shared_ptr<LogControl::LineWriter> getLineWriter() const
236 { return _lineWriter; }
238 /** Assert \a _lineFormater is not NULL. */
239 void setLineFormater( const shared_ptr<LogControl::LineFormater> & format_r )
242 _lineFormater = format_r;
244 _lineFormater.reset( new LogControl::LineFormater );
247 void logfile( const Pathname & logfile_r, mode_t mode_r = 0640 )
249 if ( logfile_r.empty() )
250 setLineWriter( shared_ptr<LogControl::LineWriter>() );
251 else if ( logfile_r == Pathname( "-" ) )
252 setLineWriter( shared_ptr<LogControl::LineWriter>(new log::StderrLineWriter) );
254 setLineWriter( shared_ptr<LogControl::LineWriter>(new log::FileLineWriter(logfile_r, mode_r)) );
258 std::ostream _no_stream;
261 shared_ptr<LogControl::LineFormater> _lineFormater;
262 shared_ptr<LogControl::LineWriter> _lineWriter;
265 /** Provide the log stream to write (logger interface) */
266 std::ostream & getStream( const std::string & group_r,
274 if ( level_r == E_XXX && !_excessive )
277 if ( !_streamtable[group_r][level_r] )
279 _streamtable[group_r][level_r].reset( new Loglinestream( group_r, level_r ) );
281 return _streamtable[group_r][level_r]->getStream( file_r, func_r, line_r );
284 /** Format and write out a logline from Loglinebuf. */
285 void putStream( const std::string & group_r,
290 const std::string & message_r )
293 _lineWriter->writeOut( _lineFormater->format( group_r, level_r,
294 file_r, func_r, line_r,
299 typedef shared_ptr<Loglinestream> StreamPtr;
300 typedef std::map<LogLevel,StreamPtr> StreamSet;
301 typedef std::map<std::string,StreamSet> StreamTable;
302 /** one streambuffer per group and level */
303 StreamTable _streamtable;
307 * No logging per default, unless enabled via $ZYPP_LOGFILE.
311 , _excessive( getenv("ZYPP_FULLLOG") )
312 , _lineFormater( new LogControl::LineFormater )
314 if ( getenv("ZYPP_LOGFILE") )
315 logfile( getenv("ZYPP_LOGFILE") );
317 if ( getenv("ZYPP_PROFILING") )
319 shared_ptr<LogControl::LineFormater> formater(new ProfilingFormater);
320 setLineFormater(formater);
325 /** The LogControlImpl singleton
326 * \note As most dtors log, it is inportant that the
327 * LogControlImpl instance is the last static variable
328 * destructed. At least destucted after all statics
329 * which log from their dtor.
331 static LogControlImpl & instance();
333 ///////////////////////////////////////////////////////////////////
335 // 'THE' LogControlImpl singleton
336 inline LogControlImpl & LogControlImpl::instance()
338 static LogControlImpl _instance;
342 ///////////////////////////////////////////////////////////////////
344 /** \relates LogControlImpl Stream output */
345 inline std::ostream & operator<<( std::ostream & str, const LogControlImpl & obj )
347 return str << "LogControlImpl";
350 ///////////////////////////////////////////////////////////////////
352 // Access from logger::
354 ///////////////////////////////////////////////////////////////////
356 std::ostream & getStream( const char * group_r,
362 return LogControlImpl::instance().getStream( group_r,
369 /** That's what Loglinebuf calls. */
370 inline void putStream( const std::string & group_r, LogLevel level_r,
371 const char * file_r, const char * func_r, int line_r,
372 const std::string & buffer_r )
374 LogControlImpl::instance().putStream( group_r, level_r,
375 file_r, func_r, line_r,
380 { return LogControlImpl::instance().isExcessive(); }
382 /////////////////////////////////////////////////////////////////
383 } // namespace logger
384 ///////////////////////////////////////////////////////////////////
386 ///////////////////////////////////////////////////////////////////
388 // CLASS NAME : LogControl
389 // Forward to LogControlImpl singleton.
391 ///////////////////////////////////////////////////////////////////
393 using logger::LogControlImpl;
395 void LogControl::logfile( const Pathname & logfile_r )
396 { LogControlImpl::instance().logfile( logfile_r ); }
398 void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r )
399 { LogControlImpl::instance().logfile( logfile_r, mode_r ); }
401 shared_ptr<LogControl::LineWriter> LogControl::getLineWriter() const
402 { return LogControlImpl::instance().getLineWriter(); }
404 void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r )
405 { LogControlImpl::instance().setLineWriter( writer_r ); }
407 void LogControl::setLineFormater( const shared_ptr<LineFormater> & formater_r )
408 { LogControlImpl::instance().setLineFormater( formater_r ); }
410 void LogControl::logNothing()
411 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>() ); }
413 void LogControl::logToStdErr()
414 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) ); }
416 ///////////////////////////////////////////////////////////////////
418 // LogControl::TmpExcessive
420 ///////////////////////////////////////////////////////////////////
421 LogControl::TmpExcessive::TmpExcessive()
422 { LogControlImpl::instance().excessive( true ); }
423 LogControl::TmpExcessive::~TmpExcessive()
424 { LogControlImpl::instance().excessive( false ); }
426 /******************************************************************
428 ** FUNCTION NAME : operator<<
429 ** FUNCTION TYPE : std::ostream &
431 std::ostream & operator<<( std::ostream & str, const LogControl & obj )
433 return str << LogControlImpl::instance();
436 /////////////////////////////////////////////////////////////////
438 ///////////////////////////////////////////////////////////////////
439 /////////////////////////////////////////////////////////////////
441 ///////////////////////////////////////////////////////////////////