Skip to content

Instantly share code, notes, and snippets.

@TwoSquirrels
Last active August 18, 2025 17:36
Show Gist options
  • Select an option

  • Save TwoSquirrels/3c9ade71f849134c52d6e458b57fdeb0 to your computer and use it in GitHub Desktop.

Select an option

Save TwoSquirrels/3c9ade71f849134c52d6e458b57fdeb0 to your computer and use it in GitHub Desktop.
Siv3D 用のシンプルなロギングライブラリ
// SPDX-FileCopyrightText: (c) 2025 TwoSquirrels
// SPDX-License-Identifier: MIT
// SPDX-FileComment: see https://gist.github.com/TwoSquirrels/3c9ade71f849134c52d6e458b57fdeb0
# pragma once
# include <Siv3D.hpp>
namespace RisuLogger
{
/// @brief ログレベル
enum class Level : uint8
{
Trace = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
};
// s3d::Format 対応用
inline void Formatter(FormatData& formatData, const Level& value)
{
constexpr std::array<StringView, 5> LevelNames = {U"TRACE", U"DEBUG", U"INFO", U"WARN", U"ERROR"};
formatData.string += LevelNames[FromEnum(value)];
}
/// @brief どのレベルから画面に出力するかの閾値
/// @note リリースビルドでは画面にログを出力しないことに注意。
inline auto printLevel = Level::Info;
namespace detail
{
inline void Log(const FilePathView file, const uint32 line, const Level level, const String& text)
{
const String message = U"{} ({})\t[{}:{}/{:<5}]:\t{}"_fmt(
DateTime::Now().format(U"HH:mm:ss.SS"), Scene::FrameCount(), FileSystem::RelativePath(file), line, Format(level), text);
Logger(message);
# if defined(_DEBUG) || defined(DEBUG)
if (printLevel <= level)
{
Print(message);
}
# endif
}
} // namespace detail
} // namespace RisuLogger
/// @brief ファイル名・行番号付きでログを出力する。
/// @param LEVEL ログレベル
/// @param FMT フォーマット文字列
/// @param ... フォーマット文字列内の `{}` に対応する値
/// @remarks ユーザーはこのマクロを直接使用せず、LOG_INFO などを使用すること。
# define RISU_LOGGER_LOG(LEVEL, FMT, ...) (::RisuLogger::detail::Log(s3d::Unicode::Widen(__FILE__), (__LINE__), (LEVEL), s3d::Fmt(FMT)(##__VA_ARGS__)))
/// @brief 変数の名前と値をログに出力する。
/// @param VALUE 値を確認したい変数や式
# define DUMP_TRACE(VALUE) (RISU_LOGGER_LOG(::RisuLogger::Level::Trace, U"{} = {}", s3d::Unicode::Widen(#VALUE), (VALUE)))
/// @brief デバッグ用の詳細な情報を出力する。
/// @param FMT フォーマット文字列
/// @param ... フォーマット文字列内の `{}` に対応する値
# define LOG_DEBUG(FMT, ...) (RISU_LOGGER_LOG(::RisuLogger::Level::Debug, (FMT), ##__VA_ARGS__))
/// @brief アプリケーションの状態や進行状況を出力する。
/// @param FMT フォーマット文字列
/// @param ... フォーマット文字列内の `{}` に対応する値
# define LOG_INFO(FMT, ...) (RISU_LOGGER_LOG(::RisuLogger::Level::Info, (FMT), ##__VA_ARGS__))
/// @def LOG_WARN(FMT, ...)
/// @brief 予期しない事象や注意が必要な情報を出力する。
/// @param FMT フォーマット文字列
/// @param ... フォーマット文字列内の `{}` に対応する値
# define LOG_WARN(FMT, ...) (RISU_LOGGER_LOG(::RisuLogger::Level::Warn, (FMT), ##__VA_ARGS__))
/// @def LOG_ERROR(FMT, ...)
/// @brief エラーや例外的な状況を出力する。
/// @param FMT フォーマット文字列
/// @param ... フォーマット文字列内の `{}` に対応する値
# define LOG_ERROR(FMT, ...) (RISU_LOGGER_LOG(::RisuLogger::Level::Error, (FMT), ##__VA_ARGS__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment