Skip to content

Commit

Permalink
add emojis and new logging levels
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexiousLu committed Oct 15, 2024
1 parent e593f66 commit 069b62d
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 15 deletions.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
# spdlog-emoji !

It's an emoji version of [spdlog](https://github.com/gabime/spdlog)! Plus some new levels of logging.

## What's difference
#### Why not enjoy some emojis!
WHO WANTS A LOG LIKE:
`[info] This is an info`
BUT NOT:
`[💬] This is an info`
?

#### (New) Log levels and emojis
```
TRACE 🐾
DEBUG 🔧
INFO 📢
TIMING ⏰
FUNNY 🤣
WARN 🤔 *
JOKER 🤡
GHOST 👻
SUCCESS 🎉
ERROR 🚨
FINISH ✅
CRITICAL ❌
OFF O (yes, this is not an emoji since it's never used by users)
```
\* I tried to use ⚠️, but it will be rendered by some default fonts, such as Menlo on MacOS, and it looks like `⚠️`. Since it is 21th Century now, The last one is totally unacceptable.

#### Usage
Use it just like spdlog, and you have more choices:
```
some_logger->trace("This is Trace");
some_logger->debug("This is Debug");
some_logger->info("This is Info");
some_logger->timing("This is Timing");
some_logger->funny("This is Funny");
some_logger->warn("This is Warn");
some_logger->joker("This is Joker");
some_logger->ghost("This is Ghost");
some_logger->success("This is Success");
some_logger->error("This is Error");
some_logger->finish("This is Finish");
some_logger->critical("This is Critical");
```

Here are what you got:

```
[2024-10-15 18:24:57.617] [some_logger_name] [🐾] This is Trace
[2024-10-15 18:24:57.617] [some_logger_name] [🔧] This is Debug
[2024-10-15 18:24:57.617] [some_logger_name] [💬] This is Info
[2024-10-15 18:24:57.617] [some_logger_name] [⏰] This is Timing
[2024-10-15 18:24:57.617] [some_logger_name] [🤣] This is Funny
[2024-10-15 18:24:57.617] [some_logger_name] [🤔] This is Warn
[2024-10-15 18:24:57.617] [some_logger_name] [🤡] This is Joker
[2024-10-15 18:24:57.617] [some_logger_name] [👻] This is Ghost
[2024-10-15 18:24:57.617] [some_logger_name] [🎉] This is Success
[2024-10-15 18:24:57.617] [some_logger_name] [🚨] This is Error
[2024-10-15 18:24:57.617] [some_logger_name] [✅] This is Finish
[2024-10-15 18:24:57.617] [some_logger_name] [❌] This is Critical
```


Tips: If you are using logging format that truncates the log_level_name, be careful, since emojis don't have a uniform string length and your truncation could result in weird output.

-----

Thanks to spdlog for its clean and tidy design and implementation, making this change very easy.

Following is the README of spdlog:

-----
# spdlog

