自制的日志生成器
2026-03-29 15:54:54
发布于:四川
LOG.exe - 竞赛编程日志生成工具
注:本文由 AI 生成 , 但程序为原创,如有疑问,请发在评论区,谢谢!
一、工具概述
在算法竞赛和项目开发过程中,记录编译日志是一项繁琐但必要的工作。本工具旨在自动化生成标准化、格式化的编译日志文档,解决以下痛点:
- 格式不统一:不同时间、不同题目的日志格式参差不齐
- 信息易遗漏:错误数、警告数等关键信息容易忽略
- 重复劳动:每次都需要手动填写日期、开发者等固定信息
本工具通过流式输入处理和模板化输出,实现一键生成的完整工作流。
二、核心功能设计
2.1 功能模块架构
┌─────────────────────────────────────┐
│ 输入处理层 │
│ ├─ 项目元数据(网址、名称) │
│ ├─ 编译器输出流(多行文本) │
│ └─ 便签注释(可选) │
├─────────────────────────────────────┤
│ 解析处理层 │
│ ├─ 正则匹配错误数 / 警告数 │
│ ├─ 编译状态判定(通过/失败/警告) │
│ └─ 时间戳生成 │
├─────────────────────────────────────┤
│ 输出管理层 │
│ └─ 格式化日志构建 │
└─────────────────────────────────────┘
2.2 配置文件系统
开发者: xgy
首次运行时自动默认配置,后续可直接编辑修改,在代码的cout函数中。
三、关键技术实现
3.1 字符串流处理
使用 std::ostringstream 构建日志内容,避免频繁的字符串拼接操作:
相比 std::string 的 operator+=,ostringstream 在多段格式化场景下具有更好的性能和可读性。
3.2 编译信息解析
通过 子串查找算法 提取关键字段:
// 解析错误数量
if (s.find("- Errors: ") != string::npos) {
size_t pos = s.find(":");
errors = stoi(s.substr(pos + 1));
}
设编译器输出总行数为 ,单条规则匹配的时间复杂度为:
其中 为模式串长度(此处为常数级),整体仍为线性复杂度。
四、代码实现
#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
string s[100]={},bq[100]={};
int32_t main() {
int Errors=0,f=0,Warnings=0,cnt=0,cut=0;
string wz,tmmc_xmmc;
std::cout<<"输入格式:题目/项目网址(没有填'无') 题目/项目名称(没有填'无') 编译器编译结果(换行并输入'.end'结束) 便签(没有填'无',换行并输入'.end'结束):\n";
std::cin>>wz>>tmmc_xmmc;
while(getline(cin, s[++cnt])){
if(int(s[cnt].find(".end"))!=-1)
break;
if(int(s[cnt].find("- Errors: "))!=-1){
std::size_t pos = s[cnt].find(":");
std::string str = s[cnt].substr(pos+1);
Errors=stol(str);
}if(int(s[cnt].find("- Warnings: "))!=-1){
std::size_t pos = s[cnt].find(":");
std::string str = s[cnt].substr(pos+1);
Warnings=stol(str);
}
}while(getline(cin, bq[++cut])){
if(int(bq[cut].find(".end"))!=-1)
break;
if(int(bq[cut].find("无"))!=-1)
f=-1;
}
time_t now = time(0);
// 转换为本地时间
tm *localTime = localtime(&now);
// 输出年月日时分秒
std::cout << "\n\n\n\n\n\n\n输出结果:\n## .begin |log| .txt\n# 题目/项目网址: " << wz << "\n# 题目/项目名称: "<< tmmc_xmmc<<" / 编译环境: gcc14 / 开发者: xgy / DATA: "<< 1900 + localTime->tm_year << "年"<< 1 + localTime->tm_mon << "月"<< localTime->tm_mday << "日 \n## .end";
std::cout <<"\n\n## .begin |Tools_Output| .LOG\n编译器编译结果:\n";
for(int i=1;i<cnt;i++){
cout<<s[i]<<'\n';
}//- Compilation passed [YES]
//- Compilation failed [NO]
//- Existence warnings [NO]
if(Errors==0){
cout<<"- Compilation passed [YES]\n- Compilation failed [NO]";
}else{
cout<<"- Compilation passed [NO]\n- Compilation failed [YES]";
}cout<<'\n';
if(Warnings==0){
cout<<"- Existence warnings [NO]";
}else{
cout<<"- Existence warnings [YES]";
}cout<<'\n';
if(f==-1)return cout<<"## .end",0;
else{cout<<"## .end\n\n## .begin |NOTE| .LOG\n";
for(int i=0;i<cut;i++){
cout<<bq[i]<<'\n';
}cout<<"## .end\n";
}
return 0;
}
五、使用示例
5.1 输入样例
无 LOG.exe/LOG.cpp
Compiling single file...
- Filename: C:/Users/Windows10/Desktop/LOG.cpp
- Errors: 0
- Warnings: 2
.end
修复了剪贴板编码问题
.end
5.2 输出结果
## .begin |log| .txt
# 题目/项目网址: 无
# 题目/项目名称: LOG.exe/LOG.cpp / 编译环境: gcc14 / 开发者: xgy / DATA: 2026年3月27日
## .end
## .begin |Tools_Output| .LOG
编译器编译结果:
Compiling single file...
- Filename: C:/Users/Windows10/Desktop/LOG.cpp
- Errors: 0
- Warnings: 2
- Compilation passed [YES]
- Compilation failed [NO]
- Existence warnings [YES]
## .end
## .begin |NOTE| .LOG
修复了剪贴板编码问题
## .end
六、编译与部署
# MinGW编译(静态链接,无需依赖)
g++ LOG.cpp -o LOG.exe -O2 -std=c++20 -fexec-charset=GBK -static -Wl,--stack,12582912
运行环境:Windows 7 及以上,无需额外运行时库。
七、总结与展望
本工具通过流式处理、模板生成和系统API调用,实现了编译日志的自动化管理。核心设计思想:
- 多模态输出:控制台种输出
- 向后兼容:追加模式确保历史数据安全
未来可扩展方向:
- 支持 JSON/XML 格式的结构化日志输出
- 集成 Git 提交信息自动获取
- 添加图形界面(GUI)版本
项目地址:本地工具(单文件可执行程序)
协议:MIT License(自由使用、修改、分发)
全部评论 1
评论区请讨论正经问题,否则会被删除!
2026-03-27 来自 四川
0
















有帮助,赞一个