可以记录它正在做什么的类...有时候 [英] Class that can log what it's doing...sometimes

查看:55
本文介绍了可以记录它正在做什么的类...有时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个班级,我希望能够输出详细信息

关于它正在做什么

到文件或屏幕。我首先尝试的是在类中添加一个

std :: ostream-reference成员变量,然后添加一个构造函数,除了normal之外

参数,也拿了一个std :: ostream&。

我打算设置一个名为logging的布尔成员变量为true,

有另一个成员函数

检查该变量以确定它们是否应该执行日志记录。

类似于:


class foo

{

public:

foo(args); / * logging设置为false,m_out设置为空* /

foo(args,std :: ostream& out); / * logging设置为true,m_out设置为out * /


void bar(); / * log to m_out if logging == true * /


private:

std :: ostream& m_out;

bool伐木;

};


我想到如果我在会员功能中忘记检查会发生什么

记录的值并尝试使用m_out
而不引用某些内容。但是,我没有得到那个

远,因为编译器停止了我说我必须初始化m_out(我在第一次做的时候没做过) />
构造函数)。

我明白为什么......没有像NULL引用这样的东西,它必须是

a引用一个有效的对象

并且它可能永远不会被设置为引用别的东西。

所以我想第二个构造函数接受一个const

std :: string& ;相反,该字符串将

持有一个文件名,然后我会打开一个具有该名称的文件并登录到它。

但是那个变种不是很好/>
令人满意,因为那时用户将无法直接登录到cout或

cerr ...我该怎么办?我可以通过使用预处理器来解决

...#ifndef LOGGING,那可能是

产生更快的代码时间

我不希望登录开启,因为我从来不需要检查任何

标志或其他东西(并且产生

a较小的程序),但它不是很灵活。 br />

/ Eric

Hello, I have a class that I want to be able to output detailed information
about what it''s doing either
to a file or to the screen. What I first tried was adding a
std::ostream-reference member variable to
the class and then adding a constructor that, apart from the "normal"
arguments, also took an std::ostream&.
I then intended to set a boolean member variable called logging to true and
have the other member functions
check that variable to determine whether they should perform logging or not.
Something like:

class foo
{
public:
foo(args); /* logging set to false, m_out set to nothing */
foo(args, std::ostream& out); /* logging set to true, m_out set to out */

void bar(); /* log to m_out if logging == true */

private:
std::ostream& m_out;
bool logging;
};

I thought about what would happen if I in a member function forgot to check
the value of logging and tried
to use m_out without it referencing something. However, I didn''t get that
far because the compiler stopped
me saying that I must initialize m_out (which I didn''t do in the first
constructor).
I understand why...there is no such thing as a NULL reference, it has to be
a reference to a valid object
and it may never be set to reference something else.
So I was thinking having the second constructor accepting a const
std::string& instead and that string would
hold a file name and then I would open a file with that name and log to it.
But that variant isn''t very
satisfying because then the user wont be able to log directly to cout or
cerr...how do I proceed? I could solve
it by using the preprocessor...#ifndef LOGGING, and that would probably
yield faster code for the times
when I dont want logging turned on because I never would have to check any
flags or something (and yield
a smaller program), but it''s not very flexible.

/ Eric

推荐答案

Eric Lila写道:
Eric Lila wrote:
你好,我有一个我希望能够输出详细信息的课程,关于它对文件或屏幕的作用。我首先尝试的是在类中添加一个
std :: ostream-reference成员变量,然后添加一个构造函数,除了普通
参数之外,还使用了std :: ostream&。
然后我打算设置一个名为logging的布尔成员变量为true,并且
有其他成员函数
检查该变量以确定它们是否应该执行日志记录。类似于:

class foo
{
公开:
foo(args); / * logging设置为false,m_out设置为空* /
foo(args,std :: ostream& out); / * logging设置为true,m_out设置为out * /

void bar(); / * log to m_out if logging == true * /

private:
std :: ostream& m_out;
bool logging;
};

