log: encapsulate logger
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
		
							
								
								
									
										8
									
								
								src/util/log.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/util/log.cc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
#include "log.hh"
 | 
			
		||||
 | 
			
		||||
logging::Logger glogger = logging::Logger();
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
matar::set_log_level(LogLevel level) {
 | 
			
		||||
    glogger.set_level(level);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										106
									
								
								src/util/log.hh
									
									
									
									
									
								
							
							
						
						
									
										106
									
								
								src/util/log.hh
									
									
									
									
									
								
							@@ -1,12 +1,10 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "util/loglevel.hh"
 | 
			
		||||
#include <fmt/ostream.h>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
using fmt::print;
 | 
			
		||||
using std::clog;
 | 
			
		||||
 | 
			
		||||
namespace logger {
 | 
			
		||||
namespace logging {
 | 
			
		||||
namespace ansi {
 | 
			
		||||
static constexpr std::string_view RED     = "\033[31m";
 | 
			
		||||
static constexpr std::string_view YELLOW  = "\033[33m";
 | 
			
		||||
@@ -16,43 +14,69 @@ static constexpr std::string_view BOLD    = "\033[1m";
 | 
			
		||||
static constexpr std::string_view RESET   = "\033[0m";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<typename... Args>
 | 
			
		||||
inline void
 | 
			
		||||
log_raw(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
    fmt::println(clog, fmt, std::forward<Args>(args)...);
 | 
			
		||||
using fmt::print;
 | 
			
		||||
 | 
			
		||||
class Logger {
 | 
			
		||||
    using LogLevel = matar::LogLevel;
 | 
			
		||||
 | 
			
		||||
  public:
 | 
			
		||||
    Logger(LogLevel level = LogLevel::Debug)
 | 
			
		||||
      : level(0) {
 | 
			
		||||
        set_level(level);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void log(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
        fmt::println(stream, fmt, std::forward<Args>(args)...);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void debug(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
        if (level & static_cast<uint8_t>(LogLevel::Debug)) {
 | 
			
		||||
            print(stream, "{}{}[DEBUG] ", ansi::MAGENTA, ansi::BOLD);
 | 
			
		||||
            log(fmt, std::forward<Args>(args)...);
 | 
			
		||||
            print(stream, ansi::RESET);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void info(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
        if (level & static_cast<uint8_t>(LogLevel::Info)) {
 | 
			
		||||
            print(stream, "{}[INFO] ", ansi::WHITE);
 | 
			
		||||
            log(fmt, std::forward<Args>(args)...);
 | 
			
		||||
            print(stream, ansi::RESET);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void warn(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
        if (level & static_cast<uint8_t>(LogLevel::Warn)) {
 | 
			
		||||
            print(stream, "{}[WARN] ", ansi::YELLOW);
 | 
			
		||||
            log(fmt, std::forward<Args>(args)...);
 | 
			
		||||
            print(stream, ansi::RESET);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    template<typename... Args>
 | 
			
		||||
    void error(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
        if (level & static_cast<uint8_t>(LogLevel::Error)) {
 | 
			
		||||
            print(stream, "{}{}[ERROR] ", ansi::RED, ansi::BOLD);
 | 
			
		||||
            log(fmt, std::forward<Args>(args)...);
 | 
			
		||||
            print(stream, ansi::RESET);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void set_level(LogLevel level) {
 | 
			
		||||
        this->level = (static_cast<uint8_t>(level) << 1) - 1;
 | 
			
		||||
    }
 | 
			
		||||
    void set_stream(std::ostream& stream) { this->stream = stream; }
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
    uint8_t level;
 | 
			
		||||
    std::reference_wrapper<std::ostream> stream = std::clog;
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<typename... Args>
 | 
			
		||||
inline void
 | 
			
		||||
log_debug(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
    print(clog, "{}{}[DEBUG] ", ansi::MAGENTA, ansi::BOLD);
 | 
			
		||||
    log_raw(fmt, std::forward<Args>(args)...);
 | 
			
		||||
    print(clog, ansi::RESET);
 | 
			
		||||
}
 | 
			
		||||
extern logging::Logger glogger;
 | 
			
		||||
 | 
			
		||||
template<typename... Args>
 | 
			
		||||
inline void
 | 
			
		||||
log_info(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
    print(clog, "{}[INFO] ", ansi::WHITE);
 | 
			
		||||
    log_raw(fmt, std::forward<Args>(args)...);
 | 
			
		||||
    print(clog, ansi::RESET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<typename... Args>
 | 
			
		||||
inline void
 | 
			
		||||
log_warn(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
    print(clog, "{}[WARN] ", ansi::YELLOW);
 | 
			
		||||
    log_raw(fmt, std::forward<Args>(args)...);
 | 
			
		||||
    print(clog, ansi::RESET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<typename... Args>
 | 
			
		||||
inline void
 | 
			
		||||
log_error(const fmt::format_string<Args...>& fmt, Args&&... args) {
 | 
			
		||||
    print(clog, "{}{}[ERROR] ", ansi::RED, ansi::BOLD);
 | 
			
		||||
    log_raw(fmt, std::forward<Args>(args)...);
 | 
			
		||||
    print(clog, ansi::RESET);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define debug(value) logger::log_debug("{} = {}", #value, value)
 | 
			
		||||
#define debug(x) logger.debug("{} = {}", #x, x);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								src/util/meson.build
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/util/meson.build
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
lib_sources += files(
 | 
			
		||||
  'log.cc'
 | 
			
		||||
)
 | 
			
		||||
		Reference in New Issue
	
	Block a user