28 #include <zypp-core/ng/io/Socket> 29 #include <zypp-core/ng/io/SockAddr> 30 #include <zypp-core/ng/base/EventLoop> 31 #include <zypp-core/ng/base/EventDispatcher> 32 #include <zypp-core/ng/base/Timer> 34 #include <zypp-core/ng/thread/Wakeup> 36 #include <zypp-core/ng/base/SocketNotifier> 45 #include <sys/types.h> 72 std::this_thread::yield();
113 if (
_thread.get_id() != std::this_thread::get_id() )
132 _thread = std::thread( [
this] () {
148 std::vector<zyppng::Socket::Ptr> clients;
151 server->bind( std::make_shared<zyppng::UnixSockAddr>(
sockPath(),
true ) );
157 auto cl = server->accept();
159 clients.push_back( cl );
165 if ( !writer )
return;
166 while ( sock->canReadLine() ) {
167 auto br = sock->readLine();
168 writer->writeOut( std::string( br.data(), br.size() - 1 ) );
174 auto lock = sock.lock();
178 auto idx = std::find_if( clients.begin(), clients.end(), [lock](
const auto &s ){
return lock.get() == s.get(); } );
179 clients.erase( idx );
193 for (
auto &sock : clients ){
194 auto br = sock->readLine();
195 while ( !br.empty() ) {
196 if ( br.back () ==
'\n' )
197 writer->writeOut( std::string( br.data(), br.size() - 1 ) );
199 writer->writeOut( std::string( br.data(), br.size() ) );
201 br = sock->readLine();
242 _sockFD = ::socket( AF_UNIX, SOCK_STREAM, 0 );
268 writer->writeOut( msg );
275 if ( msg.back() !=
'\n' )
279 while ( written < msg.size() ) {
308 static constexpr std::string_view
OO {
"\033[0m" };
309 static constexpr std::string_view
WH {
"\033[37;40m" };
310 static constexpr std::string_view
CY {
"\033[36;40m" };
311 static constexpr std::string_view
YE {
"\033[33;1;40m" };
312 static constexpr std::string_view
GR {
"\033[32;40m" };
313 static constexpr std::string_view
RE {
"\033[31;1;40m" };
314 static constexpr std::string_view
MA {
"\033[35;40m" };
318 std::string
tracestr(
char tag_r,
unsigned depth_r,
const std::string & msg_r,
const char * file_r,
const char * fnc_r,
int line_r )
320 static str::Format fmt {
"***%2d %s%c %s(%s):%d %s" };
321 fmt % depth_r %std::string(depth_r,
'.') % tag_r %
Pathname::basename(file_r) % fnc_r % line_r % msg_r;
329 , _msg(
std::move(msg_r) )
331 unsigned depth =
_depth++;
333 Osd(
L_USR(
"TRACE"),depth) << m << endl;
338 unsigned depth = --
_depth;
340 Osd(
L_USR(
"TRACE"),depth) << m << endl;
344 : _strout { std::cerr }
346 { _strout << (i?
WH:
YE); }
364 #endif // ZYPP_NDEBUG 390 int fd = ::open( file_r.
c_str(), O_CREAT|O_EXCL, mode_r );
395 std::ofstream * fstr = 0;
396 _outs.reset( (fstr =
new std::ofstream( file_r.
asString().c_str(), std::ios_base::app )) );
397 fstr->rdbuf()->pubsetbuf(0,0);
414 const char * file_r,
const char * func_r,
int line_r,
415 const std::string & buffer_r );
446 void tagSet(
const char * fil_r,
const char * fnc_r,
int lne_r )
455 std::streamsize
xsputn(
const char * s, std::streamsize n )
override 468 virtual int writeout(
const char* s, std::streamsize n )
475 for (
int i = 0; i < n; ++i, ++c )
478 _buffer += std::string( s, c-s );
486 _buffer += std::string( s, c-s );
512 :
_mybuf( group_r, level_r )
527 std::ostream &
getStream(
const char * fil_r,
const char * fnc_r,
int lne_r )
552 static thread_local
int logControlValid = 0;
553 return logControlValid;
593 return impl ? impl->hideThreadName() :
false;
599 if ( impl ) impl->hideThreadName( onOff_r );
606 return impl ? impl->_logToPPIDMode :
false;
614 impl->_logToPPIDMode = onOff_r;
635 if ( logfile_r.
empty() )
637 else if ( logfile_r ==
Pathname(
"-" ) )
673 ret <<
"---<RESET LOGSTREAM FROM FAILED STATE]" << endl;
688 const std::string & message_r )
691 file_r, func_r, line_r,
706 if ( getenv(
"ZYPP_LOGFILE") )
707 logfile( getenv(
"ZYPP_LOGFILE") );
709 if ( getenv(
"ZYPP_PROFILING") )
766 return str <<
"LogControlImpl";
781 static std::ostream nstream(NULL);
783 if ( !control || !group_r || strlen(group_r ) == 0 ) {
789 return control->getStream( group_r,
798 const char * file_r,
const char * func_r,
int line_r,
799 const std::string & buffer_r )
805 control->putStream( group_r, level_r,
806 file_r, func_r, line_r,
815 return impl->isExcessive();
822 using logger::LogControlImpl;
832 const std::string & message_r )
834 static char hostname[1024];
835 static char nohostname[] =
"unknown";
841 ret =
str::form(
"%s <%d> %s(%d) [%s] %s(%s):%d %s",
842 now.c_str(), level_r,
843 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
846 file_r, func_r, line_r,
849 ret =
str::form(
"%s <%d> %s(%d) [%s] %s(%s):%d {T:%s} %s",
850 now.c_str(), level_r,
851 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
852 logToPPID ? getppid() : getpid(),
854 file_r, func_r, line_r,
865 const std::string & message_r )
869 ret =
str::form(
"<%d> [%s] %s(%s):%d %s",
870 level_r, group_r.c_str(),
871 file_r, func_r, line_r,
874 ret =
str::form(
"<%d> [%s] %s(%s):%d {T:%s} %s",
875 level_r, group_r.c_str(),
876 file_r, func_r, line_r,
895 impl->logfile( logfile_r );
904 impl->logfile( logfile_r, mode_r );
913 return impl->getLineWriter();
921 impl->setLineWriter( writer_r );
929 impl->setLineFormater( formater_r );
942 impl->setLineWriter( shared_ptr<LineWriter>() );
978 impl->excessive(
true );
985 impl->excessive(
false );
LogControlImpl & operator=(const LogControlImpl &)=delete
void enableLogForwardingMode(bool enable=true)
std::atomic_flag _atomicLock
Osd & operator<<(Tp &&val)
LogClient & operator=(const LogClient &)=delete
static Ptr create(int domain, int type, int protocol)
LogLevel
Definition of log levels.
constexpr bool always_false_v
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)
static constexpr std::string_view MA
Base class for ostream based LineWriter.
static bool instanceHideThreadName()
void tagSet(const char *fil_r, const char *fnc_r, int lne_r)
zypp::shared_ptr< log::LineWriter > getLineWriter()
Loglinestream(const std::string &group_r, LogLevel level_r)
static void instanceSetLogToPPID(bool onOff_r)
bool trySocketConnection(int &sockFD, const SockAddr &addr, uint64_t timeout)
const std::string & name() const
static constexpr std::string_view WH
const char * c_str() const
String representation.
String related utilities and Regular expression matching.
SignalProxy< void()> sigIncomingConnection()
void excessive(bool onOff_r)
shared_ptr< log::LineWriter > _lineWriter
static void instanceHideThreadName(bool onOff_r)
std::string basename() const
Return the last component of this path.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
LogControlImpl()
Singleton ctor.
Osd(std::ostream &, int=0)
void setLineWriter(const shared_ptr< LogControl::LineWriter > &writer_r)
NULL _lineWriter indicates no loggin.
static std::string sockPath()
void logfile(const Pathname &logfile_r, mode_t mode_r=0640)
void logfile(const Pathname &logfile_r)
Set path for the logfile.
LogThread & operator=(const LogThread &)=delete
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
void setLineWriter(const shared_ptr< LineWriter > &writer_r)
Assign a LineWriter.
std::shared_ptr< Socket > Ptr
std::string tracestr(char tag_r, unsigned depth_r, const std::string &msg_r, const char *file_r, const char *fnc_r, int line_r)
bool _logToPPIDMode
Hint for formatter to use the PPID and always show the thread name.
void logToStdErr()
Log to std::err.
bool empty() const
Test for an empty path.
TraceLeave(const TraceLeave &)=delete
std::shared_ptr< SocketNotifier > makeNotifier(const bool enabled=true) const
std::thread::id threadId()
int overflow(int ch=EOF) override
static bool instanceLogToPPID()
Hint for formatter wether we forward all logs to a parents log.
shared_ptr< LogControl::LineWriter > getLineWriter() const
zyppng::Wakeup _stopSignal
const std::string & asString() const
String representation.
shared_ptr< Loglinestream > StreamPtr
static constexpr std::string_view GR
friend std::ostream & operator<<(std::ostream &str, const LogControl &obj)
relates: LogControl Stream output
Loglinebuf & operator=(const Loglinebuf &)=default
static constexpr std::string_view YE
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)
bool hideThreadName() const
Hint for Formater whether to hide the thread name.
static LogControlImpl * instance()
The LogControlImpl singleton.
std::map< LogLevel, StreamPtr > StreamSet
std::once_flag flagReadEnvAutomatically
void putRawLine(std::string &&line)
TriBool _hideThreadName
Hint for Formater whether to hide the thread name.
static constexpr std::string_view RE
std::map< std::string, StreamSet > StreamTable
Loglinebuf(std::string group_r, LogLevel level_r)
void logNothing()
Turn off logging.
Loglinestream & operator=(const Loglinestream &)=delete
void pushMessage(std::string msg)
std::streamsize xsputn(const char *s, std::streamsize n) override
static ZYPP_API ThreadData & current()
Maintain logfile related options.
virtual int writeout(const char *s, std::streamsize n)
static constexpr std::string_view OO
SignalProxy< void()> sigReadyRead()
static void notifyFork()
This will completely disable logging.
std::ostream & operator<<(std::ostream &str, const LogControlImpl &)
relates: LogControlImpl Stream output
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.
static Date now()
Return the current time.
auto eintrSafeCall(Fun &&function, Args &&... args)
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.
void setLineWriter(zypp::shared_ptr< log::LineWriter > writer)
void logRawLine(std::string &&line)
will push a line to the logthread without formatting it
void hideThreadName(bool onOff_r)
shared_ptr< LineWriter > getLineWriter() const
Get the current LineWriter.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
SignalProxy< void(const SocketNotifier &sock, int evTypes)> sigActivated()
int & logControlValidFlag()
void setLineFormater(const shared_ptr< LineFormater > &formater_r)
Assign a LineFormater.
constexpr std::string_view ZYPP_MAIN_THREAD_NAME("Zypp-main")
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.
Easy-to use interface to the ZYPP dependency resolver.
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
bool blockAllSignalsForCurrentThread()
static constexpr std::string_view CY
zyppng::Socket::Ptr _sock
SignalProxy< void()> sigDisconnected()
static Ptr create(GMainContext *ctx=nullptr)
static LogThread & instance()
LogControl implementation (thread_local Singleton).
FileLineWriter(const Pathname &file_r, mode_t mode_r=0)
StreamTable _streamtable
one streambuffer per group and level