libzypp 17.38.15

LogControl.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_BASE_LOGCONTROL_H
13#define ZYPP_BASE_LOGCONTROL_H
14
15#include <iosfwd>
16#include <ostream> //for std::endl
17
18#include <zypp-core/base/Logger.h>
19#include <zypp-core/base/PtrTypes.h>
20#include <zypp-core/Pathname.h>
21
23 namespace zypp
24{
25
27 namespace log
28 {
29
35 struct ZYPP_API LineWriter
36 {
37 virtual void writeOut( const std::string & /*formated_r*/ )
38 {}
39 virtual ~LineWriter()
40 {}
41 };
42
44 struct StreamLineWriter : public LineWriter
45 {
46 StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {}
47
48 void writeOut( const std::string & formated_r ) override
49 { (*_str) << formated_r << std::endl; }
50
51 protected:
52 StreamLineWriter() : _str( 0 ) {}
53 std::ostream *_str;
54 };
55
57 struct StdoutLineWriter : public StreamLineWriter
58 {
59 StdoutLineWriter();
60 };
61
63 struct StderrLineWriter : public StreamLineWriter
64 {
65 StderrLineWriter();
66 };
67
72 struct ZYPP_API FileLineWriter : public StreamLineWriter
73 {
74 FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 );
75 protected:
76 shared_ptr<void> _outs;
77 };
78
80 } // namespace log
82
83
85 namespace base
86 {
87
89 //
90 // CLASS NAME : LogControl
91 //
96 class ZYPP_API LogControl
97 {
98 friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
99
100 public:
102 static LogControl instance()
103 { return LogControl(); }
104
105
107 using LineWriter = log::LineWriter;
108
114 struct ZYPP_API LineFormater
115 {
116 virtual std::string format( const std::string & /*group_r*/,
117 logger::LogLevel /*level_r*/,
118 const char * /*file_r*/,
119 const char * /*func_r*/,
120 int /*line_r*/,
121 const std::string & /*message_r*/ );
122 virtual ~LineFormater() {}
123 };
124
128 struct ZYPP_API JournalLineFormater: public LineFormater
129 {
130 std::string format( const std::string & /*group_r*/,
131 logger::LogLevel /*level_r*/,
132 const char * /*file_r*/,
133 const char * /*func_r*/,
134 int /*line_r*/,
135 const std::string & /*message_r*/ ) override;
136 };
137
138 public:
143 void setLineFormater( const shared_ptr<LineFormater> & formater_r );
144
150 void enableLogForwardingMode ( bool enable = true );
151
152 public:
159 void logfile( const Pathname & logfile_r );
160 void logfile( const Pathname & logfile_r, mode_t mode_r );
161
163 void logNothing();
164
166 void logToStdErr();
167
169 void emergencyShutdown();
170
176 static void notifyFork();
177
179 void logRawLine ( std::string &&line );
180
181 public:
183 shared_ptr<LineWriter> getLineWriter() const;
184
190 void setLineWriter( const shared_ptr<LineWriter> & writer_r );
191
192 public:
194 struct TmpExcessive
195 {
196 TmpExcessive();
197 ~TmpExcessive();
198 };
199
203 struct ZYPP_API TmpLineWriter
204 {
205 TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() )
206 : _writer( LogControl::instance().getLineWriter() )
207 { LogControl::instance().setLineWriter( writer_r ); }
208
214 template<class TLineWriter>
215 TmpLineWriter( TLineWriter * _allocated_r )
216 : _writer( LogControl::instance().getLineWriter() )
217 { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
218
219 ~TmpLineWriter()
220 { LogControl::instance().setLineWriter( _writer ); }
221
222 private:
223 shared_ptr<LineWriter> _writer;
224 };
225
226 private:
228 LogControl()
229 {}
230 };
231
232
234 std::ostream & operator<<( std::ostream & str, const LogControl & obj ) ZYPP_API;
235
237 } // namespace base
240} // namespace zypp
242#endif // ZYPP_BASE_LOGCONTROL_H
Logger.h
Pathname.h
PtrTypes.h
shared_ptr
zypp::base::LogControl::operator<<
friend std::ostream & operator<<(std::ostream &str, const LogControl &obj)
relates: LogControl Stream output
Definition LogControl.cc:1005
zypp::base::LogControl::LogControl
LogControl()
Default ctor: Singleton.
Definition LogControl.h:228
zypp::base::LogControl::getLineWriter
shared_ptr< LineWriter > getLineWriter() const
Get the current LineWriter.
Definition LogControl.cc:919
zypp::base::LogControl::setLineWriter
void setLineWriter(const shared_ptr< LineWriter > &writer_r)
Assign a LineWriter.
Definition LogControl.cc:928
zypp::base::LogControl::logToStdErr
void logToStdErr()
Log to std::err.
Definition LogControl.cc:957
zypp::base::LogControl::logRawLine
void logRawLine(std::string &&line)
will push a line to the logthread without formatting it
Definition LogControl.cc:975
zypp::base::LogControl::LineWriter
log::LineWriter LineWriter
Definition LogControl.h:107
zypp::base::LogControl::logNothing
void logNothing()
Turn off logging.
Definition LogControl.cc:949
zypp::base::LogControl::notifyFork
static void notifyFork()
This will completely disable logging.
Definition LogControl.cc:970
zypp::base::LogControl::setLineFormater
void setLineFormater(const shared_ptr< LineFormater > &formater_r)
Assign a LineFormater.
Definition LogControl.cc:936
zypp::base::LogControl::instance
static LogControl instance()
Singleton access.
Definition LogControl.h:102
zypp::base::LogControl::enableLogForwardingMode
void enableLogForwardingMode(bool enable=true)
Definition LogControl.cc:944
zypp::base::LogControl::logfile
void logfile(const Pathname &logfile_r)
Set path for the logfile.
Definition LogControl.cc:901
zypp::base::LogControl::emergencyShutdown
void emergencyShutdown()
will cause the log thread to exit and flush all sockets
Definition LogControl.cc:965
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
str
String related utilities and Regular expression matching.
zypp::base::logger::LogLevel
LogLevel
Definition of log levels.
Definition Logger.h:186
zypp::base
Definition Random.cc:7
zypp::log
Definition LogControl.cc:380
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::base::LogControl::JournalLineFormater
Our default format but without date, host and PID info.
Definition LogControl.h:129
zypp::base::LogControl::JournalLineFormater::format
std::string format(const std::string &, logger::LogLevel, const char *, const char *, int, const std::string &) override
Definition LogControl.cc:872
zypp::base::LogControl::LineFormater
If you want to format loglines by yourself, derive from this, and overload format.
Definition LogControl.h:115
zypp::base::LogControl::LineFormater::~LineFormater
virtual ~LineFormater()
Definition LogControl.h:122
zypp::base::LogControl::LineFormater::format
virtual std::string format(const std::string &, logger::LogLevel, const char *, const char *, int, const std::string &)
Definition LogControl.cc:839
zypp::base::LogControl::TmpExcessive::TmpExcessive
TmpExcessive()
Definition LogControl.cc:985
zypp::base::LogControl::TmpLineWriter::TmpLineWriter
TmpLineWriter(const shared_ptr< LineWriter > &writer_r=shared_ptr< LineWriter >())
Definition LogControl.h:205
zypp::base::LogControl::TmpLineWriter::_writer
shared_ptr< LineWriter > _writer
Definition LogControl.h:223
zypp::base::LogControl::TmpLineWriter::~TmpLineWriter
~TmpLineWriter()
Definition LogControl.h:219
zypp::base::LogControl::TmpLineWriter::TmpLineWriter
TmpLineWriter(TLineWriter *_allocated_r)
Convenience ctor taking over ownership of an allocated LineWriter.
Definition LogControl.h:215
zypp::log::FileLineWriter::_outs
shared_ptr< void > _outs
Definition LogControl.h:76
zypp::log::FileLineWriter::FileLineWriter
FileLineWriter(const Pathname &file_r, mode_t mode_r=0)
Definition LogControl.cc:390
zypp::log::LineWriter
If you want to log the (formated) loglines by yourself, derive from this, and overload writeOut.
Definition LogControl.h:36
zypp::log::LineWriter::writeOut
virtual void writeOut(const std::string &)
Definition LogControl.h:37
zypp::log::LineWriter::~LineWriter
virtual ~LineWriter()
Definition LogControl.h:39
zypp::log::StderrLineWriter::StderrLineWriter
StderrLineWriter()
Definition LogControl.cc:386
zypp::log::StdoutLineWriter::StdoutLineWriter
StdoutLineWriter()
Definition LogControl.cc:382
zypp::log::StreamLineWriter::StreamLineWriter
StreamLineWriter()
Definition LogControl.h:52
zypp::log::StreamLineWriter::_str
std::ostream * _str
Definition LogControl.h:53
zypp::log::StreamLineWriter::StreamLineWriter
StreamLineWriter(std::ostream &str_r)
Definition LogControl.h:46
zypp::log::StreamLineWriter::writeOut
void writeOut(const std::string &formated_r) override
Definition LogControl.h:48