我想过如果我在一个成员函数中忘记检查日志记录的价值会怎么样? />使用m_out而不引用某些东西。但是,我没有得到那个
,因为编译器停止说我必须初始化m_out(我在第一个
构造函数中没做过)。
我理解为什么......没有NULL引用这样的东西,它必须是对有效对象的引用
并且它可能永远不会被设置为引用别的东西。
所以我想第二个构造函数接受一个const
std :: string&相反,该字符串将
保存一个文件名,然后我将打开一个具有该名称的文件并登录到它。
但该变体并不是很令人满意,因为那时用户不会能够直接登录cout或
cerr ...我该怎么办?我可以通过使用预处理器来解决它...#ifndef LOGGING,这可能会产生更快的代码时间
当我不想打开日志时因为我从来没有必要检查任何
标志或东西(并产生一个较小的程序),但它不是很灵活。
Hello, I have a class that I want to be able to output detailed information
about what it''s doing either
to a file or to the screen. What I first tried was adding a
std::ostream-reference member variable to
the class and then adding a constructor that, apart from the "normal"
arguments, also took an std::ostream&.
I then intended to set a boolean member variable called logging to true and
have the other member functions
check that variable to determine whether they should perform logging or not.
Something like:

class foo
{
public:
foo(args); /* logging set to false, m_out set to nothing */
foo(args, std::ostream& out); /* logging set to true, m_out set to out */

void bar(); /* log to m_out if logging == true */

private:
std::ostream& m_out;
bool logging;
};

I thought about what would happen if I in a member function forgot to check
the value of logging and tried
to use m_out without it referencing something. However, I didn''t get that
far because the compiler stopped
me saying that I must initialize m_out (which I didn''t do in the first
constructor).
I understand why...there is no such thing as a NULL reference, it has to be
a reference to a valid object
and it may never be set to reference something else.
So I was thinking having the second constructor accepting a const
std::string& instead and that string would
hold a file name and then I would open a file with that name and log to it.
But that variant isn''t very
satisfying because then the user wont be able to log directly to cout or
cerr...how do I proceed? I could solve
it by using the preprocessor...#ifndef LOGGING, and that would probably
yield faster code for the times
when I dont want logging turned on because I never would have to check any
flags or something (and yield
a smaller program), but it''s not very flexible.




我会给' 'out''一个默认值,比如说,你的''foo''类的静态成员是

,并且本质上是一个/ dev / null类的流:
< br $>
class foo {

foo(args,std :: ostream& s = foo :: nullstream);

static std :: ostream& nullstream;

};


std :: ostream& foo :: nullstream = ??? ;


你必须找到一个体面的nullstream实现(我是
不是很好用流来给你一个单行代码) 。所有你需要的是一个流,其缓冲区只会吃掉所有东西而什么都不做。我打赌

那里有实现。


V



I would give ''out'' a default value, which is, say, a static member of
your ''foo'' class, and is essentially a /dev/null kind of stream:

class foo {
foo(args, std::ostream &s = foo::nullstream);
static std::ostream& nullstream;
};

std::ostream& foo::nullstream = ??? ;

You would have to find a decent implementation of the nullstream (I am
not that good with streams to give you a one-liner for it). All you
need is a stream whose buffer would just eat all and do nothing. I bet
there are implementations of it out there.

V




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:Kg ***************** @ newsread1.dllstx09.us.to。 verio.net ...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Kg*****************@newsread1.dllstx09.us.to. verio.net...
Eric Lila写道:
Eric Lila wrote:
您好,我有一个班级,我希望能够输出详细的信息,关于它的内容
对文件或屏幕。我首先尝试的是在类中添加一个
std :: ostream-reference成员变量,然后添加一个构造函数,除了普通
参数之外,还使用了std :: ostream&。
然后我打算设置一个名为logging的布尔成员变量为true
并让其他成员函数检查该变量以确定它们是否应该执行日志记录或
不是。类似于:

class foo
{
公开:
foo(args); / * logging设置为false,m_out设置为空* /
foo(args,std :: ostream& out); / * logging设置为true,m_out设置为out
* /

void bar(); / * log to m_out if logging == true * /

private:
std :: ostream& m_out;
bool logging;
};

