c++ - 在这个宏定义的消息传递、日志记录、错误处理系统中是否有任何不可预见的副作用?
问题描述
到目前为止,以下源代码已编译并运行,并且其行为符合我的预期。它正在将正确的消息记录到控制台,它正在抛出异常并基于 停止执行MsgTy
,如果标志为真,它正在写入文本文件。如果一个文件不存在,它正在创建一个文件,如果它已经存在,它只是追加到它上面。到目前为止,它似乎正在工作。
我知道我本可以选择其他方法来创建日志记录、错误处理、消息传递系统,但这是一个练习,以刷新我编写可靠宏的技能。我知道它们更难调试,而且我已经尝试过测试所有情况,而且似乎没问题......我只想知道是否有任何我可能遗漏的东西。这不是生产代码,它是针对个人项目的,但我希望它像生产代码一样受到批评。
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <map>
enum MsgTy {
OK = 0,
WARNING,
ERROR,
CRITICAL,
};
static std::map<MsgTy, std::string> msg_id{
{MsgTy::OK, {"OK: "}},
{MsgTy::WARNING, {"WARNING: "}},
{MsgTy::ERROR, {"ERROR: "}},
{MsgTy::CRITICAL, {"CRITICAL: "}}
};
class FileWriter {
std::string filename_;
std::ostringstream msg_;
public:
FileWriter(const std::string& filename, std::ostringstream& msg)
: filename_{ filename } {
operator()(msg);
}
void operator()(std::ostringstream& msg) {
std::ofstream out("log.txt", std::ios::app);
out << msg.str();
}
};
#define messaging(MsgTy, msg, log2file) do { \
std::ostringstream strm; \
if ((MsgTy) == OK) { \
strm << msg_id[(MsgTy)] << (msg) << '\n'; \
std::cout << strm.str(); \
if((log2file) == true) \
FileWriter fw("log.txt", strm); \
} \
if ((MsgTy) == WARNING) { \
strm << msg_id[(MsgTy)] << (msg) << '\n'; \
std::cout << strm.str(); \
if((log2file) == true) \
FileWriter fw("log.txt", strm);\
} \
if ((MsgTy) == ERROR) { \
strm << msg_id[(MsgTy)] << (msg) << '\n'; \
std::cerr << strm.str(); \
if((log2file) == true) \
FileWriter fw("log.txt", strm); \
throw strm.str(); \
} \
if ((MsgTy) == CRITICAL) { \
strm << msg_id[(MsgTy)] << (msg) << '\n'; \
std::cerr << strm.str(); \
if((log2file) == true) \
FileWriter fw("log.txt", strm); \
throw strm.str(); \
} \
} while(0)
int main() {
try {
messaging(MsgTy::OK, "Everything is good!", true);
messaging(MsgTy::WARNING, "Something isn't quite right!", false);
messaging(MsgTy::ERROR, "Something went wrong!", true);
messaging(MsgTy::CRITICAL, "Something horribly went wrong!", true);
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
解决方案
推荐阅读
- python-3.x - 如何使用 pip 自动创建包含所有已安装 python 包的需求文件?
- python - Django ORM 奇怪的行为;模型的数据仅通过“.values()”而不是直接访问来检索
- python - Django 只为测试创建模型
- shell - 运行powershell脚本时如何显示对话框
- node.js - 如何等待谷歌电子表格调用并将其设置在 for 循环中
- json - 为什么 MVC JsonResult 控制器方法在浏览器中返回 HTML 标记
- sql-server - 如何在 SQL Server 2012 中创建一行并汇总值
- python-3.x - 发现样本数量不一致的输入变量:[2, 8382]
- r - 当数据以闪亮的方式更新时如何更新下拉框
- python - 通过多索引在列中搜索值并获取另一列的值