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 { /////////////////////////////////////////////////////////////////
32 void osdlog( const std::string & msg_r, unsigned level_r )
34 // Fg::Black: 30 Bg: 40 Attr::Normal: 22;27
35 // Fg::Red: 31 ... Attr::Bright: 1
36 // Fg::Green: 32 Attr::Reverse: 7
43 static const char * ansi[] = {
46 "\033[33;1;40m", // 2 y
48 "\033[31;1;40m", // 4 r
51 static const unsigned n = sizeof(ansi)/sizeof(const char *);
54 case 'w': level_r = 0; break;
55 case 'c': level_r = 1; break;
56 case 'y': level_r = 2; break;
57 case 'g': level_r = 3; break;
58 case 'r': level_r = 4; break;
59 case 'm': level_r = 5; break;
61 std::cerr << ansi[level_r%n] << "OSD[" << msg_r << "]\033[0m" << std::endl;
66 ///////////////////////////////////////////////////////////////////
68 { /////////////////////////////////////////////////////////////////
70 StdoutLineWriter::StdoutLineWriter()
71 : StreamLineWriter( std::cout )
74 StderrLineWriter::StderrLineWriter()
75 : StreamLineWriter( std::cerr )
78 FileLineWriter::FileLineWriter( const Pathname & file_r, mode_t mode_r )
80 if ( file_r == Pathname("-") )
88 // not filesystem::assert_file as filesystem:: functions log,
89 // and this FileWriter is not yet in place.
90 int fd = ::open( file_r.c_str(), O_CREAT|O_EXCL, mode_r );
94 // set unbuffered write
95 std::ofstream * fstr = 0;
96 _outs.reset( (fstr = new std::ofstream( file_r.asString().c_str(), std::ios_base::app )) );
97 fstr->rdbuf()->pubsetbuf(0,0);
102 /////////////////////////////////////////////////////////////////
104 ///////////////////////////////////////////////////////////////////
106 ///////////////////////////////////////////////////////////////////
108 { /////////////////////////////////////////////////////////////////
110 ///////////////////////////////////////////////////////////////////
112 ///////////////////////////////////////////////////////////////////
113 std::string LogControl::LineFormater::format( const std::string & group_r,
114 logger::LogLevel level_r,
118 const std::string & message_r )
120 static char hostname[1024];
121 static char nohostname[] = "unknown";
122 std::string now( Date::now().form( "%Y-%m-%d %H:%M:%S" ) );
123 return str::form( "%s <%d> %s(%d) [%s] %s(%s):%d %s",
124 now.c_str(), level_r,
125 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
128 file_r, func_r, line_r,
132 ///////////////////////////////////////////////////////////////////
134 { /////////////////////////////////////////////////////////////////
136 inline void putStream( const std::string & group_r, LogLevel level_r,
137 const char * file_r, const char * func_r, int line_r,
138 const std::string & buffer_r );
140 ///////////////////////////////////////////////////////////////////
142 // CLASS NAME : Loglinebuf
144 class Loglinebuf : public std::streambuf {
148 Loglinebuf( const std::string & group_r, LogLevel level_r )
158 if ( !_buffer.empty() )
163 void tagSet( const char * fil_r, const char * fnc_r, int lne_r )
172 virtual std::streamsize xsputn( const char * s, std::streamsize n )
173 { return writeout( s, n ); }
175 virtual int overflow( int ch = EOF )
185 virtual int writeout( const char* s, std::streamsize n )
187 //logger::putStream( _group, _level, _file, _func, _line, _buffer );
192 for ( int i = 0; i < n; ++i, ++c )
195 _buffer += std::string( s, c-s );
196 logger::putStream( _group, _level, _file, _func, _line, _buffer );
197 _buffer = std::string();
203 _buffer += std::string( s, c-s );
218 ///////////////////////////////////////////////////////////////////
220 ///////////////////////////////////////////////////////////////////
222 // CLASS NAME : Loglinestream
224 class Loglinestream {
228 Loglinestream( const std::string & group_r, LogLevel level_r )
229 : _mybuf( group_r, level_r )
230 , _mystream( &_mybuf )
234 { _mystream.flush(); }
238 std::ostream & getStream( const char * fil_r, const char * fnc_r, int lne_r )
240 _mybuf.tagSet( fil_r, fnc_r, lne_r );
246 std::ostream _mystream;
248 ///////////////////////////////////////////////////////////////////
250 ///////////////////////////////////////////////////////////////////
252 // CLASS NAME : LogControlImpl
254 /** LogControl implementation (Singleton).
256 * \note There is a slight difference in using the _lineFormater and _lineWriter!
257 * \li \c _lineFormater must not be NULL (create default LogControl::LineFormater)
258 * \li \c _lineWriter is NULL if no logging is performed, this way we can pass
259 * _no_stream as logstream to the application, and avoid unnecessary formating
260 * of logliles, which would then be discarded when passed to some dummy
263 struct LogControlImpl
267 { return _excessive; }
269 void excessive( bool onOff_r )
270 { _excessive = onOff_r; }
272 /** NULL _lineWriter indicates no loggin. */
273 void setLineWriter( const shared_ptr<LogControl::LineWriter> & writer_r )
274 { _lineWriter = writer_r; }
276 shared_ptr<LogControl::LineWriter> getLineWriter() const
277 { return _lineWriter; }
279 /** Assert \a _lineFormater is not NULL. */
280 void setLineFormater( const shared_ptr<LogControl::LineFormater> & format_r )
283 _lineFormater = format_r;
285 _lineFormater.reset( new LogControl::LineFormater );
288 void logfile( const Pathname & logfile_r, mode_t mode_r = 0640 )
290 if ( logfile_r.empty() )
291 setLineWriter( shared_ptr<LogControl::LineWriter>() );
292 else if ( logfile_r == Pathname( "-" ) )
293 setLineWriter( shared_ptr<LogControl::LineWriter>(new log::StderrLineWriter) );
295 setLineWriter( shared_ptr<LogControl::LineWriter>(new log::FileLineWriter(logfile_r, mode_r)) );
299 std::ostream _no_stream;
302 shared_ptr<LogControl::LineFormater> _lineFormater;
303 shared_ptr<LogControl::LineWriter> _lineWriter;
306 /** Provide the log stream to write (logger interface) */
307 std::ostream & getStream( const std::string & group_r,
315 if ( level_r == E_XXX && !_excessive )
318 if ( !_streamtable[group_r][level_r] )
320 _streamtable[group_r][level_r].reset( new Loglinestream( group_r, level_r ) );
322 return _streamtable[group_r][level_r]->getStream( file_r, func_r, line_r );
325 /** Format and write out a logline from Loglinebuf. */
326 void putStream( const std::string & group_r,
331 const std::string & message_r )
334 _lineWriter->writeOut( _lineFormater->format( group_r, level_r,
335 file_r, func_r, line_r,
340 typedef shared_ptr<Loglinestream> StreamPtr;
341 typedef std::map<LogLevel,StreamPtr> StreamSet;
342 typedef std::map<std::string,StreamSet> StreamTable;
343 /** one streambuffer per group and level */
344 StreamTable _streamtable;
348 * No logging per default, unless enabled via $ZYPP_LOGFILE.
352 , _excessive( getenv("ZYPP_FULLLOG") )
353 , _lineFormater( new LogControl::LineFormater )
355 if ( getenv("ZYPP_LOGFILE") )
356 logfile( getenv("ZYPP_LOGFILE") );
358 if ( getenv("ZYPP_PROFILING") )
360 shared_ptr<LogControl::LineFormater> formater(new ProfilingFormater);
361 setLineFormater(formater);
371 /** The LogControlImpl singleton
372 * \note As most dtors log, it is inportant that the
373 * LogControlImpl instance is the last static variable
374 * destructed. At least destucted after all statics
375 * which log from their dtor.
377 static LogControlImpl & instance();
379 ///////////////////////////////////////////////////////////////////
381 // 'THE' LogControlImpl singleton
382 inline LogControlImpl & LogControlImpl::instance()
384 static LogControlImpl _instance;
388 ///////////////////////////////////////////////////////////////////
390 /** \relates LogControlImpl Stream output */
391 inline std::ostream & operator<<( std::ostream & str, const LogControlImpl & obj )
393 return str << "LogControlImpl";
396 ///////////////////////////////////////////////////////////////////
398 // Access from logger::
400 ///////////////////////////////////////////////////////////////////
402 std::ostream & getStream( const char * group_r,
408 return LogControlImpl::instance().getStream( group_r,
415 /** That's what Loglinebuf calls. */
416 inline void putStream( const std::string & group_r, LogLevel level_r,
417 const char * file_r, const char * func_r, int line_r,
418 const std::string & buffer_r )
420 LogControlImpl::instance().putStream( group_r, level_r,
421 file_r, func_r, line_r,
426 { return LogControlImpl::instance().isExcessive(); }
428 /////////////////////////////////////////////////////////////////
429 } // namespace logger
430 ///////////////////////////////////////////////////////////////////
432 ///////////////////////////////////////////////////////////////////
434 // CLASS NAME : LogControl
435 // Forward to LogControlImpl singleton.
437 ///////////////////////////////////////////////////////////////////
439 using logger::LogControlImpl;
441 void LogControl::logfile( const Pathname & logfile_r )
442 { LogControlImpl::instance().logfile( logfile_r ); }
444 void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r )
445 { LogControlImpl::instance().logfile( logfile_r, mode_r ); }
447 shared_ptr<LogControl::LineWriter> LogControl::getLineWriter() const
448 { return LogControlImpl::instance().getLineWriter(); }
450 void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r )
451 { LogControlImpl::instance().setLineWriter( writer_r ); }
453 void LogControl::setLineFormater( const shared_ptr<LineFormater> & formater_r )
454 { LogControlImpl::instance().setLineFormater( formater_r ); }
456 void LogControl::logNothing()
457 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>() ); }
459 void LogControl::logToStdErr()
460 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) ); }
462 ///////////////////////////////////////////////////////////////////
464 // LogControl::TmpExcessive
466 ///////////////////////////////////////////////////////////////////
467 LogControl::TmpExcessive::TmpExcessive()
468 { LogControlImpl::instance().excessive( true ); }
469 LogControl::TmpExcessive::~TmpExcessive()
470 { LogControlImpl::instance().excessive( false ); }
472 /******************************************************************
474 ** FUNCTION NAME : operator<<
475 ** FUNCTION TYPE : std::ostream &
477 std::ostream & operator<<( std::ostream & str, const LogControl & obj )
479 return str << LogControlImpl::instance();
482 /////////////////////////////////////////////////////////////////
484 ///////////////////////////////////////////////////////////////////
485 /////////////////////////////////////////////////////////////////
487 ///////////////////////////////////////////////////////////////////