我想过如果我在一个成员函数中忘记检查日志记录的价值会怎么样? />使用m_out而不引用某些东西。但是,我没有得到那个
,因为编译器停止说我必须初始化m_out(我在第一个
构造函数中没做过)。
我理解为什么......没有NULL引用这样的东西,它必须是对有效对象的引用
并且它可能永远不会被设置为引用别的东西。
所以我想第二个构造函数接受一个const
std :: string&相反,该字符串将
保存文件名,然后我将打开一个具有该名称的文件并登录
它。但是那种变体并不是很令人满意,因为那时用户将无法直接登录到cout或
cerr ...我该怎么办?我可以通过使用预处理器来解决它...#ifndef LOGGING,这可能会产生更快的代码时间
当我不想打开日志时因为我从来没有必要检查
任何标志或东西(并产生一个较小的程序),但它不是很灵活。
Hello, I have a class that I want to be able to output detailed
information about what it''s doing either
to a file or to the screen. What I first tried was adding a
std::ostream-reference member variable to
the class and then adding a constructor that, apart from the "normal"
arguments, also took an std::ostream&.
I then intended to set a boolean member variable called logging to true
and have the other member functions
check that variable to determine whether they should perform logging or
not. Something like:

class foo
{
public:
foo(args); /* logging set to false, m_out set to nothing */
foo(args, std::ostream& out); /* logging set to true, m_out set to out
*/

void bar(); /* log to m_out if logging == true */

private:
std::ostream& m_out;
bool logging;
};

I thought about what would happen if I in a member function forgot to
check the value of logging and tried
to use m_out without it referencing something. However, I didn''t get that
far because the compiler stopped
me saying that I must initialize m_out (which I didn''t do in the first
constructor).
I understand why...there is no such thing as a NULL reference, it has to
be a reference to a valid object
and it may never be set to reference something else.
So I was thinking having the second constructor accepting a const
std::string& instead and that string would
hold a file name and then I would open a file with that name and log to
it. But that variant isn''t very
satisfying because then the user wont be able to log directly to cout or
cerr...how do I proceed? I could solve
it by using the preprocessor...#ifndef LOGGING, and that would probably
yield faster code for the times
when I dont want logging turned on because I never would have to check
any flags or something (and yield
a smaller program), but it''s not very flexible.



我会''out''一个默认值,比如说,你的''foo''类的静态成员,并且本质上是一个/ dev / null类的流:

class foo {
foo(args,std :: ostream& s = foo :: nullstream);
static std :: ostream& nullstream;
};

std :: ostream& foo :: nullstream = ??? ;

你必须找到一个体面的nullstream实现(我不是那么好用流为你提供一个单行)。你需要的只是一个流,其缓冲区只会吃掉所有东西而什么都不做。我打赌
有它的实现。

V



I would give ''out'' a default value, which is, say, a static member of
your ''foo'' class, and is essentially a /dev/null kind of stream:

class foo {
foo(args, std::ostream &s = foo::nullstream);
static std::ostream& nullstream;
};

std::ostream& foo::nullstream = ??? ;

You would have to find a decent implementation of the nullstream (I am
not that good with streams to give you a one-liner for it). All you
need is a stream whose buffer would just eat all and do nothing. I bet
there are implementations of it out there.

V




这似乎是一个很好的解决方案!我现在正在谷歌上搜索我能找到的b $ b,但是没有运气。很多点击,只是没找到我正在寻找

for。


谢谢Victor


/ Eric



That seems to be a nice solution! I am googling right now to see what I
can find, no luck yet though. Lots of hits, just not found what I''m looking
for yet.

Thanks Victor

/ Eric


Eric Lilja写道:
Eric Lilja wrote:
private:
std :: ostream& m_out;
bool logging;
};
private:
std::ostream& m_out;
bool logging;
};



你的问题的一个选择是添加一个空输出

device。


class NullStreamBuf:public streambuf {

int_type overflow(int_type c){return c; }


};


class NullStream:public ostream

public:

NullStream():ostream(new NullStreamBuf){}

~NullStream(){delete rdbuf(); }


};


然后您可以输出到您心中的内容

和数据刚刚被丢弃。


One option to your problem would be to add a null output
device.

class NullStreamBuf : public streambuf {
int_type overflow(int_type c) { return c; }

} ;

class NullStream : public ostream
public:
NullStream() : ostream(new NullStreamBuf) { }
~NullStream() { delete rdbuf(); }

};

You can then output to the stream to your heart''s content
and the data is just discarded.


这篇关于可以记录它正在做什么的类...有时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