00001
00002
00003
00004
00005 #ifndef WIN32
00006
00007 #include "Xostream.h"
00008
00009
00010 #include <errno.h>
00011 #include <strstream>
00012 #include <iostream>
00013 #include <cstring>
00014 #include <cstdlib>
00015
00016 #include <cstdio>
00017
00019
00020 class logbuf : public std::streambuf
00021 {
00022
00023 public:
00024 enum { SIZE = 32 };
00025
00026 logbuf (void);
00027 ~logbuf (void);
00028
00029 #if !defined(__GNUG__)
00030 virtual int_type overflow (int_type c );
00031 virtual std::streamsize xsputn (const char_type *string,
00032 std::streamsize length);
00033 #else
00034 virtual int overflow (int c );
00035 virtual int xsputn (const char *string, int length);
00036 #endif
00037 private:
00038 int m_pos;
00039 #if !defined(__GNUG__)
00040 char_type m_buffer [SIZE];
00041 #else
00042 char m_buffer [SIZE];
00043 #endif
00044 };
00045
00047
00049
00050 Xostream::Xostream(const char* )
00051 : std::ostream(new logbuf)
00052 {
00053 }
00054
00055
00056 void Xostream::flush()
00057 {
00058 std::ostream::flush();
00059
00060 }
00061
00062
00063
00064 void Xostream::clear()
00065 {
00066
00067 flush();
00068
00069 }
00071
00073
00074
00075 logbuf::logbuf ()
00076 : m_pos (0)
00077 {
00078 setp (0, 0);
00079 setg (0, 0, 0);
00080 }
00081
00082
00083 logbuf::~logbuf (void)
00084 {
00085 #if !defined(__GNUG__)
00086 overflow (traits_type::eof ());
00087 #else
00088 overflow (EOF);
00089 #endif
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 #if !defined(__GNUG__)
00106 std::streambuf::int_type
00107 logbuf::overflow (int_type ch)
00108 #else
00109 int
00110 logbuf::overflow (int ch)
00111 #endif
00112 {
00113 if (
00114 #if !defined(__GNUG__)
00115 ch != traits_type::eof ()
00116 #else
00117 ch != EOF
00118 #endif
00119 )
00120 {
00121 m_buffer [m_pos++] = (char) ch;
00122
00123
00124
00125
00126 if (ch == '\n' || m_pos == SIZE)
00127 {
00128 std::cout.write (m_buffer, m_pos);
00129 m_pos = 0;
00130 }
00131 }
00132 else if (m_pos)
00133 {
00134 std::cout.write (m_buffer, m_pos);
00135 m_pos = 0;
00136 }
00137
00138 return 0;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 #if !defined(__GNUG__)
00154 std::streamsize
00155 logbuf::xsputn (const char_type *string, std::streamsize length)
00156 #else
00157 int
00158 logbuf::xsputn (const char *string, int length)
00159 #endif
00160 {
00161 for (int start = 0, end = 0; start < length; start = end) {
00162
00163 while (end < length && string [end] != '\n')
00164 ++end;
00165
00166 if (end < length)
00167
00168 ++end;
00169
00170
00171
00172
00173
00174
00175
00176 if (m_pos) {
00177
00178 while (m_pos < SIZE && start < end)
00179 m_buffer [m_pos++] = string [start++];
00180 if (m_pos == SIZE) {
00181 std::cout.write (m_buffer, m_pos);
00182 m_pos = 0;
00183 }
00184 }
00185 if (end - start < SIZE - m_pos)
00186
00187
00188
00189
00190 while (start < end) m_buffer [m_pos++] = string [start++];
00191 else if (start < end)
00192
00193
00194
00195 std::cout.write (string + start, end - start);
00196 }
00197 return length;
00198 }
00199 #endif