libzypp  13.10.6
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 
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/Pathname.h"
20 
22 namespace zypp
23 {
24 
26  namespace log
27  {
28 
34  struct LineWriter
35  {
36  virtual void writeOut( const std::string & /*formated_r*/ )
37  {}
38  virtual ~LineWriter()
39  {}
40  };
41 
43  struct StreamLineWriter : public LineWriter
44  {
45  StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {}
46 
47  virtual void writeOut( const std::string & formated_r )
48  { (*_str) << formated_r << std::endl; }
49 
50  protected:
51  StreamLineWriter() : _str( 0 ) {}
52  std::ostream *_str;
53  };
54 
57  {
59  };
60 
63  {
65  };
66 
72  {
73  FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 );
74  protected:
75  shared_ptr<void> _outs;
76  };
77 
79  } // namespace log
81 
82 
84  namespace base
85  {
86 
88  //
89  // CLASS NAME : LogControl
90  //
95  class LogControl
96  {
97  friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
98 
99  public:
102  { return LogControl(); }
103 
104 
107 
114  {
115  virtual std::string format( const std::string & /*group_r*/,
116  logger::LogLevel /*level_r*/,
117  const char * /*file_r*/,
118  const char * /*func_r*/,
119  int /*line_r*/,
120  const std::string & /*message_r*/ );
121  virtual ~LineFormater() {}
122  };
123 
124  public:
129  void setLineFormater( const shared_ptr<LineFormater> & formater_r );
130 
131  public:
138  void logfile( const Pathname & logfile_r );
139  void logfile( const Pathname & logfile_r, mode_t mode_r );
140 
142  void logNothing();
143 
145  void logToStdErr();
146 
147  public:
149  shared_ptr<LineWriter> getLineWriter() const;
150 
156  void setLineWriter( const shared_ptr<LineWriter> & writer_r );
157 
158  public:
161  {
162  TmpExcessive();
163  ~TmpExcessive();
164  };
165 
170  {
171  TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() )
174 
180  template<class _LineWriter>
181  TmpLineWriter( _LineWriter * _allocated_r )
183  { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
184 
187 
188  private:
189  shared_ptr<LineWriter> _writer;
190  };
191 
192  private:
195  {}
196  };
198 
200  std::ostream & operator<<( std::ostream & str, const LogControl & obj );
201 
203  } // namespace base
206 } // namespace zypp
208 #endif // ZYPP_BASE_LOGCONTROL_H
LogControl()
Default ctor: Singleton.
Definition: LogControl.h:194
LogLevel
Definition of log levels.
Definition: Logger.h:86
Base class for ostream based LineWriter.
Definition: LogControl.h:43
virtual void writeOut(const std::string &formated_r)
Definition: LogControl.h:47
LineWriter to file.
Definition: LogControl.h:71
If you want to format loglines by yourself, derive from this, and overload format.
Definition: LogControl.h:113
Exchange LineWriter for the lifetime of this object.
Definition: LogControl.h:169
LineWriter to stderr.
Definition: LogControl.h:62
virtual ~LineWriter()
Definition: LogControl.h:38
void logfile(const Pathname &logfile_r)
Set path for the logfile.
Definition: LogControl.cc:404
static LogControl instance()
Singleton access.
Definition: LogControl.h:101
void setLineWriter(const shared_ptr< LineWriter > &writer_r)
Assign a LineWriter.
Definition: LogControl.cc:413
void logToStdErr()
Log to std::err.
Definition: LogControl.cc:422
friend std::ostream & operator<<(std::ostream &str, const LogControl &obj)
Definition: LogControl.cc:440
StreamLineWriter(std::ostream &str_r)
Definition: LogControl.h:45
Turn on excessive logging for the lifetime of this object.
Definition: LogControl.h:160
void logNothing()
Turn off logging.
Definition: LogControl.cc:419
virtual std::string format(const std::string &, logger::LogLevel, const char *, const char *, int, const std::string &)
Definition: LogControl.cc:76
Maintain logfile related options.
Definition: LogControl.h:95
log::LineWriter LineWriter
Definition: LogControl.h:106
If you want to log the (formated) loglines by yourself, derive from this, and overload writeOut...
Definition: LogControl.h:34
std::ostream & operator<<(std::ostream &str, const Flags< Enum > &obj)
Definition: Flags.h:110
shared_ptr< LineWriter > _writer
Definition: LogControl.h:189
void setLineFormater(const shared_ptr< LineFormater > &formater_r)
Assign a LineFormater.
Definition: LogControl.cc:416
shared_ptr< void > _outs
Definition: LogControl.h:75
virtual void writeOut(const std::string &)
Definition: LogControl.h:36
shared_ptr< LineWriter > getLineWriter() const
Get the current LineWriter.
Definition: LogControl.cc:410
LineWriter to stdout.
Definition: LogControl.h:56
TmpLineWriter(_LineWriter *_allocated_r)
Convenience ctor taking over ownership of an allocated LineWriter.
Definition: LogControl.h:181
FileLineWriter(const Pathname &file_r, mode_t mode_r=0)
Definition: LogControl.cc:41