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>
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>
70 std::this_thread::yield();
109 if (
_thread.get_id() != std::this_thread::get_id() )
128 _thread = std::thread( [
this] () {
136 zyppng::blockAllSignalsForCurrentThread();
138 zyppng::ThreadData::current().setName(
"Zypp-Log");
140 auto ev = zyppng::EventLoop::create();
141 auto server = zyppng::Socket::create( AF_UNIX, SOCK_STREAM, 0 );
142 auto stopNotifyWatch =
_stopSignal.makeNotifier( );
144 std::vector<zyppng::Socket::Ptr> clients;
147 server->bind( std::make_shared<zyppng::UnixSockAddr>(
sockPath(),
true ) );
151 server->connectFunc( &zyppng::Socket::sigIncomingConnection, [&](){
153 auto cl = server->accept();
155 clients.push_back( cl );
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 ) );
169 cl->connectFunc( &zyppng::Socket::sigDisconnected, [&clients, sock = std::weak_ptr(cl)](){
170 auto lock = sock.lock();
174 auto idx = std::find_if( clients.begin(), clients.end(), [lock](
const auto &s ){ return lock.get() == s.get(); } );
175 clients.erase( idx );
180 stopNotifyWatch->connectFunc( &zyppng::SocketNotifier::sigActivated, [&ev](
const auto &,
auto ) {
189 for (
auto &sock : clients ){
190 auto br = sock->readLine();
191 while ( !br.empty() ) {
192 if ( br.back () ==
'\n' )
193 writer->writeOut( std::string( br.data(), br.size() - 1 ) );
195 writer->writeOut( std::string( br.data(), br.size() ) );
197 br = sock->readLine();
212 boost::shared_ptr<log::LineWriter> _lineWriter{
nullptr };
220 LogThread::instance();
235 _sockFD = ::socket( AF_UNIX, SOCK_STREAM, 0 );
239 zyppng::UnixSockAddr addr( LogThread::sockPath(),
true );
240 return zyppng::trySocketConnection( _sockFD, addr, 100 );
247 if ( inPushMessage ) {
255 inPushMessage =
true;
258 if ( std::this_thread::get_id() == LogThread::instance().threadId() ) {
259 auto writer = LogThread::instance().getLineWriter();
261 writer->writeOut( msg );
265 if(!ensureConnection())
268 if ( msg.back() !=
'\n' )
272 while ( written < msg.size() ) {
273 const auto res = zyppng::eintrSafeCall( ::send, _sockFD, msg.data() + written, msg.size() - written, MSG_NOSIGNAL );
286 bool inPushMessage =
false;
301 static constexpr std::string_view
OO {
"\033[0m" };
302 static constexpr std::string_view
WH {
"\033[37;40m" };
303 static constexpr std::string_view
CY {
"\033[36;40m" };
304 static constexpr std::string_view
YE {
"\033[33;1;40m" };
305 static constexpr std::string_view
GR {
"\033[32;40m" };
306 static constexpr std::string_view
RE {
"\033[31;1;40m" };
307 static constexpr std::string_view
MA {
"\033[35;40m" };
309 unsigned TraceLeave::_depth = 1;
311 std::string
tracestr(
char tag_r,
unsigned depth_r,
const char * file_r,
const char * fnc_r,
int line_r )
314 fmt % std::string(depth_r,tag_r) % file_r % fnc_r % line_r;
318 TraceLeave::TraceLeave(
const char * file_r,
const char * fnc_r,
int line_r )
320 , _fnc(
std::move(fnc_r) )
336 : _strout {
std::cerr }
376 int fd = ::open( file_r.
c_str(), O_CREAT|O_EXCL, mode_r );
381 std::ofstream * fstr = 0;
382 _outs.reset( (fstr =
new std::ofstream( file_r.
asString().c_str(), std::ios_base::app )) );
383 fstr->rdbuf()->pubsetbuf(0,0);
400 const char * file_r,
const char * func_r,
int line_r,
401 const std::string & buffer_r );
426 void tagSet(
const char * fil_r,
const char * fnc_r,
int lne_r )
435 virtual std::streamsize
xsputn(
const char * s, std::streamsize n )
448 virtual int writeout(
const char* s, std::streamsize n )
455 for (
int i = 0; i < n; ++i, ++c )
458 _buffer += std::string( s, c-s );
466 _buffer += std::string( s, c-s );
492 :
_mybuf( group_r, level_r )
501 std::ostream &
getStream(
const char * fil_r,
const char * fnc_r,
int lne_r )
526 static thread_local int logControlValid = 0;
527 return logControlValid;
568 return impl ? impl->hideThreadName() :
false;
574 if ( impl ) impl->hideThreadName( onOff_r );
581 return impl ? impl->_logToPPIDMode :
false;
589 impl->_logToPPIDMode = onOff_r;
610 if ( logfile_r.
empty() )
612 else if ( logfile_r ==
Pathname(
"-" ) )
648 ret <<
"---<RESET LOGSTREAM FROM FAILED STATE]" << endl;
663 const std::string & message_r )
666 file_r, func_r, line_r,
681 if ( getenv(
"ZYPP_LOGFILE") )
682 logfile( getenv(
"ZYPP_LOGFILE") );
684 if ( getenv(
"ZYPP_PROFILING") )
736 return str <<
"LogControlImpl";
751 static std::ostream nstream(NULL);
753 if ( !control || !group_r || strlen(group_r ) == 0 ) {
759 return control->getStream( group_r,
768 const char * file_r,
const char * func_r,
int line_r,
769 const std::string & buffer_r )
775 control->putStream( group_r, level_r,
776 file_r, func_r, line_r,
785 return impl->isExcessive();
792 using logger::LogControlImpl;
802 const std::string & message_r )
804 static char hostname[1024];
805 static char nohostname[] =
"unknown";
806 std::string now(
Date::now().form(
"%Y-%m-%d %H:%M:%S" ) );
811 ret =
str::form(
"%s <%d> %s(%d) [%s] %s(%s):%d %s",
812 now.c_str(), level_r,
813 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
816 file_r, func_r, line_r,
819 ret =
str::form(
"%s <%d> %s(%d) [%s] %s(%s):%d {T:%s} %s",
820 now.c_str(), level_r,
821 ( gethostname( hostname, 1024 ) ? nohostname : hostname ),
822 logToPPID ? getppid() : getpid(),
824 file_r, func_r, line_r,
825 zyppng::ThreadData::current().name().c_str(),
844 impl->logfile( logfile_r );
853 impl->logfile( logfile_r, mode_r );
862 return impl->getLineWriter();
870 impl->setLineWriter( writer_r );
878 impl->setLineFormater( formater_r );
891 impl->setLineWriter( shared_ptr<LineWriter>() );
927 impl->excessive(
true );
934 impl->excessive(
false );
std::once_flag flagReadEnvAutomatically
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
static Date now()
Return the current time.
void pushMessage(std::string &&msg)
boost::shared_ptr< log::LineWriter > getLineWriter()
zyppng::Wakeup _stopSignal
std::thread::id threadId()
static LogThread & instance()
static std::string sockPath()
boost::shared_ptr< log::LineWriter > _lineWriter
void setLineWriter(boost::shared_ptr< log::LineWriter > writer)
std::atomic_flag _atomicLock
Maintain logfile related options.
friend std::ostream & operator<<(std::ostream &str, const LogControl &obj)
shared_ptr< LineWriter > getLineWriter() const
Get the current LineWriter.
void setLineWriter(const shared_ptr< LineWriter > &writer_r)
Assign a LineWriter.
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
void logToStdErr()
Log to std::err.
void logRawLine(std::string &&line)
will push a line to the logthread without formatting it
void logNothing()
Turn off logging.
static void notifyFork()
This will completely disable logging.
void setLineFormater(const shared_ptr< LineFormater > &formater_r)
Assign a LineFormater.
void enableLogForwardingMode(bool enable=true)
void logfile(const Pathname &logfile_r)
Set path for the logfile.
virtual int overflow(int ch=EOF)
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Loglinebuf(const std::string &group_r, LogLevel level_r)
void tagSet(const char *fil_r, const char *fnc_r, int lne_r)
virtual int writeout(const char *s, std::streamsize n)
Loglinestream(const std::string &group_r, LogLevel level_r)
std::ostream & getStream(const char *fil_r, const char *fnc_r, int lne_r)
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
bool empty() const
Test for an empty path.
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
String related utilities and Regular expression matching.
int & logControlValidFlag()
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.
LogLevel
Definition of log levels.
@ E_XXX
Excessive logging.
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.
static constexpr std::string_view WH
std::string tracestr(char tag_r, unsigned depth_r, const char *file_r, const char *fnc_r, int line_r)
static constexpr std::string_view OO
static constexpr std::string_view YE
static constexpr std::string_view CY
static constexpr std::string_view GR
static constexpr std::string_view MA
static constexpr std::string_view RE
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Easy-to use interface to the ZYPP dependency resolver.
constexpr bool always_false_v
constexpr std::string_view ZYPP_MAIN_THREAD_NAME("Zypp-main")
LogControl implementation (thread_local Singleton).
void putRawLine(std::string &&line)
void setLineWriter(const shared_ptr< LogControl::LineWriter > &writer_r)
NULL _lineWriter indicates no loggin.
static LogControlImpl * instance()
The LogControlImpl singleton.
static bool instanceHideThreadName()
std::ostream & operator<<(std::ostream &str, const LogControlImpl &)
Stream output.
LogControlImpl()
Singleton ctor.
static void instanceHideThreadName(bool onOff_r)
static void instanceSetLogToPPID(bool onOff_r)
StreamTable _streamtable
one streambuffer per group and level
std::map< LogLevel, StreamPtr > StreamSet
static bool instanceLogToPPID()
Hint for formatter wether we forward all logs to a parents log.
std::map< std::string, StreamSet > StreamTable
void excessive(bool onOff_r)
shared_ptr< Loglinestream > StreamPtr
bool _logToPPIDMode
Hint for formatter to use the PPID and always show the thread name.
void setLineFormater(const shared_ptr< LogControl::LineFormater > &format_r)
Assert _lineFormater is not NULL.
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)
void logfile(const Pathname &logfile_r, mode_t mode_r=0640)
shared_ptr< LogControl::LineWriter > getLineWriter() const
shared_ptr< LogControl::LineFormater > _lineFormater
bool hideThreadName() const
Hint for Formater whether to hide the thread name.
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 hideThreadName(bool onOff_r)
zyppng::Socket::Ptr _sock
TriBool _hideThreadName
Hint for Formater whether to hide the thread name.
Osd(std::ostream &, int=0)
Osd & operator<<(Tp &&val)
FileLineWriter(const Pathname &file_r, mode_t mode_r=0)
Base class for ostream based LineWriter.