Series article contents
The version of the source code is v1 x
Code statistics through cloc command
------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C/C++ Header 104 6521 3730 33410 C++ 34 635 264 3130 CMake 7 92 82 433 Markdown 1 64 0 402 YAML 2 33 18 218 SVG 1 0 0 43 reStructuredText 1 5 0 22 Python 1 4 0 13 Bourne Shell 1 4 0 12 ------------------------------------------------------------------------------- SUM: 152 7358 4094 37683 -------------------------------------------------------------------------------
You can see that the total amount of code is not small.
However, the header file has more than 3w lines, and the source file has only 3130 lines
directory structure
Look through the tree command. The good guy is almost all header files
bench directory
The bench directory is worth studying. From it, you can judge what the core of the project is by looking at what it tests. Bench should also cover almost all usage scenarios. In other words, learning begins with using. So it is of great significance to study it.
├── bench │ ├── async_bench.cpp │ ├── bench.cpp │ ├── CMakeLists.txt │ ├── formatter-bench.cpp │ ├── latency.cpp │ └── utils.h ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C++ 4 98 95 498 CMake 1 8 5 28 C/C++ Header 1 6 4 24 ------------------------------------------------------------------------------- SUM: 6 112 104 550 -------------------------------------------------------------------------------
bench has four cpp files, which test four indicators respectively: asynchronous log, synchronous log, format efficiency test and delay test. There is also a utils tool.
example directory
A usage example file, readme The examples in MD are all here. You can compile and run them
├── example │ ├── CMakeLists.txt │ └── example.cpp ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C++ 1 49 70 258 CMake 1 4 8 11 ------------------------------------------------------------------------------- SUM: 2 53 78 269 -------------------------------------------------------------------------------
include
This can be said to be the core of the project. The spd log library says that it can only run with header files. It seems that its essence is here. I'm also curious. Header files are usually some class definitions and interfaces. If you want to attach an implementation, it's difficult to handle the references between various types. So how is its code implementation organized?
└── spdlog ├── async.h ├── async_logger.h ├── async_logger-inl.h ├── cfg │ ├── argv.h │ ├── env.h │ ├── helpers.h │ └── helpers-inl.h ├── common.h ├── common-inl.h ├── details │ ├── backtracer.h │ ├── backtracer-inl.h │ ├── circular_q.h │ ├── console_globals.h │ ├── file_helper.h │ ├── file_helper-inl.h │ ├── fmt_helper.h │ ├── log_msg_buffer.h │ ├── log_msg_buffer-inl.h │ ├── log_msg.h │ ├── log_msg-inl.h │ ├── mpmc_blocking_q.h │ ├── null_mutex.h │ ├── os.h │ ├── os-inl.h │ ├── periodic_worker.h │ ├── periodic_worker-inl.h │ ├── registry.h │ ├── registry-inl.h │ ├── synchronous_factory.h │ ├── tcp_client.h │ ├── tcp_client-windows.h │ ├── thread_pool.h │ ├── thread_pool-inl.h │ ├── udp_client.h │ ├── udp_client-windows.h │ └── windows_include.h ├── fmt │ ├── bin_to_hex.h │ ├── bundled │ │ ├── args.h │ │ ├── chrono.h │ │ ├── color.h │ │ ├── compile.h │ │ ├── core.h │ │ ├── fmt.license.rst │ │ ├── format.h │ │ ├── format-inl.h │ │ ├── locale.h │ │ ├── os.h │ │ ├── ostream.h │ │ ├── printf.h │ │ ├── ranges.h │ │ └── xchar.h │ ├── chrono.h │ ├── compile.h │ ├── fmt.h │ ├── ostr.h │ ├── ranges.h │ └── xchar.h ├── formatter.h ├── fwd.h ├── logger.h ├── logger-inl.h ├── pattern_formatter.h ├── pattern_formatter-inl.h ├── sinks │ ├── android_sink.h │ ├── ansicolor_sink.h │ ├── ansicolor_sink-inl.h │ ├── base_sink.h │ ├── base_sink-inl.h │ ├── basic_file_sink.h │ ├── basic_file_sink-inl.h │ ├── daily_file_sink.h │ ├── dist_sink.h │ ├── dup_filter_sink.h │ ├── hourly_file_sink.h │ ├── mongo_sink.h │ ├── msvc_sink.h │ ├── null_sink.h │ ├── ostream_sink.h │ ├── qt_sinks.h │ ├── ringbuffer_sink.h │ ├── rotating_file_sink.h │ ├── rotating_file_sink-inl.h │ ├── sink.h │ ├── sink-inl.h │ ├── stdout_color_sinks.h │ ├── stdout_color_sinks-inl.h │ ├── stdout_sinks.h │ ├── stdout_sinks-inl.h │ ├── syslog_sink.h │ ├── systemd_sink.h │ ├── tcp_sink.h │ ├── udp_sink.h │ ├── wincolor_sink.h │ ├── wincolor_sink-inl.h │ └── win_eventlog_sink.h ├── spdlog.h ├── spdlog-inl.h ├── stopwatch.h ├── tweakme.h └── version.h ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C/C++ Header 99 3342 2600 19602 reStructuredText 1 5 0 22 ------------------------------------------------------------------------------- SUM: 100 3347 2600 19624 -------------------------------------------------------------------------------
After browsing, it is mainly divided into several modules: configuration module, details module, formatting module and sins module. There are also some scattered header files. I guess these header files depend on the above modules, encapsulate several modules, and provide external use interfaces
src directory
There are only 7 files, which roughly correspond to several major modules in the header file and several important scattered header files. But there is no one-to-one correspondence. And the source code file is only 180 lines, which is surprisingly small. There are more test files than it. You can imagine how important and subtle those header files are
async,fmt,spdlog,stdout_ Sins has a corresponding header file with the same name
file_sinks,color_ Sins and cfg do not. among
- color_ Sins has an stdout_color_sinks.h corresponds to it.
- cfg should correspond to the entire cfg module.
- file_ Sins has a lot of files_ Sink corresponds to it.
├── src │ ├── async.cpp │ ├── cfg.cpp │ ├── color_sinks.cpp │ ├── file_sinks.cpp │ ├── fmt.cpp │ ├── spdlog.cpp │ └── stdout_sinks.cpp ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C++ 7 45 30 180 ------------------------------------------------------------------------------- SUM: 7 45 30 180 -------------------------------------------------------------------------------
Test catalog
Test files should provide more detailed usage information than files in the bench directory. It is better to use it as an auxiliary when you can't understand the code.
└── tests ├── catch.hpp ├── catch.license ├── CMakeLists.txt ├── includes.h ├── main.cpp ├── test_async.cpp ├── test_backtrace.cpp ├── test_cfg.cpp ├── test_create_dir.cpp ├── test_daily_logger.cpp ├── test_dup_filter.cpp ├── test_errors.cpp ├── test_eventlog.cpp ├── test_file_helper.cpp ├── test_file_logging.cpp ├── test_fmt_helper.cpp ├── test_macros.cpp ├── test_misc.cpp ├── test_mpmc_q.cpp ├── test_pattern_formatter.cpp ├── test_registry.cpp ├── test_sink.h ├── test_stdout_api.cpp ├── test_stopwatch.cpp ├── test_systemd.cpp ├── test_time_point.cpp ├── utils.cpp └── utils.h ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- C/C++ Header 4 3173 1126 13784 C++ 22 443 69 2194 CMake 1 10 3 57 ------------------------------------------------------------------------------- SUM: 27 3626 1198 16035 -------------------------------------------------------------------------------