libzypp  17.28.5
LogControl.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <fstream>
14 #include <string>
15 #include <mutex>
16 #include <map>
17 
18 #include <zypp-core/base/Logger.h>
19 #include <zypp-core/base/LogControl.h>
20 #include <zypp-core/base/ProfilingFormater.h>
21 #include <zypp-core/base/String.h>
22 #include <zypp-core/Date.h>
23 #include <zypp-core/TriBool.h>
24 #include <zypp-core/AutoDispose.h>
25 
26 #include <zypp-core/zyppng/io/Socket>
27 #include <zypp-core/zyppng/io/SockAddr>
28 #include <zypp-core/zyppng/base/EventLoop>
29 #include <zypp-core/zyppng/base/EventDispatcher>
30 #include <zypp-core/zyppng/base/Timer>
31 #include <zypp-core/zyppng/base/private/linuxhelpers_p.h>
32 #include <zypp-core/zyppng/thread/Wakeup>
33 #include <zypp-core/zyppng/base/private/threaddata_p.h>
34 #include <zypp-core/zyppng/base/SocketNotifier>
35 
36 #include <thread>
37 #include <variant>
38 #include <atomic>
39 #include <csignal>
40 
41 extern "C"
42 {
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <dirent.h>
48 }
49 
50 using std::endl;
51 
52 std::once_flag flagReadEnvAutomatically;
53 
54 namespace zypp
55 {
56  constexpr std::string_view ZYPP_MAIN_THREAD_NAME( "Zypp-main" );
57 
58  template<class> inline constexpr bool always_false_v = false;
59 
64  class SpinLock {
65  public:
66  void lock () {
67  // acquire lock
68  while ( _atomicLock.test_and_set())
69  // reschedule the current thread while we wait, maybe when its our next turn the lock is free again
70  std::this_thread::yield();
71  }
72 
73  void unlock() {
74  _atomicLock.clear();
75  }
76 
77  private:
78  // we use a lock free atomic flag here so this lock can be safely obtained in a signal handler as well
79  std::atomic_flag _atomicLock = ATOMIC_FLAG_INIT;
80  };
81 
82  class LogThread
83  {
84 
85  public:
86 
88  stop();
89  }
90 
91  static LogThread &instance () {
92  static LogThread t;
93  return t;
94  }
95 
96  void setLineWriter ( boost::shared_ptr<log::LineWriter> writer ) {
97  std::lock_guard lk( _lineWriterLock );
98  _lineWriter = writer;
99  }
100 
101  boost::shared_ptr<log::LineWriter> getLineWriter () {
102  std::lock_guard lk( _lineWriterLock );
103  auto lw = _lineWriter;
104  return lw;
105  }
106 
107  void stop () {
108  _stopSignal.notify();
109  if ( _thread.get_id() != std::this_thread::get_id() )
110  _thread.join();
111  }
112 
113  std::thread::id threadId () {
114  return _thread.get_id();
115  }
116 
117  static std::string sockPath () {
118  static std::string path = zypp::str::Format("zypp-logsocket-%1%") % getpid();
119  return path;
120  }
121 
122  private:
123 
125  {
126  // Name the thread that started the logger assuming it's main thread.
127  zyppng::ThreadData::current().setName(ZYPP_MAIN_THREAD_NAME);
128  _thread = std::thread( [this] () {
129  workerMain();
130  });
131  }
132 
133  void workerMain () {
134 
135  // force the kernel to pick another thread to handle those signals
136  zyppng::blockSignalsForCurrentThread( { SIGTERM, SIGINT, SIGPIPE, } );
137 
138  zyppng::ThreadData::current().setName("Zypp-Log");
139 
140  auto ev = zyppng::EventLoop::create();
141  auto server = zyppng::Socket::create( AF_UNIX, SOCK_STREAM, 0 );
142  auto stopNotifyWatch = _stopSignal.makeNotifier( );
143 
144  std::vector<zyppng::Socket::Ptr> clients;
145 
146  // bind to a abstract unix domain socket address, which means we do not need to care about cleaning it up
147  server->bind( std::make_shared<zyppng::UnixSockAddr>( sockPath(), true ) );
148  server->listen();
149 
150  // wait for incoming connections from other threads
151  server->connectFunc( &zyppng::Socket::sigIncomingConnection, [&](){
152 
153  auto cl = server->accept();
154  if ( !cl ) return;
155  clients.push_back( cl );
156 
157  // wait until data is available, we operate line by line so we only
158  // log a string once we encounter \n
159  cl->connectFunc( &zyppng::Socket::sigReadyRead, [ this, sock = cl.get() ](){
160  auto writer = getLineWriter();
161  if ( !writer ) return;
162  while ( sock->canReadLine() ) {
163  auto br = sock->readLine();
164  writer->writeOut( std::string( br.data(), br.size() - 1 ) );
165  }
166  }, *cl);
167 
168  // once a client disconnects we remove it from the std::vector so that the socket is not leaked
169  cl->connectFunc( &zyppng::Socket::sigDisconnected, [&clients, sock = std::weak_ptr(cl)](){
170  auto lock = sock.lock();
171  if ( !lock )
172  return;
173 
174  auto idx = std::find_if( clients.begin(), clients.end(), [lock]( const auto &s ){ return lock.get() == s.get(); } );
175  clients.erase( idx );
176  });
177 
178  });
179 
180  stopNotifyWatch->connectFunc( &zyppng::SocketNotifier::sigActivated, [&ev]( const auto &, auto ) {
181  ev->quit();
182  });
183 
184  ev->run();
185 
186  // make sure we have written everything
187  auto writer = getLineWriter();
188  if ( writer ) {
189  for ( auto &sock : clients ){
190  while ( sock->canReadLine() ) {
191  auto br = sock->readLine();
192  writer->writeOut( std::string( br.data(), br.size() - 1 ) );
193  }
194  }
195  }
196  }
197 
198  private:
199  std::thread _thread;
200  zyppng::Wakeup _stopSignal;
201 
202  // since the public API uses boost::shared_ptr we can not use the atomic
203  // functionalities provided in std.
204  // this lock type can be used safely in signals
206  // boost shared_ptr has a lock free implementation of reference counting so it can be used from signal handlers as well
207  boost::shared_ptr<log::LineWriter> _lineWriter{ nullptr };
208  };
209 
210  class LogClient
211  {
212  public:
214  // make sure the thread is running
216  }
217 
219  ::close( _sockFD );
220  }
221 
227  if ( _sockFD >= 0 )
228  return true;
229 
230  _sockFD = ::socket( AF_UNIX, SOCK_STREAM, 0 );
231  if ( _sockFD == -1 )
232  return false;
233 
234  zyppng::UnixSockAddr addr( LogThread::sockPath(), true );
235  return zyppng::trySocketConnection( _sockFD, addr, 100 );
236  }
237 
241  void pushMessage ( std::string &&msg ) {
242  if ( inPushMessage ) {
243  return;
244  }
245 
246  // make sure we do not end up in a busy loop
247  zypp::AutoDispose<bool *> res( &inPushMessage, [](auto val){
248  *val = false;
249  });
250  inPushMessage = true;
251 
252  // if we are in the same thread as the Log worker we can directly push our messages out, no need to use the socket
253  if ( std::this_thread::get_id() == LogThread::instance().threadId() ) {
254  auto writer = LogThread::instance().getLineWriter();
255  writer->writeOut( msg );
256  return;
257  }
258 
259  if(!ensureConnection())
260  return;
261 
262  if ( msg.back() != '\n' )
263  msg.push_back('\n');
264 
265  size_t written = 0;
266  while ( written < msg.size() ) {
267  const auto res = zyppng::eintrSafeCall( ::send, _sockFD, msg.data() + written, msg.size() - written, MSG_NOSIGNAL );
268  if ( res == -1 ) {
269  //assume broken socket
270  ::close( _sockFD );
271  _sockFD = -1;
272  return;
273  }
274  written += res;
275  }
276  }
277 
278  private:
279  int _sockFD = -1;
280  bool inPushMessage = false;
281  };
282 
283 #ifndef ZYPP_NDEBUG
284  namespace debug
285  {
286  // Fg::Black: 30 Bg: 40 Attr::Normal: 22;27
287  // Fg::Red: 31 ... Attr::Bright: 1
288  // Fg::Green: 32 Attr::Reverse: 7
289  // Fg::Yellow: 33
290  // Fg::Blue: 34
291  // Fg::Magenta: 35
292  // Fg::Cyan: 36
293  // Fg::White: 37
294  // Fg::Default: 39
295  static constexpr std::string_view OO { "\033[0m" };
296  static constexpr std::string_view WH { "\033[37;40m" };
297  static constexpr std::string_view CY { "\033[36;40m" };
298  static constexpr std::string_view YE { "\033[33;1;40m" };
299  static constexpr std::string_view GR { "\033[32;40m" };
300  static constexpr std::string_view RE { "\033[31;1;40m" };
301  static constexpr std::string_view MA { "\033[35;40m" };
302 
303  unsigned TraceLeave::_depth = 1;
304 
305  std::string tracestr( char tag_r, unsigned depth_r, const char * file_r, const char * fnc_r, int line_r )
306  {
307  static str::Format fmt { "*** %s %s(%s):%d" };
308  fmt % std::string(depth_r,tag_r) % file_r % fnc_r % line_r;
309  return fmt;
310  }
311 
312  TraceLeave::TraceLeave( const char * file_r, const char * fnc_r, int line_r )
313  : _file( std::move(file_r) )
314  , _fnc( std::move(fnc_r) )
315  , _line( line_r )
316  {
317  const std::string & m { tracestr( '>',_depth++, _file,_fnc,_line ) };
318  USR << m << endl;
319  Osd(L_USR("TRACE"),1) << m << endl;
320  }
321 
323  {
324  const std::string & m { tracestr( '<',--_depth, _file,_fnc,_line ) };
325  USR << m << endl;
326  Osd(L_USR("TRACE"),1) << m << endl;
327  }
328 
329  Osd::Osd( std::ostream & str, int i )
330  : _strout { std::cerr }
331  , _strlog { str }
332  { _strout << (i?WH:YE); }
333 
335  { _strout << OO; }
336 
337  Osd & Osd::operator<<( std::ostream& (*iomanip)( std::ostream& ) )
338  {
339  _strout << iomanip;
340  _strlog << iomanip;
341  return *this;
342  }
343 }
344 #endif // ZYPP_NDEBUG
345 
347  namespace log
348  {
349 
351  : StreamLineWriter( std::cout )
352  {}
353 
355  : StreamLineWriter( std::cerr )
356  {}
357 
358  FileLineWriter::FileLineWriter( const Pathname & file_r, mode_t mode_r )
359  {
360  if ( file_r == Pathname("-") )
361  {
362  _str = &std::cerr;
363  }
364  else
365  {
366  if ( mode_r )
367  {
368  // not filesystem::assert_file as filesystem:: functions log,
369  // and this FileWriter is not yet in place.
370  int fd = ::open( file_r.c_str(), O_CREAT|O_EXCL, mode_r );
371  if ( fd != -1 )
372  ::close( fd );
373  }
374  // set unbuffered write
375  std::ofstream * fstr = 0;
376  _outs.reset( (fstr = new std::ofstream( file_r.asString().c_str(), std::ios_base::app )) );
377  fstr->rdbuf()->pubsetbuf(0,0);
378  _str = &(*fstr);
379  }
380  }
381 
383  } // namespace log
385 
387  namespace base
388  {
389  namespace logger
391  {
392 
393  inline void putStream( const std::string & group_r, LogLevel level_r,
394  const char * file_r, const char * func_r, int line_r,
395  const std::string & buffer_r );
396 
398  //
399  // CLASS NAME : Loglinebuf
400  //
401  class Loglinebuf : public std::streambuf {
402 
403  public:
405  Loglinebuf( const std::string & group_r, LogLevel level_r )
406  : _group( group_r )
407  , _level( level_r )
408  , _file( "" )
409  , _func( "" )
410  , _line( -1 )
411  {}
414  {
415  if ( !_buffer.empty() )
416  writeout( "\n", 1 );
417  }
418 
420  void tagSet( const char * fil_r, const char * fnc_r, int lne_r )
421  {
422  _file = fil_r;
423  _func = fnc_r;
424  _line = lne_r;
425  }
426 
427  private:
429  virtual std::streamsize xsputn( const char * s, std::streamsize n )
430  { return writeout( s, n ); }
432  virtual int overflow( int ch = EOF )
433  {
434  if ( ch != EOF )
435  {
436  char tmp = ch;
437  writeout( &tmp, 1 );
438  }
439  return 0;
440  }
442  virtual int writeout( const char* s, std::streamsize n )
443  {
444  //logger::putStream( _group, _level, _file, _func, _line, _buffer );
445  //return n;
446  if ( s && n )
447  {
448  const char * c = s;
449  for ( int i = 0; i < n; ++i, ++c )
450  {
451  if ( *c == '\n' ) {
452  _buffer += std::string( s, c-s );
454  _buffer = std::string();
455  s = c+1;
456  }
457  }
458  if ( s < c )
459  {
460  _buffer += std::string( s, c-s );
461  }
462  }
463  return n;
464  }
465 
466  private:
467  std::string _group;
469  const char * _file;
470  const char * _func;
471  int _line;
472  std::string _buffer;
473  };
474 
476 
478  //
479  // CLASS NAME : Loglinestream
480  //
482 
483  public:
485  Loglinestream( const std::string & group_r, LogLevel level_r )
486  : _mybuf( group_r, level_r )
487  , _mystream( &_mybuf )
488  {}
491  { _mystream.flush(); }
492 
493  public:
495  std::ostream & getStream( const char * fil_r, const char * fnc_r, int lne_r )
496  {
497  _mybuf.tagSet( fil_r, fnc_r, lne_r );
498  return _mystream;
499  }
500 
501  private:
503  std::ostream _mystream;
504  };
506 
507  struct LogControlImpl;
508 
509  /*
510  * Ugly hack to prevent the use of LogControlImpl when libzypp is shutting down.
511  * Due to the c++ std thread_local static instances are cleaned up before the first global static
512  * destructor is called. So all classes that use logging after that point in time would crash the
513  * application because its accessing a variable that has already been destroyed.
514  */
516  // We are using a POD flag that does not have a destructor,
517  // to flag if the thread_local destructors were already executed.
518  // Since TLS data is stored in a segment that is available until the thread ceases to exist it should still be readable
519  // after thread_local c++ destructors were already executed. Or so I hope.
520  static thread_local int logControlValid = 0;
521  return logControlValid;
522  }
523 
525  //
526  // CLASS NAME : LogControlImpl
527  //
538  {
539  public:
540  bool isExcessive() const
541  { return _excessive; }
542 
543  void excessive( bool onOff_r )
544  { _excessive = onOff_r; }
545 
546 
548  bool hideThreadName() const
549  {
550  if ( indeterminate(_hideThreadName) )
551  _hideThreadName = ( zyppng::ThreadData::current().name() == ZYPP_MAIN_THREAD_NAME );
552  return bool(_hideThreadName);
553  }
555  void hideThreadName( bool onOff_r )
556  { _hideThreadName = onOff_r; }
559  {
560  auto impl = LogControlImpl::instance();
561  return impl ? impl->hideThreadName() : false;
562  }
564  static void instanceHideThreadName( bool onOff_r )
565  {
566  auto impl = LogControlImpl::instance();
567  if ( impl ) impl->hideThreadName( onOff_r );
568  }
569 
570 
572  void setLineWriter( const shared_ptr<LogControl::LineWriter> & writer_r )
573  { LogThread::instance().setLineWriter( writer_r ); }
574 
575  shared_ptr<LogControl::LineWriter> getLineWriter() const
576  { return LogThread::instance().getLineWriter(); }
577 
579  void setLineFormater( const shared_ptr<LogControl::LineFormater> & format_r )
580  {
581  if ( format_r )
582  _lineFormater = format_r;
583  else
585  }
586 
587  void logfile( const Pathname & logfile_r, mode_t mode_r = 0640 )
588  {
589  if ( logfile_r.empty() )
590  setLineWriter( shared_ptr<LogControl::LineWriter>() );
591  else if ( logfile_r == Pathname( "-" ) )
592  setLineWriter( shared_ptr<LogControl::LineWriter>(new log::StderrLineWriter) );
593  else
594  setLineWriter( shared_ptr<LogControl::LineWriter>(new log::FileLineWriter(logfile_r, mode_r)) );
595  }
596 
597  private:
599  std::ostream _no_stream;
601  mutable TriBool _hideThreadName = indeterminate;
602 
603  shared_ptr<LogControl::LineFormater> _lineFormater;
604 
605  public:
607  std::ostream & getStream( const std::string & group_r,
608  LogLevel level_r,
609  const char * file_r,
610  const char * func_r,
611  const int line_r )
612  {
613  if ( ! getLineWriter() )
614  return _no_stream;
615  if ( level_r == E_XXX && !_excessive )
616  return _no_stream;
617 
618  if ( !_streamtable[group_r][level_r] )
619  {
620  _streamtable[group_r][level_r].reset( new Loglinestream( group_r, level_r ) );
621  }
622  std::ostream & ret( _streamtable[group_r][level_r]->getStream( file_r, func_r, line_r ) );
623  if ( !ret )
624  {
625  ret.clear();
626  ret << "---<RESET LOGSTREAM FROM FAILED STATE]" << endl;
627  }
628  return ret;
629  }
630 
632  void putStream( const std::string & group_r,
633  LogLevel level_r,
634  const char * file_r,
635  const char * func_r,
636  int line_r,
637  const std::string & message_r )
638  {
639  _logClient.pushMessage( _lineFormater->format( group_r, level_r,
640  file_r, func_r, line_r,
641  message_r ) );
642  }
643 
644  private:
645  typedef shared_ptr<Loglinestream> StreamPtr;
646  typedef std::map<LogLevel,StreamPtr> StreamSet;
647  typedef std::map<std::string,StreamSet> StreamTable;
650  zyppng::Socket::Ptr _sock;
651 
652  private:
653 
654  void readEnvVars () {
655  if ( getenv("ZYPP_LOGFILE") )
656  logfile( getenv("ZYPP_LOGFILE") );
657 
658  if ( getenv("ZYPP_PROFILING") )
659  {
660  shared_ptr<LogControl::LineFormater> formater(new ProfilingFormater);
661  setLineFormater(formater);
662  }
663  }
668  : _no_stream( NULL )
669  , _excessive( getenv("ZYPP_FULLLOG") )
670  , _lineFormater( new LogControl::LineFormater )
671  {
672  logControlValidFlag() = 1;
673  std::call_once( flagReadEnvAutomatically, &LogControlImpl::readEnvVars, this);
674  }
675 
676  public:
677 
679  {
680  logControlValidFlag() = 0;
681  }
682 
689  static LogControlImpl *instance();
690  };
692 
693  // 'THE' LogControlImpl singleton
695  {
696  thread_local static LogControlImpl _instance;
697  if ( logControlValidFlag() > 0 )
698  return &_instance;
699  return nullptr;
700  }
701 
703 
705  inline std::ostream & operator<<( std::ostream & str, const LogControlImpl & )
706  {
707  return str << "LogControlImpl";
708  }
709 
711  //
712  // Access from logger::
713  //
715 
716  std::ostream & getStream( const char * group_r,
717  LogLevel level_r,
718  const char * file_r,
719  const char * func_r,
720  const int line_r )
721  {
722  static std::ostream nstream(NULL);
723  auto control = LogControlImpl::instance();
724  if ( !control || !group_r || strlen(group_r ) == 0 ) {
725  return nstream;
726  }
727 
728 
729 
730  return control->getStream( group_r,
731  level_r,
732  file_r,
733  func_r,
734  line_r );
735  }
736 
738  inline void putStream( const std::string & group_r, LogLevel level_r,
739  const char * file_r, const char * func_r, int line_r,
740  const std::string & buffer_r )
741  {
742  auto control = LogControlImpl::instance();
743  if ( !control )
744  return;
745 
746  control->putStream( group_r, level_r,
747  file_r, func_r, line_r,
748  buffer_r );
749  }
750 
751  bool isExcessive()
752  {
753  auto impl = LogControlImpl::instance();
754  if ( !impl )
755  return false;
756  return impl->isExcessive();
757  }
758 
760  } // namespace logger
762 
763  using logger::LogControlImpl;
764 
766  // LineFormater
768  std::string LogControl::LineFormater::format( const std::string & group_r,
769  logger::LogLevel level_r,
770  const char * file_r,
771  const char * func_r,
772  int line_r,
773  const std::string & message_r )
774  {
775  static char hostname[1024];
776  static char nohostname[] = "unknown";
777  std::string now( Date::now().form( "%Y-%m-%d %H:%M:%S" ) );
778  std::string ret;
780  ret = str::form( "%s <%d> %s(%d) [%s] %s(%s):%d %s",
781  now.c_str(), level_r,
782  ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
783  getpid(),
784  group_r.c_str(),
785  file_r, func_r, line_r,
786  message_r.c_str() );
787  else
788  ret = str::form( "%s <%d> %s(%d) [%s] %s(%s):%d {T:%s} %s",
789  now.c_str(), level_r,
790  ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
791  getpid(),
792  group_r.c_str(),
793  file_r, func_r, line_r,
794  zyppng::ThreadData::current().name().c_str(),
795  message_r.c_str() );
796  return ret;
797  }
798 
800  //
801  // CLASS NAME : LogControl
802  // Forward to LogControlImpl singleton.
803  //
805 
806 
807  void LogControl::logfile( const Pathname & logfile_r )
808  {
809  auto impl = LogControlImpl::instance();
810  if ( !impl )
811  return;
812 
813  impl->logfile( logfile_r );
814  }
815 
816  void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r )
817  {
818  auto impl = LogControlImpl::instance();
819  if ( !impl )
820  return;
821 
822  impl->logfile( logfile_r, mode_r );
823  }
824 
825  shared_ptr<LogControl::LineWriter> LogControl::getLineWriter() const
826  {
827  auto impl = LogControlImpl::instance();
828  if ( !impl )
829  return nullptr;
830 
831  return impl->getLineWriter();
832  }
833 
834  void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r )
835  {
836  auto impl = LogControlImpl::instance();
837  if ( !impl )
838  return;
839  impl->setLineWriter( writer_r );
840  }
841 
842  void LogControl::setLineFormater( const shared_ptr<LineFormater> & formater_r )
843  {
844  auto impl = LogControlImpl::instance();
845  if ( !impl )
846  return;
847  impl->setLineFormater( formater_r );
848  }
849 
851  {
852  auto impl = LogControlImpl::instance();
853  if ( !impl )
854  return;
855  impl->setLineWriter( shared_ptr<LineWriter>() );
856  }
857 
859  {
860  auto impl = LogControlImpl::instance();
861  if ( !impl )
862  return;
863  impl->setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) );
864  }
865 
867  {
869  }
870 
872  //
873  // LogControl::TmpExcessive
874  //
877  {
878  auto impl = LogControlImpl::instance();
879  if ( !impl )
880  return;
881  impl->excessive( true );
882  }
884  {
885  auto impl = LogControlImpl::instance();
886  if ( !impl )
887  return;
888  impl->excessive( false );
889  }
890 
891  /******************************************************************
892  **
893  ** FUNCTION NAME : operator<<
894  ** FUNCTION TYPE : std::ostream &
895  */
896  std::ostream & operator<<( std::ostream & str, const LogControl & )
897  {
898  auto impl = LogControlImpl::instance();
899  if ( !impl )
900  return str;
901  return str << *impl;
902  }
903 
905  } // namespace base
908 } // namespace zypp
std::map< LogLevel, StreamPtr > StreamSet
Definition: LogControl.cc:646
std::atomic_flag _atomicLock
Definition: LogControl.cc:79
Osd & operator<<(Tp &&val)
Definition: Logger.h:48
LogLevel
Definition of log levels.
Definition: Logger.h:145
constexpr bool always_false_v
Definition: LogControl.cc:58
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)
Definition: LogControl.cc:607
static constexpr std::string_view MA
Definition: LogControl.cc:301
Base class for ostream based LineWriter.
Definition: LogControl.h:44
std::ostream & _strlog
Definition: Logger.h:59
void tagSet(const char *fil_r, const char *fnc_r, int lne_r)
Definition: LogControl.cc:420
Loglinestream(const std::string &group_r, LogLevel level_r)
Definition: LogControl.cc:485
static constexpr std::string_view WH
Definition: LogControl.cc:296
const char * c_str() const
String representation.
Definition: Pathname.h:110
String related utilities and Regular expression matching.
std::ostream & _strout
Definition: Logger.h:58
Definition: Arch.h:347
LineWriter to file.
Definition: LogControl.h:72
If you want to format loglines by yourself, derive from this, and overload format.
Definition: LogControl.h:114
static void instanceHideThreadName(bool onOff_r)
Definition: LogControl.cc:564
Convenient building of std::string with boost::format.
Definition: String.h:252
LineWriter to stderr.
Definition: LogControl.h:63
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
Osd(std::ostream &, int=0)
Definition: LogControl.cc:329
void setLineWriter(const shared_ptr< LogControl::LineWriter > &writer_r)
NULL _lineWriter indicates no loggin.
Definition: LogControl.cc:572
static std::string sockPath()
Definition: LogControl.cc:117
void logfile(const Pathname &logfile_r, mode_t mode_r=0640)
Definition: LogControl.cc:587
void logfile(const Pathname &logfile_r)
Set path for the logfile.
Definition: LogControl.cc:807
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: String.h:30
void setLineWriter(const shared_ptr< LineWriter > &writer_r)
Assign a LineWriter.
Definition: LogControl.cc:834
void logToStdErr()
Log to std::err.
Definition: LogControl.cc:858
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
const char * _fnc
Definition: Logger.h:36
TraceLeave(const TraceLeave &)=delete
std::thread::id threadId()
Definition: LogControl.cc:113
shared_ptr< LogControl::LineWriter > getLineWriter() const
Definition: LogControl.cc:575
zyppng::Wakeup _stopSignal
Definition: LogControl.cc:200
const std::string & asString() const
String representation.
Definition: Pathname.h:91
static constexpr std::string_view GR
Definition: LogControl.cc:299
friend std::ostream & operator<<(std::ostream &str, const LogControl &obj)
Definition: LogControl.cc:896
static constexpr std::string_view YE
Definition: LogControl.cc:298
#define USR
Definition: Logger.h:101
virtual int overflow(int ch=EOF)
Definition: LogControl.cc:432
void setLineFormater(const shared_ptr< LogControl::LineFormater > &format_r)
Assert _lineFormater is not NULL.
Definition: LogControl.cc:579
shared_ptr< LogControl::LineFormater > _lineFormater
Definition: LogControl.cc:603
std::ostream & getStream(const char *fil_r, const char *fnc_r, int lne_r)
Definition: LogControl.cc:495
bool hideThreadName() const
Hint for Formater whether to hide the thread name.
Definition: LogControl.cc:548
static LogControlImpl * instance()
The LogControlImpl singleton.
Definition: LogControl.cc:694
std::thread _thread
Definition: LogControl.cc:199
boost::shared_ptr< log::LineWriter > _lineWriter
Definition: LogControl.cc:207
boost::shared_ptr< log::LineWriter > getLineWriter()
Definition: LogControl.cc:101
std::once_flag flagReadEnvAutomatically
Definition: LogControl.cc:52
TriBool _hideThreadName
Hint for Formater whether to hide the thread name.
Definition: LogControl.cc:601
std::map< std::string, StreamSet > StreamTable
Definition: LogControl.cc:647
static constexpr std::string_view RE
Definition: LogControl.cc:300
static unsigned _depth
Definition: Logger.h:34
void logNothing()
Turn off logging.
Definition: LogControl.cc:850
void setLineWriter(boost::shared_ptr< log::LineWriter > writer)
Definition: LogControl.cc:96
#define L_USR(GROUP)
Definition: Logger.h:110
virtual std::string format(const std::string &, logger::LogLevel, const char *, const char *, int, const std::string &)
Definition: LogControl.cc:768
Maintain logfile related options.
Definition: LogControl.h:96
virtual int writeout(const char *s, std::streamsize n)
Definition: LogControl.cc:442
static constexpr std::string_view OO
Definition: LogControl.cc:295
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&#39;s what Loglinebuf calls.
Definition: LogControl.cc:738
static Date now()
Return the current time.
Definition: Date.h:78
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.
Definition: LogControl.cc:632
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Definition: LogControl.cc:429
void hideThreadName(bool onOff_r)
Definition: LogControl.cc:555
bool ensureConnection()
Definition: LogControl.cc:226
shared_ptr< LineWriter > getLineWriter() const
Get the current LineWriter.
Definition: LogControl.cc:825
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:92
SpinLock _lineWriterLock
Definition: LogControl.cc:205
Loglinebuf(const std::string &group_r, LogLevel level_r)
Definition: LogControl.cc:405
Pathname _file
Definition: SystemCheck.cc:34
int & logControlValidFlag()
Definition: LogControl.cc:515
void setLineFormater(const shared_ptr< LineFormater > &formater_r)
Assign a LineFormater.
Definition: LogControl.cc:842
shared_ptr< void > _outs
Definition: LogControl.h:76
shared_ptr< Loglinestream > StreamPtr
Definition: LogControl.cc:645
const char * _file
Definition: Logger.h:35
constexpr std::string_view ZYPP_MAIN_THREAD_NAME("Zypp-main")
void pushMessage(std::string &&msg)
Definition: LogControl.cc:241
Excessive logging.
Definition: Logger.h:146
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
Definition: LogControl.cc:866
std::string tracestr(char tag_r, unsigned depth_r, const char *file_r, const char *fnc_r, int line_r)
Definition: LogControl.cc:305
static constexpr std::string_view CY
Definition: LogControl.cc:297
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.
Definition: LogControl.cc:716
static LogThread & instance()
Definition: LogControl.cc:91
LogControl implementation (thread_local Singleton).
Definition: LogControl.cc:537
FileLineWriter(const Pathname &file_r, mode_t mode_r=0)
Definition: LogControl.cc:358
std::ostream & operator<<(std::ostream &str, const LogControlImpl &)
Definition: LogControl.cc:705
StreamTable _streamtable
one streambuffer per group and level
Definition: LogControl.cc:649