peary/log/logging.hpp

Peary Log. More…

Namespaces

Name
peary
peary::log

Classes

Name
class peary::log::Log Log of the framework to inform the user of process.

Detailed Description

Peary Log.

Copyright: Copyright (c) 2016-2025 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only

Source code

  
#pragma once

#include <cstring>
#include <mutex>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>

namespace peary::log {
    enum class LogLevel {
        FATAL = 0, 
        STATUS,    
        ERROR,     
        WARNING,   
        INFO,      
        DEBUG,     
        NONE,      
        TRACE,     
    };
    enum class LogFormat {
        SHORT = 0, 
        DEFAULT,   
        LONG       
    };

    class Log {
    public:
        Log();
        ~Log();


        Log(const Log&) = delete;
        Log& operator=(const Log&) = delete;


        Log(Log&&) noexcept(false) = default;
        Log& operator=(Log&&) noexcept(false) = default;

        std::ostringstream& getStream(LogLevel level = LogLevel::INFO,
                                      const std::string& file = "",
                                      const std::string& function = "",
                                      uint32_t line = 0,
                                      const std::string& name = "");

        std::ostringstream& getProcessStream(std::string identifier,
                                             LogLevel level = LogLevel::INFO,
                                             const std::string& file = "",
                                             const std::string& function = "",
                                             uint32_t line = 0,
                                             const std::string& name = "");

        static void finish();

        static LogLevel getReportingLevel();
        static void setReportingLevel(LogLevel level);

        static LogLevel getLevelFromString(const std::string& level);
        static std::string getStringFromLevel(LogLevel level);

        static LogFormat getFormat();
        static void setFormat(LogFormat format);

        static LogFormat getFormatFromString(const std::string& format);
        static std::string getStringFromFormat(LogFormat format);

        static void addStream(std::ostream& stream);
        static void clearStreams();
        static const std::vector<std::ostream*>& getStreams();

        static void setSection(std::string header);
        static std::string getSection();

        static std::string getTimestamp();

    private:
        static std::string get_current_date();

        static bool is_terminal(std::ostream& stream);

        // Output stream
        std::ostringstream os;

        // Number of exceptions to prevent abort
        int exception_count_ {};
        // Saved value of the length of the header indent
        unsigned int indent_count_ {};

        // Internal methods to store static values
        static std::string& get_section();
        static LogLevel& get_reporting_level();
        static LogFormat& get_format();
        static std::vector<std::ostream*>& get_streams();

        // Name of the process to log or empty if a normal log message
        std::string identifier_ {};
        static std::string last_message_;
        static std::string last_identifier_;

        static std::mutex write_mutex_;
    };

    // suppress a (logging) stream
    // TODO [doc] rewrite as a lowercase function in a namespace?
    inline void SUPPRESS_STREAM(std::ostream& stream) {
        stream.setstate(std::ios::failbit);
    }

    // TODO [doc] rewrite as a lowercase function in a namespace?
    inline void RELEASE_STREAM(std::ostream& stream) {
        stream.clear();
    }
} // namespace peary::log
  

Updated on 2025-11-14 at 11:31:23 +0100