save all output in log file c cpp

#include <iostream>
#include <fstream>

int main() {
    // Redirect standard output to a log file
    std::ofstream logFile("output_log.txt");
    std::streambuf* originalCoutBuffer = std::cout.rdbuf();
    std::cout.rdbuf(logFile.rdbuf());

    // Your C++ code starts here
    std::cout << "Step 1: Opening the log file and redirecting standard output." << std::endl;

    // Sample code
    int a = 5, b = 10;
    std::cout << "Step 2: Performing some operations. a = " << a << ", b = " << b << std::endl;
    int result = a + b;

    // Output the result
    std::cout << "Step 3: Output the result. a + b = " << result << std::endl;

    // Restore standard output
    std::cout.rdbuf(originalCoutBuffer);
    logFile.close();

    // End of your C++ code

    return 0;
}