LOG.exe竞赛编程日志生成工具第2版
2026-04-12 16:07:57
发布于:四川
LOG.exe - 竞赛编程日志生成工具(第2版)
注:本文由 AI 生成 , 但程序为原创,如有疑问,请发在评论区,谢谢!
-
本次改版阅读资料300余篇,点一个赞吧!
一、工具概述
在算法竞赛和项目开发过程中,记录编译日志是一项繁琐但必要的工作。本工具旨在自动化生成标准化、格式化的编译日志文档,解决以下痛点:
- 格式不统一:不同时间、不同题目的日志格式参差不齐
- 信息易遗漏:错误数、警告数等关键信息容易忽略
- 重复劳动:每次都需要手动填写日期、开发者等固定信息
- 手动复制:每次都需要手动复制日志
本工具通过流式输入处理、自动粘贴至剪切板和模板化输出,实现一键生成的完整工作流。
二、核心功能设计
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 <vector>
#include <string>
#include <sstream>
#include <windows.h>
using namespace std;
// 设置控制台为GBK编码(中文Windows默认)
void InitConsole() {
SetConsoleOutputCP(936); // GBK编码
SetConsoleCP(936);
}
// 设置剪贴板文本(GBK版本)
void SetClipboardText(const string& text) {
if (!OpenClipboard(nullptr)) return;
EmptyClipboard();
int len = text.length() + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
if (hMem) {
char* str = static_cast<char*>(GlobalLock(hMem));
memcpy(str, text.c_str(), len);
GlobalUnlock(hMem);
SetClipboardData(CF_TEXT, hMem);
}
CloseClipboard();
}
int main() {
InitConsole(); // 初始化控制台编码为GBK
vector<string> compilerLines;
vector<string> noteLines;
string line;
string url, projectName;
cout << "输入格式:\n";
cout << "1. 题目/项目网址(没有填'无') 题目/项目名称(没有填'无')\n";
cout << "2. 编译器编译结果(换行并输入'.end'结束)\n";
cout << "3. 便签(没有填'无',换行并输入'.end'结束):\n\n";
cin >> url >> projectName;
cin.ignore();
// 读取编译器输出
while (getline(cin, line)) {
if (line.find(".end") != string::npos) break;
compilerLines.push_back(line);
}
// 读取便签
while (getline(cin, line)) {
if (line.find(".end") != string::npos) break;
noteLines.push_back(line);
}
// 解析错误和警告
int errors = 0, warnings = 0;
for (const auto& s : compilerLines) {
if (s.find("- Errors: ") != string::npos) {
size_t pos = s.find(":");
errors = stoi(s.substr(pos + 1));
}
if (s.find("- Warnings: ") != string::npos) {
size_t pos = s.find(":");
warnings = stoi(s.substr(pos + 1));
}
}
bool hasNotes = !(noteLines.size() == 1 && noteLines[0] == "无");
// 构建日志
time_t now = time(0);
tm* localTime = localtime(&now);
ostringstream log;
log << "## .begin |log| .txt\n";
log << "# 题目/项目网址: " << url << "\n";
log << "# 题目/项目名称: " << projectName
<< " / 编译环境: gcc14 / 开发者: xgy / DATA: "
<< 1900 + localTime->tm_year << "年"
<< 1 + localTime->tm_mon << "月"
<< localTime->tm_mday << "日\n";
log << "## .end\n\n";
log << "## .begin |Tools_Output| .LOG\n";
log << "编译器编译结果:\n";
for (const auto& s : compilerLines) {
log << s << "\n";
}
log << "- Compilation passed [" << (errors == 0 ? "YES" : "NO") << "]\n";
log << "- Compilation failed [" << (errors == 0 ? "NO" : "YES") << "]\n";
log << "- Existence warnings [" << (warnings == 0 ? "NO" : "YES") << "]\n";
if (!hasNotes) {
log << "## .end\n";
} else {
log << "## .end\n\n";
log << "## .begin |NOTE| .LOG\n";
for (const auto& s : noteLines) {
log << s << "\n";
}
log << "## .end\n";
}
string finalLog = log.str();
// 输出到控制台
cout << "\n\n\n\n\n\n\n输出结果:\n" << finalLog;
// 复制到剪贴板
SetClipboardText(finalLog);
cout << "\n[日志已复制到剪贴板]\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(自由使用、修改、分发)
感谢观看!
注:自制工具不易拿走点个赞吧!
这里空空如也



















有帮助,赞一个