>_ DashByte
Modern C++20 STL ⏱ 60 Minutes Zero External Libs

Build a Fast CLI Log & Word Analyzer

Compile a native command-line utility that reads any text or server log file, counts lines, words, and byte size via std::filesystem, ranks the top 5 most frequent tokens, and benchmarks its own execution time in microseconds.

Project Folder Structure

cpp-log-analyzer/
├── main.cpp          # C++20 analyzer + timer
└── CMakeLists.txt    # Cross-platform build config
Quick Compile: g++ -std=c++20 -O2 main.cpp -o analyzer

60-Minute Build Checklist

0 / 5 Done

Check off each milestone as you build. Your progress saves automatically in your browser.

main.cpp — C++20 CLI Analyzer
#include <algorithm>
#include <cctype>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

namespace fs = std::filesystem;

std::string normalize_token(const std::string& raw) {
    std::string clean;
    clean.reserve(raw.size());
    for (unsigned char ch : raw) {
        if (std::isalnum(ch)) {
            clean.push_back(static_cast<char>(std::tolower(ch)));
        }
    }
    return clean;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <file_path>\n";
        return 1;
    }

    const fs::path file_path = argv[1];
    if (!fs::exists(file_path) || !fs::is_regular_file(file_path)) {
        std::cerr << "Error: File not found -> " << file_path << "\n";
        return 1;
    }

    const auto start_time = std::chrono::high_resolution_clock::now();

    std::ifstream input(file_path);
    if (!input) {
        std::cerr << "Error: Unable to open file.\n";
        return 1;
    }

    std::size_t line_count = 0;
    std::size_t word_count = 0;
    std::unordered_map<std::string, std::size_t> freq;

    std::string line;
    while (std::getline(input, line)) {
        ++line_count;
        std::istringstream stream(line);
        std::string raw_word;
        while (stream >> raw_word) {
            std::string token = normalize_token(raw_word);
            if (!token.empty()) {
                ++word_count;
                ++freq[token];
            }
        }
    }

    std::vector<std::pair<std::string, std::size_t>> ranked(freq.begin(), freq.end());
    const std::size_t top_n = std::min<std::size_t>(5, ranked.size());

    std::partial_sort(
        ranked.begin(),
        ranked.begin() + top_n,
        ranked.end(),
        [](const auto& a, const auto& b) { return a.second > b.second; }
    );

    const auto end_time = std::chrono::high_resolution_clock::now();
    const auto elapsed_us = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();

    std::cout << "=== DevBytes C++20 File Report ===\n";
    std::cout << "File:   " << file_path.filename().string() << "\n";
    std::cout << "Size:   " << fs::file_size(file_path) << " bytes\n";
    std::cout << "Lines:  " << line_count << "\n";
    std::cout << "Words:  " << word_count << "\n";
    std::cout << "Unique: " << freq.size() << "\n\n";

    std::cout << "Top " << top_n << " Tokens:\n";
    for (std::size_t i = 0; i < top_n; ++i) {
        std::cout << "  " << (i + 1) << ". " << ranked[i].first
                  << " (" << ranked[i].second << ")\n";
    }

    std::cout << "\nCompleted in " << elapsed_us << " us\n";
    return 0;
}
CMakeLists.txt — Cross-Platform Build File
cmake_minimum_required(VERSION 3.20)
project(cpp_log_analyzer LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(analyzer main.cpp)

if(MSVC)
  target_compile_options(analyzer PRIVATE /W4 /O2)
else()
  target_compile_options(analyzer PRIVATE -Wall -Wextra -Wpedantic -O2)
endif()