32 void osdlog(
const std::string & msg_r,
unsigned level_r )
43 static const char * ansi[] = {
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;
80 if ( file_r == Pathname(
"-") )
90 int fd = ::open( file_r.c_str(), O_CREAT|O_EXCL, mode_r );
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);
118 const std::string & message_r )
120 static char hostname[1024];
121 static char nohostname[] =
"unknown";
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,
137 const char * file_r,
const char * func_r,
int line_r,
138 const std::string & buffer_r );
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 ); }
185 virtual int writeout(
const char* s, std::streamsize n )
192 for (
int i = 0; i < n; ++i, ++c )
195 _buffer += std::string( s, c-s );
203 _buffer += std::string( s, c-s );
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 );
267 {
return _excessive; }
270 { _excessive = onOff_r; }
274 { _lineWriter = writer_r; }
277 {
return _lineWriter; }
283 _lineFormater = format_r;
288 void logfile(
const Pathname & logfile_r, mode_t mode_r = 0640 )
290 if ( logfile_r.empty() )
292 else if ( logfile_r == Pathname(
"-" ) )
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 std::ostream & ret( _streamtable[group_r][level_r]->
getStream( file_r, func_r, line_r ) );
326 ret <<
"---<RESET LOGSTREAM FROM FAILED STATE]" << endl;
337 const std::string & message_r )
340 _lineWriter->writeOut( _lineFormater->format( group_r, level_r,
341 file_r, func_r, line_r,
358 , _excessive( getenv(
"ZYPP_FULLLOG") )
359 , _lineFormater( new
LogControl::LineFormater )
361 if ( getenv(
"ZYPP_LOGFILE") )
362 logfile( getenv(
"ZYPP_LOGFILE") );
364 if ( getenv(
"ZYPP_PROFILING") )
399 return str <<
"LogControlImpl";
414 return LogControlImpl::instance().getStream( group_r,
423 const char * file_r,
const char * func_r,
int line_r,
424 const std::string & buffer_r )
426 LogControlImpl::instance().putStream( group_r, level_r,
427 file_r, func_r, line_r,
432 {
return LogControlImpl::instance().isExcessive(); }
445 using logger::LogControlImpl;
448 { LogControlImpl::instance().logfile( logfile_r ); }
451 { LogControlImpl::instance().logfile( logfile_r, mode_r ); }
454 {
return LogControlImpl::instance().getLineWriter(); }
457 { LogControlImpl::instance().setLineWriter( writer_r ); }
460 { LogControlImpl::instance().setLineFormater( formater_r ); }
463 { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>() ); }
474 { LogControlImpl::instance().excessive(
true ); }
476 { LogControlImpl::instance().excessive(
false ); }
485 return str << LogControlImpl::instance();
std::map< LogLevel, StreamPtr > StreamSet
LogLevel
Definition of log levels.
std::ostream & getStream(const std::string &group_r, LogLevel level_r, const char *file_r, const char *func_r, const int line_r)
Provide the log stream to write (logger interface)
Base class for ostream based LineWriter.
void tagSet(const char *fil_r, const char *fnc_r, int lne_r)
shared_ptr< LogControl::LineWriter > getLineWriter() const
Loglinestream(const std::string &group_r, LogLevel level_r)
void excessive(bool onOff_r)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
LogControlImpl()
Singleton ctor.
void setLineWriter(const shared_ptr< LogControl::LineWriter > &writer_r)
NULL _lineWriter indicates no loggin.
void logfile(const Pathname &logfile_r, mode_t mode_r=0640)
void logfile(const Pathname &logfile_r)
Set path for the logfile.
void osdlog(const std::string &msg_r, unsigned level_r)
static LogControl instance()
Singleton access.
std::ostream & operator<<(std::ostream &str, const LogControlImpl &obj)
void setLineWriter(const shared_ptr< LineWriter > &writer_r)
Assign a LineWriter.
void logToStdErr()
Log to std::err.
const std::string & _buffer
friend std::ostream & operator<<(std::ostream &str, const LogControl &obj)
virtual int overflow(int ch=EOF)
void setLineFormater(const shared_ptr< LogControl::LineFormater > &format_r)
Assert _lineFormater is not NULL.
shared_ptr< LogControl::LineFormater > _lineFormater
std::ostream & getStream(const char *fil_r, const char *fnc_r, int lne_r)
std::map< std::string, StreamSet > StreamTable
void logNothing()
Turn off logging.
Maintain logfile related options.
virtual int writeout(const char *s, std::streamsize n)
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)
That's what Loglinebuf calls.
shared_ptr< LogControl::LineWriter > _lineWriter
static Date now()
Return the current time.
void putStream(const std::string &group_r, LogLevel level_r, const char *file_r, const char *func_r, int line_r, const std::string &message_r)
Format and write out a logline from Loglinebuf.
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Loglinebuf(const std::string &group_r, LogLevel level_r)
void setLineFormater(const shared_ptr< LineFormater > &formater_r)
Assign a LineFormater.
shared_ptr< Loglinestream > StreamPtr
shared_ptr< LineWriter > getLineWriter() const
Get the current LineWriter.
std::ostream & getStream(const char *group_r, LogLevel level_r, const char *file_r, const char *func_r, const int line_r)
Return a log stream to write on.
LogControl implementation (Singleton).
FileLineWriter(const Pathname &file_r, mode_t mode_r=0)
StreamTable _streamtable
one streambuffer per group and level