Very fast, header-only/compiled, C++ logging library. [![ci](https://github.com/gabime/spdlog/actions/workflows/ci.yml/badge.svg)](https://github.com/gabime/spdlog/actions/workflows/ci.yml)  [![Build status](https://ci.appveyor.com/api/projects/status/d2jnxclg20vd0o50?svg=true&branch=v1.x)](https://ci.appveyor.com/project/gabime/spdlog) [![Release](https://img.shields.io/github/release/gabime/spdlog.svg)](https://github.com/gabime/spdlog/releases/latest)
Expand Down
57 changes: 43 additions & 14 deletions include/spdlog/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,17 @@ using level_t = std::atomic<int>;
#define SPDLOG_LEVEL_TRACE 0
#define SPDLOG_LEVEL_DEBUG 1
#define SPDLOG_LEVEL_INFO 2
#define SPDLOG_LEVEL_WARN 3
#define SPDLOG_LEVEL_ERROR 4
#define SPDLOG_LEVEL_CRITICAL 5
#define SPDLOG_LEVEL_OFF 6
#define SPDLOG_LEVEL_TIMING 3
#define SPDLOG_LEVEL_FUNNY 4
#define SPDLOG_LEVEL_WARN 5
#define SPDLOG_LEVEL_JOKER 6
#define SPDLOG_LEVEL_GHOST 7
#define SPDLOG_LEVEL_SUCCESS 8
#define SPDLOG_LEVEL_ERROR 9
#define SPDLOG_LEVEL_FINISH 10
#define SPDLOG_LEVEL_CRITICAL 11
#define SPDLOG_LEVEL_OFF 12


#if !defined(SPDLOG_ACTIVE_LEVEL)
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
Expand All @@ -246,34 +253,56 @@ enum level_enum : int {
trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG,
info = SPDLOG_LEVEL_INFO,
timing = SPDLOG_LEVEL_TIMING,
funny = SPDLOG_LEVEL_FUNNY,
warn = SPDLOG_LEVEL_WARN,
joker = SPDLOG_LEVEL_JOKER,
ghost = SPDLOG_LEVEL_GHOST,
success = SPDLOG_LEVEL_SUCCESS,
err = SPDLOG_LEVEL_ERROR,
finish = SPDLOG_LEVEL_FINISH,
critical = SPDLOG_LEVEL_CRITICAL,
off = SPDLOG_LEVEL_OFF,
n_levels
};

#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("🐾", sizeof("🐾")-1)
#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("🔧", sizeof("🔧")-1) // 🛠️ is kind of too dark in dark theme
#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("💬", sizeof("💬")-1)
#define SPDLOG_LEVEL_NAME_TIMING spdlog::string_view_t("", sizeof("")-1)
#define SPDLOG_LEVEL_NAME_FUNNY spdlog::string_view_t("🤣", sizeof("🤣")-1)
#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("🤔", sizeof("🤔")-1) // ⚠️ will be rendered wrong!
#define SPDLOG_LEVEL_NAME_JOKER spdlog::string_view_t("🤡", sizeof("🤡")-1)
#define SPDLOG_LEVEL_NAME_GHOST spdlog::string_view_t("👻", sizeof("👻")-1)
#define SPDLOG_LEVEL_NAME_SUCCESS spdlog::string_view_t("🎉", sizeof("🎉")-1)
#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("🚨", sizeof("🚨")-1)
#define SPDLOG_LEVEL_NAME_FINISH spdlog::string_view_t("", sizeof("")-1)
#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("", sizeof("")-1)
#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)

#if !defined(SPDLOG_LEVEL_NAMES)
#define SPDLOG_LEVEL_NAMES \
{ \
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, \
SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \
SPDLOG_LEVEL_NAME_OFF \
SPDLOG_LEVEL_NAME_TRACE, \
SPDLOG_LEVEL_NAME_DEBUG, \
SPDLOG_LEVEL_NAME_INFO, \
SPDLOG_LEVEL_NAME_TIMING, \
SPDLOG_LEVEL_NAME_FUNNY, \
SPDLOG_LEVEL_NAME_WARNING, \
SPDLOG_LEVEL_NAME_JOKER, \
SPDLOG_LEVEL_NAME_GHOST, \
SPDLOG_LEVEL_NAME_SUCCESS, \
SPDLOG_LEVEL_NAME_ERROR, \
SPDLOG_LEVEL_NAME_FINISH, \
SPDLOG_LEVEL_NAME_CRITICAL, \
SPDLOG_LEVEL_NAME_OFF \
}
#endif

#if !defined(SPDLOG_SHORT_LEVEL_NAMES)

#define SPDLOG_SHORT_LEVEL_NAMES \
{ "T", "D", "I", "W", "E", "C", "O" }
{ "🐾", "🔧", "💬", "", "🤣", "🤔", "🤡", "👻", "🎉", "🚨", "", "", "O" }
#endif

SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
Expand Down
90 changes: 90 additions & 0 deletions include/spdlog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <spdlog/common.h>
#include <spdlog/details/backtracer.h>
#include <spdlog/details/log_msg.h>
#include "common.h"

#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
#ifndef _WIN32
Expand Down Expand Up @@ -140,16 +141,45 @@ class SPDLOG_API logger {
log(level::info, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void timing(format_string_t<Args...> fmt, Args &&...args) {
log(level::timing, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void funny(format_string_t<Args...> fmt, Args &&...args) {
log(level::funny, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void warn(format_string_t<Args...> fmt, Args &&...args) {
log(level::warn, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void joker(format_string_t<Args...> fmt, Args &&...args) {
log(level::joker, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void ghost(format_string_t<Args...> fmt, Args &&...args) {
log(level::ghost, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void success(format_string_t<Args...> fmt, Args &&...args) {
log(level::success, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void error(format_string_t<Args...> fmt, Args &&...args) {
log(level::err, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void finish(format_string_t<Args...> fmt, Args &&...args) {
log(level::finish, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void critical(format_string_t<Args...> fmt, Args &&...args) {
log(level::critical, fmt, std::forward<Args>(args)...);
Expand Down Expand Up @@ -212,16 +242,46 @@ class SPDLOG_API logger {
log(level::info, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void timing(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::timing, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void funny(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::funny, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void warn(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::warn, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void joker(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::joker, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void ghost(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::ghost, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void success(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::success, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void error(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::err, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void finish(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::finish, fmt, std::forward<Args>(args)...);
}

template <typename... Args>
void critical(wformat_string_t<Args...> fmt, Args &&...args) {
log(level::critical, fmt, std::forward<Args>(args)...);
Expand All @@ -243,16 +303,46 @@ class SPDLOG_API logger {
log(level::info, msg);
}

template <typename T>
void timing(const T &msg) {
log(level::timing, msg);
}

template <typename T>
void funny(const T &msg) {
log(level::funny, msg);
}

template <typename T>
void warn(const T &msg) {
log(level::warn, msg);
}

template <typename T>
void joker(const T &msg) {
log(level::joker, msg);
}

template <typename T>
void ghost(const T &msg) {
log(level::ghost, msg);
}

template <typename T>
void success(const T &msg) {
log(level::success, msg);
}

template <typename T>
void error(const T &msg) {
log(level::err, msg);
}

template <typename T>
void finish(const T &msg) {
log(level::finish, msg);
}

template <typename T>
void critical(const T &msg) {
log(level::critical, msg);
Expand Down
Loading

0 comments on commit 069b62d

Please sign in to comment.