This commit is contained in:
Ben Kyd
2023-02-28 14:06:49 +00:00
parent 3f715ed57c
commit 054170b916
5 changed files with 163 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
example/*
!example/example.cpp
!example/CMakeLists.txt
.ccls-cache/
compile_commands.json

View File

@@ -1,2 +1,17 @@
# YOLO: You Only Log Once
The super simple BLAZING FAST c++ logging library
```cpp
#include "yolo/yolo.hpp"
yolo::info("Hello, World! {}", 42);
```
The simplest possible form of module logging for super speed
```cpp
auto mod = yolo::registerModule("MODULE", "\001bansi;m");
yolo::debug(mod, "This is a debug message {}", "Incredible!");
```

7
example/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.9)
project(yolo_example)
add_executable(yolo_example example.cpp)
target_include_directories(yolo_example PRIVATE "../include")

17
example/example.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "yolo/yolo.hpp"
int main(int, char**)
{
int x = 12;
int y = 3;
yolo::info("value for x is {} and value for y is {}", x, y);
auto mod = yolo::registerModule("COMPONENT", "\u001b[35;1m");
yolo::info(mod, "value for x is {} and value for y is {}", x, y);
yolo::warn(mod, "value for x is {} and value for y is {}", x, y);
yolo::error(mod, "value for x is {} and value for y is {}", x, y);
yolo::debug(mod, "value for x is {} and value for y is {}", x, y);
return 0;
}

119
include/yolo/yolo.hpp Normal file
View File

@@ -0,0 +1,119 @@
#pragma once
#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
#include <iomanip>
#include <vector>
#include <utility>
namespace yolo {
namespace detail {
static std::vector<std::pair<std::string, std::string>> modules;
template<typename... Args>
std::string format(const std::string& format, Args... args)
{
std::ostringstream oss;
size_t start = 0; size_t pos;
((oss << format.substr(start, (pos = format.find("{}", start)) - start) << args, start = pos + 2), ...);
oss << format.substr(start);
return oss.str();
}
std::string formatTime()
{
auto now = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(now);
std::tm local_time = *std::localtime(&time);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() % 1000;
std::ostringstream oss;
oss << std::put_time(&local_time, "%H:%M:%S.") << std::setfill('0') << std::setw(3) << ms;
return oss.str();
}
}
template <typename... Args>
inline void __log(const std::string& level, const std::string& col, const std::string& message, Args... args)
{
std::cout
<< '[' << detail::formatTime() << "] "
<< '[' << col << level << "\033[0m" << "] "
<< detail::format(message, args...) << std::endl;
}
template <typename... Args>
inline void __log(uint8_t module, const std::string& level, const std::string& col, const std::string& message, Args... args)
{
auto mod = detail::modules[module];
std::cout
<< '[' << detail::formatTime() << "] "
<< '[' << col << level << "\033[0m" << "] "
<< '[' << mod.second << mod.first << "\033[0m" << "] "
<< detail::format(message, args...) << std::endl;
}
template <typename... Args>
inline void info(const std::string& format, Args&&... args)
{
__log("INFO", "\u001b[32;1m", format, args...);
}
template <typename... Args>
inline void warn(const std::string& format, Args&&... args)
{
__log("WARN", "\u001b[33;1m", format, args...);
}
template <typename... Args>
inline void error(const std::string& format, Args&&... args)
{
__log("ERROR", "\u001b[31;1m", format, args...);
}
template <typename... Args>
inline void debug(const std::string& format, Args&&... args)
{
__log("DEBUG", "\u001b[34;1m", format, args...);
}
template <typename... Args>
inline void info(uint8_t module, const std::string& format, Args&&... args)
{
__log(module, "INFO", "\u001b[32;1m", format, args...);
}
template <typename... Args>
inline void warn(uint8_t module, const std::string& format, Args&&... args)
{
__log(module, "WARN", "\u001b[33;1m", format, args...);
}
template <typename... Args>
inline void error(uint8_t module, const std::string& format, Args&&... args)
{
__log(module, "ERROR", "\u001b[31;1m", format, args...);
}
template <typename... Args>
inline void debug(uint8_t module, const std::string& format, Args&&... args)
{
__log(module, "DEBUG", "\u001b[34;1m", format, args...);
}
// Registers a module with a name and an optional ANSI colour code..
// this will return a reference that you can pass for future logs
// which will format the logger correctly to your name and colour
inline uint8_t registerModule(std::string name, std::string ANSI)
{
detail::modules.push_back(std::make_pair(name, ANSI));
return detail::modules.size() - 1;
}
}