C++20以降の世界ではstd::chronoを使うのがすっかりあたり前になりつつありますね。ありがたや。
私もstd::chronoの恩恵に預かっていたのですが
std::chronoが提供する型を標準出力やログに出力したい
という用途がでてきて少し嵌ったのでメモしておきます。
fmtlibでは問題なく動く
std::printと同様にfmt::printであればfmt/chrono.hをincludeすることで、{:<format>}と任意の形式で日付・時刻を表示することができます。
#include <chrono> #include "fmt/chrono.h" int main() { auto now = std::chrono::system_clock::now(); fmt::print("Time: {:%H:%M:%S}\n", now); }
実行結果は次のようになります。
Time: 20:10:39.467474536
std::print(C++23以降)でも問題なく動く
C++23に対応していないといけませんが、std::printでも問題なくstd::chronoが使えます。
#include <print> #include <chrono> int main() { auto now = std::chrono::system_clock::now(); std::print("Time: {:%H:%M:%S}\n", now); }
Quillを使う場合に問題が発生してしまう
ところがQuillでは問題がおきてしまいます。
QuillとはC++ロギングライブラリです。
すごく高速に動き、さまざまな出力先をサポートしていて、fmtlibサポートによるフォーマットに対応しているライブラリで、C++でログを出力したい場合に便利です。
ここでのQuillは10.0.1をvcpkgでインストールして使っています。
ソースコードは以下の通り。
#include <chrono> #include "quill/Backend.h" #include "quill/Frontend.h" #include "quill/LogMacros.h" #include "quill/sinks/ConsoleSink.h" int main() { quill::Backend::start(); auto logger = quill::Frontend::create_or_get_logger( "root", quill::Frontend::create_or_get_sink<quill::ConsoleSink>("root_sink")); auto now = std::chrono::system_clock::now(); LOG_INFO(logger, "Time: {:%H:%M:%S}\n", now); }
悲しい。 けどちゃんとエラーメッセージが出てくれるのありがたい。
blog-2025-08-25/03_quill_error.cpp:13:3: required from here
13 | LOG_INFO(logger, "Time: {:%H:%M:%S}\n", now);
| ^
blog-2025-08-25/build/vcpkg_installed/x64-linux/include/quill/core/Codec.h:64:5: エラー: static assertion failed:
+------------------------------------------------------------------------------+
| Missing Codec for Type 'Arg' |
+------------------------------------------------------------------------------+
Error: A codec for the specified type 'Arg' is not available.
Possible solutions:
1. If Arg is an STL type:
- Ensure you have included the necessary headers for the specific STL type you are using from the quill/std folder.
2. If Arg is a user-defined type:
- Use either 'DeferredFormatCodec' or 'DirectFormatCodec'.
- Define a custom Codec for your type.
- Consider converting the value to a string before logging.
Note: The specific type of 'Arg' can be found in the compiler error message.
Look for the instantiation of 'codec_not_found_for_type<Arg>' in the error output.
The compiler should indicate what type 'Arg' represents in this instance.
For more information see https://quillcpp.readthedocs.io/en/latest/cheat_sheet.html
丁寧なエラーメッセージの通り「std::chrono::time_pointに対応するquill::Codecがない」のが原因ですね。
/** typename = void for specializations with enable_if **/ template <typename Arg, typename = void> struct Codec { /***/ QUILL_NODISCARD QUILL_ATTRIBUTE_HOT static size_t compute_encoded_size( QUILL_MAYBE_UNUSED detail::SizeCacheVector& conditional_arg_size_cache, QUILL_MAYBE_UNUSED Arg const& arg) noexcept {
専用のヘッダーをインクルードすると解決
ちゃんと公式ドキュメントに解決策がのってました。 quillcpp.readthedocs.io
タイトルにSTLって書いてありますが、std::chrono関連も対応するヘッダー(quill/std/Chrono.h)を明示的にインクルードすれば良いとのこと。
- #include "quill/sinks/ConsoleSink.h" + #include "quill/sinks/ConsoleSink.h" + #include "quill/std/Chrono.h"
念のため適用したコードは以下の通り。
#include <chrono> #include "quill/Backend.h" #include "quill/Frontend.h" #include "quill/LogMacros.h" #include "quill/sinks/ConsoleSink.h" #include "quill/std/Chrono.h" int main() { quill::Backend::start(); auto logger = quill::Frontend::create_or_get_logger( "root", quill::Frontend::create_or_get_sink<quill::ConsoleSink>("root_sink")); auto now = std::chrono::system_clock::now(); LOG_INFO(logger, "Time: {:%H:%M:%S}\n", now); }
この1行追加だけで、うまく動くようになりました。
公式ドキュメント読むの大事ですね。
12:02:35.870619739 [3609912] 04_quill.cpp:14 LOG_INFO root Time: 03:02:35.870619654
ちなみにquill/std/Chrono.hにはquill::Codecの特殊化が定義されています。
template <template <typename...> class ChronoType, typename TimeSpec, typename DurationType> struct Codec<ChronoType<TimeSpec, DurationType>, std::enable_if_t<std::disjunction_v<std::is_same<ChronoType<TimeSpec, DurationType>, std::chrono::time_point<TimeSpec, DurationType>>, std::is_same<ChronoType<TimeSpec, DurationType>, std::chrono::duration<TimeSpec, DurationType>>>>> : DeferredFormatCodec<ChronoType<TimeSpec, DurationType>> { };
DeferredFormatCodecを継承しているので、fmtlibのフォーマット指定子がそのまま使えるようになっています。
DeferredFormatCodecでは文字列へのフォーマットは別スレッドのバックエンドでやってくれるのでメインスレッドの負荷はほとんどありません。ありがたい。
quill/std以下のヘッダー一覧
quill/std/Chrono.h以外にもquill/std以下にはたくさんのヘッダーがあって、色々な型に対応しています。
quill/include/quill/std at master · odygrd/quill · GitHub
| ヘッダー | 対応する型 |
|---|---|
| Array.h | std::array, charの配列 |
| Chrono.h | std::chrono::time_point, std::chrono::duration |
| Deque.h | std::deque |
| FilesystemPath.h | std::filesystem::path |
| ForwardList.h | std::forward_list |
| List.h | std::list |
| Map.h | std::map, std::multimap |
| Optional.h | std::optional |
| Pair.h | std::pair |
| Set.h | std::set, std::multiset |
| Tuple.h | std::tuple |
| UnorderedMap.h | std::unordered_map, std::unordered_multimap |
| UnorderedSet.h | std::unordered_set, unordered_multiset |
| Vector.h | std::vector |
| WideString.h | std::wstring, wchar_tの配列 |
ちなみにstd::unordered_mapだとこんな感じになります。
#include <unordered_map> #include "quill/Backend.h" #include "quill/Frontend.h" #include "quill/LogMacros.h" #include "quill/sinks/ConsoleSink.h" #include "quill/std/UnorderedMap.h" int main() { quill::Backend::start(); auto logger = quill::Frontend::create_or_get_logger( "root", quill::Frontend::create_or_get_sink<quill::ConsoleSink>("root_sink")); auto value = std::unordered_map<int, std::string>{{1, "one"}, {2, "two"}, {3, "three"}}; LOG_INFO(logger, "unordered_map: {}\n", value); }
実行結果は以下の通り。 ちゃんと中身が表示されてますね。便利。
11:58:29.260206994 [3592695] 05_unordered_map.cpp:20 LOG_INFO root unordered_map: {1: "one", 2: "two", 3: "three"}
まとめ
Missing Codec for Type 'Arg'(std::chrono::time_point)というコンパイルエラーは、対応するquill::Codecが未定義なのが原因です。
対応するquill::Codecを定義しているquill/std/Chrono.hをインクルードすることで解決できます。
std::chrono以外でも必要な型に対応するquill/std/<Header>.hを追加でincludeすることで、追加実装をすることなくQuillでログ出力できるようになります。
補足
ソースコードとvcpkg.json, CMakeLists.txtなどについては以下に登録してあります。 github.com