- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想为我的应用程序实现一个记录器,因此我创建了一个派生自 QMessageLogger 的类 MyAppLogger。
假设我在我的 Qt MyApp 项目中实现了其他几个类:
如何在不创建三个记录器的情况下为所有这些类提供记录器?我想使用 Singleton 机制。但是我读到单例是一种糟糕的编程技术!?
Here我刚刚创建了一个带有两个空对话框的简单项目。然后我有一个 MyAppLogger,它将创建格式化的字符串,如:
[MyApp] info 12:12:00:123[ms]_12.12.2012) Object 'MyCalculator'created at 0xdeadbeef
[MyApp] fatal 12:12:00:123[ms]_12.12.2012) Crash at function 'divisionDouble'
当然有所需的正确参数(currentSystemTimeInMillis()、funtionname 等...)
例如 Logger,假设有很多其他类,应该能够使用那些 void info(...)、debug(...)、critical(...)、fatal(...); ETC。 header :
#ifndef MYAPPLOGGER_H
#define MYAPPLOGGER_H
#include <QString>
#include <QFile>
class MyAppLogger
{
public:
MyAppLogger(QString outputFile);
void info(char* expression);
void debug(char* expression);
void critical(char* expression);
void fatal(char* expression);
private:
QFile * _debugFile;
};
#endif // MYAPPLOGGER_H
和对应的SRC:
#include "myapplogger.h"
MyAppLogger::MyAppLogger(QString outputFile)
{
_debugFile = new QFile(outputFile);
}
void MyAppLogger::critical(char *expression)
{
QString line("[MyAPP] \t critical \t SYS_TIME (12:45:00_12.12.2012) :: ");
line.append(expression);
// write line to a file
// write line to STD output
}
最佳答案
据我所知,您想要实现两件事:
更改所有日志输出的输出格式。
您应该考虑使用 qSetMessagePattern为此。
将所有日志输出写入文件:
不需要子类化 QMessageLogger。只需使用 qInstallMessageHandler安装自定义处理程序并将日志消息写入文件。
使用这两种方法的优点是您可以使用 qt 自己的调试宏(qDebug()、qFatal() ...)并且您不需要编写自己的 MessageLogger 或考虑 Singletons。
关于c++ - QMessageLogger : Singleton for a Logger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31474860/
是否有一个意思是“不是单例”的名字? 最佳答案 CaSTLe Windsor 使用术语“ transient ”来描述所有非单例对象。 不过,我个人更喜欢“非单例”一词。 关于singleton -
我有很多(抽象)工厂,它们通常被实现为单例。 通常是为了方便,不必让他们通过那些真正与使用或了解这些工厂无关的层。 大多数时候我只需要在启动时决定其余代码程序的工厂实现,也许通过一些配置 它看起来例如
我有几个关卡都使用相同的音效。我没有在每个级别都使用相同的代码,而是将所有声音合并到一个单例类中。但是,当我从其他类运行该方法时,将其置于单例中不会播放任何声音。我没有错误或警告。 当我在每个类中使用
我应该使用 EJB-@Singleton (javax.ejb.Singleton) 进行统计或监控,还是将统计信息缓存在公共(public) @SessionScoped-Bean 中会更好?为了澄
我有这个单例类: public class Utente { private static readonly Lazy lazy = new Lazy(() => new Utente
我的 iOS 应用程序对 Web 服务做了很多不同的请求。每个请求都是对 ConnectionManager 对象的方法的调用。当响应从 Web 服务到达时,将调用委托(delegate)的方法来通知
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: cracking singleton with other ways 任何人都可以告诉我 Singleton不能用作
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: cracking singleton with other ways 任何人都可以告诉我 Singleton不能用作
做用 @Singleton 注释的类必须遵循 Singleton design pattern ? 我的猜测是他们没有:没有必要有私有(private)构造函数和 static .instance()
我想提供 Java 中可用的内置单例类列表以及使它们成为单例的原因。 对于 E.G. java.lang.Runtime 原因:因为整个 Java 应用程序只能有一个运行时环境。 java.awt.T
我有一个 Python 3 类,它目前是使用 @singleton 装饰器定义的单例,但有时它需要不是是单例。 问题:是否可以在实例化类的对象时传递一个参数,由该参数决定该类是单例还是非单例? 我正试
我正在使用 get_it 包,你有两个选项来注册单例,懒惰和我猜是“常规”(分别是 GetIt.instance.registerLazySingleton 和 GetIt.instance.regi
单例模式在 Spring 容器级别维护实例,而单例设计模式在类加载器级别维护实例。 还有其他区别吗? 其次,我仍然认为上述理由不成立。事实上,一个应用程序上下文/容器仅加载在一个类加载器中。因此从技术
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
在 Guice 中,有什么区别: // Inside your AbstractModule subclass: @Override public void configure() { bin
我使用 pthread TLS 实现了一种“线程本地单例”,我想知道在这种情况下我如何(以及何时)可以删除 pthread_key_t,因为就像现在一样,TLS key 使用的内存永远不会空闲' d。
如何从 @Singleton 组件返回非单例对象? 例如我有: ApplicationComponent.kt @Singleton @Component(modules = [Application
我正在学习 Dagger 2,我注意到在某些示例中,模块方法中有一个 @Singleton,而组件方法中有其他 @Singleton?模块方法和组件方法上的 @Singleton 批注有什么区别,究竟
我的代码中需要一个单例。我读了 Jon Skeet 的 page在单例上并根据他的推荐选择了这个模型(#4): public sealed class Singleton { private
我正在尝试使用 label2rgb 生成 RGB 标签切片并使用它来更新 RGB 体积,如下所示: labelRGB_slice=label2rgb(handles.label(:,:,han
我是一名优秀的程序员,十分优秀!