00001 #ifndef DV_THREAD_LOGSTREAM_H 00002 #define DV_THREAD_LOGSTREAM_H 00003 // $Id: logstream.h,v 1.3 2009/12/05 13:21:15 dvermeir Exp $ 00004 00005 #include <pthread.h> // pthread_t 00006 #include <dvthread/monitor.h> // pthread_t 00007 #include <iostream> 00008 #include <cstdio> // for EOF and gcc-4.4.1 00009 #include <map> 00010 #include <string> 00011 00012 namespace Dv { 00013 namespace Thread { 00014 /** A Dv::Thread::logstream is an ostream useful for logging. Each line is preceeded 00015 * by the date, the id of the writing thread, and an application-determined 00016 * identification string. 00017 * @warning Output is line-oriented and buffered, i.e. output will be kept 00018 * until @a '\\n' is seen, at which time the whole line (preceeded by 00019 * prefix, thread and date information) will be written to the output. 00020 * @warning This code assumes that @a pthread_t is a type such that 00021 * @c t1<t2 is well-defined for two @a pthread_t values @c t1 and @c t2. 00022 * @sa Dv::Util::logstream for a non-thread safe version. 00023 */ 00024 class logstream: public std::ostream { 00025 private: 00026 /** Implements Filter interface for filterstreambuf */ 00027 class LogFilter: public Monitor { 00028 public: 00029 /** Constructor. 00030 * @param path name of file on which log will be written. 00031 * @param prefix: each output line will start with the current date, the 00032 * id of the writing thread and the prefix string @sa Dv::Util::Date 00033 */ 00034 LogFilter(const std::string& path,const std::string& prefix); 00035 /** Constructor, stream is managed by client. 00036 * @param stream open output stream on which log will be written. 00037 * @param prefix: each output line will start with the current date, the 00038 * id of the writing thread and the prefix string 00039 * @sa Dv::Util::Date 00040 */ 00041 LogFilter(std::ostream& stream,const std::string& prefix); 00042 /** Destructor. */ 00043 ~LogFilter(); 00044 /** Output a character. 00045 * @param c char to output 00046 * @return @a c is all went well, @a EOF otherwise. 00047 */ 00048 int put(int c); 00049 /** Input a character, always fails. 00050 * @return EOF, this is an output-only filter. 00051 */ 00052 int get() { return EOF; } 00053 /** Flush buffers. 00054 * @return 0 if ok, @a EOF otherwise 00055 */ 00056 int sync(); 00057 /** This is a noop. */ 00058 void close() {} 00059 /** Set prefix that will be printed at the start of each output line. 00060 * @param prefix to use. 00061 */ 00062 void prefix(const std::string& prefix) { prefix_ = prefix; } 00063 /** @return prefix that is used on output 00064 * @sa prefix(const std::string&) 00065 */ 00066 const std::string& prefix() const { return prefix_; } 00067 /** @return iostate of underlying stream */ 00068 std::ios::iostate state() const; 00069 private: 00070 /** True if stream was created from a path */ 00071 bool owner_; 00072 /** Each running thread has its own buffer */ 00073 std::map<pthread_t,std::string> buffers_; 00074 std::string prefix_; 00075 std::ostream* stream_; 00076 bool lastnl_; 00077 }; 00078 public: 00079 /** Creates ofstream for path. The stream will be opened with mode 00080 * @a std::ios::app, i.e. a seek to the end of the stream will be performed before 00081 * each output operation. 00082 * @param path on which log output will be written. 00083 * @param prefix each output line will start with the date, the id of the writing thread 00084 * and this prefix string 00085 * @sa Dv::Util::Date 00086 */ 00087 logstream(const std::string& path, const std::string& prefix); 00088 /** Constructor. 00089 * @param stream existing stream on which output will be written. 00090 * @param prefix each output line will start with the date and this prefix string 00091 */ 00092 logstream(std::ostream& stream, const std::string& prefix); 00093 /** Destructor. */ 00094 ~logstream(); 00095 /** @return current prefix that is put at the start of each line. 00096 * @sa Dv::Thread::logstream::logstream 00097 */ 00098 const std::string& prefix() const { return filter_.prefix(); } 00099 /** Set prefix. 00100 * @param s prefix that is put at the start of each line. 00101 * @sa Dv::Thread::logstream::logstream 00102 */ 00103 void prefix(const std::string& s) { filter_.prefix(s); } 00104 private: 00105 LogFilter filter_; 00106 }; 00107 } 00108 } 00109 #endif
dvthread-0.13.4 | [11 December, 2009] |