我写了这个接口:这个方案有错误记录有什么问题吗?
//The idea is to use EHPrint to construct the error msg and then raise an error or
//a warning, etc. This removes the need to implement all 3 'Raise' functions
//as taking variable param list reducing code.
class ErrorHandler
{
public:
virtual void RaiseError(bool, int) = 0;
virtual void RaiseWarning(int) = 0;
virtual void RaiseMsg() = 0;
__attribute__((format(printf, 1, 2)))
virtual const char* EHPrint(const char*, ...) = 0;
virtual ~ErrorHandler(){}
};
忘记多线程问题,因为一个对象将只在一个线程中使用。实现类可以记录日志,向用户提出错误或根据需要终止。缺点是你必须先打电话给EHPrint
,然后才能提出错误等等。我可以通过任何方式使这更美丽?
编辑: 当我想要RaiseError
时,我真的想调用一个函数。截至目前,我必须致电EHPrintf
,然后致电RaiseError
。
EDIT2: 我认为这是我的错误,我把错误处理和错误记录结合在一起。将尝试和分开他们,看看。
EDIT3: 我想我现在应该发布记录:
class Logger
{
public:
enum LogType
{
LT_DEBUG = 0,
LT_WARNING,
LT_ERROR,
LT_STAT,
LT_TEXT
};
__attribute__((format(printf, 5, 6)))
virtual const char* EHLog(LogType,
int,
const char*,
int,
const char*,
...) = 0;
virtual ~Logger(){}
};
因为日志现在是分开从而清除了代码。
查看现有的开源日志库(log4cxx,Pantheios)以了解一个想法吗? – DumbCoder 2010-10-15 09:16:42
我想回答这个特定的问题。那些库是巨大的,我不想通过他们的atm。 :) – nakiya 2010-10-15 10:06:12
不需要通过代码。至少浏览头文件以获得各种想法! – DumbCoder 2010-10-15 10:16:46