在对象使用前调用成员函数
几天前,我是询问操作员对我的Logger项目的负载。现在,我还有另一个无法解决的问题 - 可能是由于我的经验低。
首先 - 我的logger
对象(被设计为单身对象)应写入与源代码同一目录中创建的文件。所需的用途看起来像这样:
logger << "log message text";
因此,我创建了一个Singleton对象logger
,但是我需要在第一个logger
使用之前进行一些控件,例如,如果已经存在文件,则它是空的等。所有提供我已经创建的成员函数或我会的。
有什么意义?我需要以某种方式检查logger
的第一次使用之前,请检查所有这些内容。我希望自动制作它们,因此可能的用户不必手动执行此操作。
因此,让我们总结一下...假设我想第一次使用它,所以我将此行放到我的代码中:
logger << "Log something";
在将日志本身制作之前,我需要logger
class class检查是否存在
- 日志文件已经存在
- 如果不,创建一个
- 如果是的,请找到结束并继续在下一行。
A few days ago, I was asking about operator overloading to my Logger project. Now I have another problem which I'm not able to solve - probably due to my low experience.
First - my Logger
object (which is designed as a Singleton object) should write to a file created in the same directory as the source code. Desired use looks this way:
logger << "log message text";
So I have created a singleton object logger
, but I need to make some controls before the first logger
use, e.g. if the file already exists, if it is empty, etc. All the member functions which provide that I've already created or I will.
What's the point? I need somehow to check all these things before the first and every use of the logger
. And I want them to be made automatically, so the possible user won't have to do that manually.
So, let´s summarize... let's say I want to use it for the first time, so I put this line to my code:
logger << "Log something";
Before it will make the log itself, I need the Logger
class to check if:
- the log file already exists
- if no, create one
- if yes, find the end and continue on the next line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
logger
在哪里打开文件开始?您为什么不在打开文件的点上进行这些检查?例如,这听起来像是您应该在
Logger
的构造函数中处理的东西。并且,与其定义
static
方法,不要定义logger
类,而不是定义全局logger
对象,例如:logger
构造器直到第一次getLogger()
才能运行。Where does your
Logger
open the file to begin with? Why aren't you doing these checks at the point where the file is being opened?This sounds like something you should be handling in your
Logger
's constructor, for instance.And, rather than defining a global
logger
object, consider defining astatic
method in yourLogger
class instead, eg:Then the
Logger
constructor won't run until the first timeGetLogger()
is called.