C++ iostream clog - Log Error Message

Updated on 15 May, 2025
clog header image

Introduction

The clog object (also written as std::clog) in C++ is a part of the iostream library. It serves as a buffered output stream for logging errors or informational messages. std::clog is similar to cout, but is intended for diagnostics and debugging, allowing developers to separate runtime information from main output. Since it is buffered, its messages may not appear immediately unless explicitly flushed.

In this article, you will learn how to employ the C++ clog to log error messages. Explore practical examples on how to use clog effectively in your C++ applications for both simple logging and more complex error handling situations.

Basic Usage of std::clog in C++

Log a Basic Message

  1. Simply include the iostream library.

  2. Use clog to output an error or informational message.

    cpp
    #include <iostream>
    
    int main() {
        std::clog << "This is a log message indicating the start of the program." << std::endl;
        return 0;
    }
    

    This code will log the message to the standard error output, which is useful for providing information about the program's state without interfering with the main output stream.

Handling Errors with clog in C++

Report an Error Condition

  1. Employ clog for communicating errors encountered during program execution.

  2. Combine conditions to check for errors and log them accordingly.

    cpp
    #include <iostream>
    
    void checkFileOperation() {
        std::clog << "Checking file operation." << std::endl;
        // Assume readFile is a function that returns false if the file read operation fails
        bool success = readFile("example.txt");
        if (!success) {
            std::clog << "Error: Failed to read file 'example.txt'." << std::endl;
        }
    }
    
    int main() {
        checkFileOperation();
        return 0;
    }
    

    In this example, clog is used to log both the start of an operation and any error that occurs. This helps in debugging and maintaining clean output logs.

Advanced Techniques with clog in C++

Redirect clog to a File

  1. Redirect clog stream to output logs to a file instead of the console.

  2. Ensure you handle the file stream appropriately.

    cpp
    #include <iostream>
    #include <fstream>
    
    int main() {
        std::ofstream logFile("log.txt");
        std::clog.rdbuf(logFile.rdbuf());
    
        std::clog << "Logging this error to a file instead of console." << std::endl;
    
        // Remember to flush or close the file when done
        logFile.close();
        return 0;
    }
    

    By rerouting clog to a file, you can maintain a persistent log that can be analyzed post execution, which is extremely useful for long-running applications or those running in production environments.

Conclusion

The clog object in C++ is a powerful logging tool used to output error messages and runtime information separately from the program’s standard output. As part of the iostream library, std::clog is particularly effective for debugging, monitoring application performance, and understanding program behavior under various conditions. Whether writing logs to the console or redirecting them to a .clog file, using clog in C++ helps improve code clarity, maintainability, and debugging efficiency.

Comments

No comments yet.