mardi 1 octobre 2019

I am trying to make a logging framework in c++ but information is not being passed to the logger's subcomponents, what am i doing wrong?

here is my code, when i try running this, main does output the information placed into the LoggerComponent, but not the Logger itself. I don't know why, what could be preventing the logger from passing information into the underlying loggercomponent?

i tried using information from http://www.cplusplus.com/reference/map/map/ and from https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/

logger.cpp:

#include "logger.hpp"

Logger::Logger(bool verbose, bool fileoutput)
{
    if(verbose)
    {
        LoggerComponent c1(LoggerLevel::DEBUG, &std::cout);
        addLogger (LoggerType::CONSOLE, &c1);
        c1.output (LoggerLevel::DEBUG, "This is the start of console output");
    }
    if(fileoutput)
    {

    }
}

void Logger::output(LoggerLevel level, std::string message)
{
    for(auto& x : components)
    {
        x.second->output (level, message);
    }
}

void Logger::addLogger(LoggerType type, LoggerComponent* component)
{
    if(components.find (type) == components.end ())
        components.emplace(type, component);
}

LoggerComponent* Logger::getLogger (LoggerType type)
{
    if(components.find (type) != components.end ())
        return components.at (type);
    return nullptr;
}

void Logger::clearLoggers()
{
    components.clear ();
}

void Logger::removeLogger(LoggerType type)
{
    if(components.find (type) != components.end ())
        components.erase (type);
}

logger.hpp

#ifndef LOGGER_HPP
#define LOGGER_HPP

#include "loggercomponent.hpp"

#include <map>

enum class LoggerType
{
    CONSOLE,
    FILE
};

class Logger
{
public:
    explicit Logger(bool verbose, bool fileoutput);

    void output(LoggerLevel level, std::string message);

    void addLogger(LoggerType type, LoggerComponent* component);
    void removeLogger(LoggerType type);
    void clearLoggers();
    LoggerComponent* getLogger(LoggerType type);

private:
    std::map<LoggerType, LoggerComponent*> components;
};

#endif // LOGGER_HPP

main.cpp

#include "logger.hpp"

int main()
{
    int* p;
    int i = 5;
    int j = 5;
    p = &i;
    std::cout << p << std::endl;
    p = &j;
    std::cout << p << std::endl;

    LoggerComponent c(LoggerLevel::DEBUG, &std::cout);

    c.output (LoggerLevel::INFO, "Hello World!");
    c.output (LoggerLevel::CRITICAL, "Hello World!");

    Logger c2(true, true);

    std::cout << c.getOutputStream () << std::endl;
    std::cout << c2.getLogger (LoggerType::CONSOLE)->getOutputStream () << std::endl;

    c2.output (LoggerLevel::INFO, "Hello World!");
    c2.output (LoggerLevel::CRITICAL, "Hello World!");
}

Aucun commentaire:

Enregistrer un commentaire