ロギングクラス内に独自のストリームマニピュレータを実装しようとしています。基本的には、フラグの状態を変更するエンドラインマニピュレータです。ただし、使用しようとすると、次のようになります。
ftypes.cpp:57: error: no match for ‘operator<<’ in ‘log->Log::debug() << log->Log::endl’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:78: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:90: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
..。
コード:
class Log {
public:
...
std::ostream& debug() { return log(logDEBUG); }
std::ostream& endl(std::ostream& out); // manipulator
...
private:
...
std::ofstream m_logstream;
bool m_newLine;
...
}
std::ostream& Log::endl(std::ostream& out)
{
out << std::endl;
m_newLine = true;
return out;
}
std::ostream& Log::log(const TLogLevel level)
{
if (level > m_logLevel) return m_nullstream;
if (m_newLine)
{
m_logstream << timestamp() << "|" << logLevelString(level) << "|";
m_newLine = false;
}
return m_logstream;
}
呼び出そうとするとエラーが発生します:
log->debug() << "START - object created" << log->endl;
(logはLogオブジェクトへのポインタです)
何か案は?マニピュレーターが実際にクラス内にあるという事実に何らかの形で関係しているのではないかと思いますが、それは私の大げさな推測です...
乾杯、
トム
編集:フォーマットを制限するため、コメントの代わりにこれをここに置きます。私は自分のstreambufを実装しようとしましたが、1つの例外を除いてうまく機能します。追加のためにfilebufを開こうとすると、失敗します。出力はうまく機能しますが、何らかの理由で追加するだけでは機能しません。追加でofstreamを直接使用しようとすると、機能します。なぜですか?–作品:
std::ofstream test;
test.open("somefile", std::ios_base::app);
if (!test) throw LogIoEx("Cannon open file for logging");
test << "test" << std::endl;
「テスト」を正しく追加します。
動作しません:
std::filebuf *fbuf = new std::filebuf();
if (!fbuf->open("somefile", std::ios_base::app)) throw LogIoEx("Cannon open file for logging");
例外をスローします。openmodeをoutに設定すると、機能します。
乾杯