00001 #ifndef DV_UTIL_LOGSTREAM_H 00002 #define DV_UTIL_LOGSTREAM_H 00003 // $Id: logstream.h,v 1.12 2008/03/15 09:31:46 dvermeir Exp $ 00004 00005 #include <string> 00006 #include <iostream> 00007 #include "filterstreambuf.h" 00008 00009 /** \file 00010 * This file defines a class logstream, which uses 00011 * a Dv::Util::filterstreambuf to prepend 00012 * a date and a prefix string to each output line. 00013 */ 00014 00015 namespace Dv { 00016 namespace Util { 00017 00018 /** 00019 * A logstream is an ostream useful for logging. 00020 * Each line is preceeded by the date and an 00021 * application-determined identification string. 00022 * @warning 00023 * Output is line-oriented and buffered, i.e. 00024 * output will be kept until @a '\\n' is seen, at which time 00025 * the whole line (preceeded by prefix and date information) 00026 * will be written to the output. 00027 * 00028 */ 00029 class logstream: public std::ostream { 00030 private: 00031 /** 00032 * Implements Filter interface for filterstreambuf 00033 */ 00034 class LogFilter { 00035 public: 00036 /** 00037 * Constructor. 00038 * \param path name of file on which log will be written. 00039 * \param prefix: each output line will start with the 00040 * current date and the prefix string \sa Dv::Util::Date 00041 */ 00042 LogFilter(const std::string& path,const std::string& prefix); 00043 /** 00044 * Constructor, stream is managed by client. 00045 * \param stream open output stream on which log will be written. 00046 * \param prefix: each output line will start with the 00047 * current date and the prefix string \sa Dv::Util::Date 00048 */ 00049 LogFilter(std::ostream& stream,const std::string& prefix); 00050 /** 00051 * Destructor. 00052 */ 00053 ~LogFilter(); 00054 /** Output a character. 00055 * @param c char to output 00056 * @return @a c is all went well, @a EOF otherwise. 00057 */ 00058 int put(int c); 00059 /** Input a character, always fails. 00060 * @return EOF, this is an output-only filter. */ 00061 int get() { return EOF; } 00062 /** Flush buffers. 00063 * @return 0 if ok, @a EOF otherwise 00064 */ 00065 int sync(); 00066 /** 00067 * This is a noop. 00068 */ 00069 void close() {} 00070 /** 00071 * Set prefix that will be printer at the start of each output line. 00072 */ 00073 void prefix(const std::string& prefix) { prefix_ = prefix; } 00074 /** 00075 * \return prefix that is used on output 00076 * \sa prefix(const std::string&) 00077 */ 00078 const std::string& prefix() const { return prefix_; } 00079 /** 00080 * \return iostate of underlying stream 00081 */ 00082 std::ios::iostate state() const; 00083 private: 00084 bool owner_; 00085 std::string buf_; 00086 std::string prefix_; 00087 std::ostream* stream_; 00088 bool lastnl_; 00089 }; 00090 public: 00091 /** 00092 * Creates ofstream for path. 00093 * The stream will be opened with mode \a ios:app, i.e. 00094 * a seek to the end of the stream will be performed 00095 * before each output operation. 00096 * \param path on which log output will be written. 00097 * \param prefix each output line will start with the 00098 * date and this prefix string 00099 * \sa Dv::Util::Date 00100 */ 00101 logstream(const std::string& path, const std::string& prefix); 00102 /** 00103 * Constructor. 00104 * \param stream existing stream on which output will be written. 00105 * \param prefix each output line will start with the 00106 * date and this prefix string 00107 */ 00108 logstream(std::ostream& stream, const std::string& prefix); 00109 /** 00110 * Destructor. 00111 */ 00112 ~logstream(); 00113 /** 00114 * \return current prefix that is put at the start of each line. 00115 * \sa Dv::Util::logstream::logstream 00116 */ 00117 const std::string& prefix() const { return filter_.prefix(); } 00118 /** 00119 * \param s prefix that is put at the start of each line. 00120 * \sa Dv::Util::logstream::logstream 00121 */ 00122 void prefix(const std::string& s) { filter_.prefix(s); } 00123 private: 00124 LogFilter filter_; 00125 }; 00126 } 00127 } 00128 00129 #endif
dvutil-1.0.10 | [ 5 December, 2009] |