Можно ли ещё сократить код сохраняя функциональность? #include <iostream> #include…
- User
Можно ли ещё сократить код сохраняя функциональность? #include #include #include #include <windows.h> using namespace std; struct LogType { string keyword; int textColor, bgColor; size_t count = 0; } logs[] = { {"[INFO]", 15, 2}, {"[DEBUG]", 15, 1}, {"[ERROR]", 15, 4} }; void SetColor(int tc, int bc) { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); if (h != INVALID_HANDLE_VALUE) SetConsoleTextAttribute(h, (bc << 4) | (tc & 0x0F)); } void ResetColor() { SetColor(7, 0); } void PrintC(const string& s, int tc, int bc) { SetColor(tc, bc); cout << s; ResetColor(); } int main() { ifstream f("emulator.log"); if (!f) { cerr << "Error opening log file.\n"; system("pause"); return 1; } string line; size_t maxL = 23; while (getline(f, line)) maxL = max(maxL, line.length()); f.clear(); f.seekg(0); while (getline(f, line)) { size_t pos = 0, pl = 0; if (line.size() >= 23 && line[4] == '-' && line[7] == '-' && line[10] == ' ' && line[13] == ':' && line[16] == ':' && line[19] == '.') { PrintC(line.substr(0, 23), 8, 0); pos = 23; pl += 23; } size_t k; while ((k = line.find('[', pos)) != string::npos) { if (k > pos) { PrintC(line.substr(pos, k - pos), 15, 0); pl += (k - pos); } bool found = false; for (auto& log : logs) { if (line.compare(k, log.keyword.size(), log.keyword) == 0) { PrintC(log.keyword, log.textColor, log.bgColor); log.count++; pos = k + log.keyword.size(); pl += log.keyword.size(); found = true; break; } } if (!found) { PrintC("[", 15, 0); pos = k + 1; pl += 1; } } if (pos < line.size()) { PrintC(line.substr(pos), 15, 0); pl += (line.size() - pos); } if (pl < maxL) cout << string(maxL - pl, ' '); cout << '\n'; } f.close(); for (auto& log : logs) { cout << log.keyword.substr(1, log.keyword.size() - 2) << ": "; PrintC(to_string(log.count), log.textColor, log.bgColor); cout << '\n'; }
codesystem("pause"); return 0;}
- Assistant