spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Provide option to use spdlog as a (static) library

Open gregoire-astruc opened this issue 11 years ago • 52 comments

For a reasonable sized project, the logger is very likely to be all over the place.

Rebuilding the whole project because an implementation detail changed would be a pain.

Could we have a compile-time option (eg SPDLOG_LIBRARY or SPDLOG_HEADER_ONLY) that would allow somebody to choose between header-only or precompiled?

I noticed cppformat uses a similar approach and I'd really appreciate that option.

I can provide that if you're up for it.

gregoire-astruc avatar Aug 13 '15 21:08 gregoire-astruc

Sounds good but since its a header only by design it probably won't be very trivial.. Although i did keep a separation of impl headers and api headers.

gabime avatar Aug 13 '15 21:08 gabime

@gregoire-astruc But an implementation detail change will only happen if you change the local copy of the spdlog library yourself, or decide to update it from the external repository, and that is not something that would likely happen even once a day, right? Therefore this will just add more complexity to the library without any real benefit in the most common case, right?

rpopescu avatar Aug 14 '15 08:08 rpopescu

I see your point, but I'm not quite sure. For instance cppformat is used in the project as header only whilst it defaults to a static one: as a client you used an option provided by the library.

Any project using spdlog is likely to be some orders of magnitude bigger, even simply looking at files count, than spdlog itself.

Now I'd expect the logging system to be all over the place. Even if you wrap it in another class, it still gets transitively included.

Using the header only approach, any change would trigger almost a rebuild all of the application.

Using the library approach, you'd rebuild spdlog and relink only, which should be the common case (no public api change).

cppformat uses this added complexity and it's nice, as a user, to have the choice.

gregoire-astruc avatar Aug 14 '15 13:08 gregoire-astruc

@gabime : I was on Linux Mint.

gregoire-astruc avatar Aug 14 '15 13:08 gregoire-astruc

@gregoire-astruc Either I don't get it, or you don't see my point, because the key part in your argument is "any change would trigger almost a rebuild all of the application."

What change? When would changes happen to spdlog, part of a larger application? I think the answer is once in a blue moon: when the larger application owner changes spdlog manually, or it updates it from an external source. These are rare events, therefore there is no pain caused by spurious rebuilds.

rpopescu avatar Aug 14 '15 13:08 rpopescu

I did not miss your point and I do agree to some extent.

However, as I mentioned with cppformat, as the library client you had a choice and you used it.

As rare as you update any library it's not uncommon, and I'd appreciate the option to compile most of it aside a larger project.

This also means I can update more frequently as long as the public headers don't change, since it will only require a relink (well there's more to it, but mostly).

gregoire-astruc avatar Aug 14 '15 14:08 gregoire-astruc

I get it - you're focused on having the option part, I'm focused on the not-paying-for-what-I-don't-use part. Those things may co-exist :)

rpopescu avatar Aug 14 '15 14:08 rpopescu

Exactly: I'm all for libraries which gives me viable options fitting my needs and expectations (thus my contribution).

As for the second part well:

  • With a static library, any non-template function is compiled only once and could still very well be inlined by the linker, optimized and all.
  • With the header only version every function will be recompiled for any compilation unit using it and duplicates will be merged at link-time. That's two to three times the work, and I'd appreciate to limit that.

gregoire-astruc avatar Aug 14 '15 14:08 gregoire-astruc

Right, although pre-compiled headers solve this too, so maybe its not critical

gabime avatar Aug 14 '15 15:08 gabime

@gabime I'm afraid that pre-compiled headers are not a working solution here. I have evaluated using spdlog for our project few days ago and here is my experience and some numbers for the following minimal code:

#include "spdlog/spdlog.h"
int main() {
  auto console = spdlog::stdout_logger_mt("console", true);
  console->info("Hello world!") ;
  return 0;
}

With Clang++ 3.9 (i5-4670K @ 3.40GHz, Linux), with and without a pre-compiled header containing only "spdlog/spdlog.h", trying "debug" and "release" builds, the compilation times were the following:

no optimizations -O3
direct #include 1.45s 2.4s
using PCH 0.95s 1.85s

So this example alone takes almost a second to compile even with PCH and no optimizations. When I want to use a logging library almost in all source files, then every single time I change anything in my source code I need to wait 1-2s more for each affected source file (depending on the setup of course). It is a serious penalty in development time.

Interestingly, when I try to compile only int main() { return 0; } with the PCH, I still get 0.75s in debug mode. So the feature it is not cheap at all.

Besides the not so great compilation speed, pre-compiled headers have plenty of their own problems.

I would hope that with a proper knowledge of the library internals, it should not be so difficult to allow users to select a desired behavior. I think, the ideal solution from user's perspective would be this:

Allow to define a flag like SPDLOG_NO_HEADER_ONLY to force the library to include only the necessary internal parts when included. Then, user could include the "spdlog/spdlog.h" into one selected source file like this:

#define SPDLOG_PLACE_INTERNALS
#include "spdlog/spdlog.h"

And the non-header-only parts of the library would be compiled there. So no static library would be needed, build process would by the same and without setting the flag, everything would work as usual.

The beauty of such solution is that simply by changing a flag from your build system, you could toggle between "header only" and "library" options. E.g. fast compilation for development, best optimization for release builds. It would be sexy :)

morpav avatar May 21 '16 15:05 morpav

Pavel,

While the experiment is worthwhile, the conclusion that you need to wait 1-2 seconds per file using spdlog is not sound.

If you want to measure that overhead, you should write a few more files with functions calling spdlog's API, and incrementally compile a series of binaries consisting of an increasing number of source files (start with 1 as you did, then 2 files etc.). Then you measure the difference between the build times of these binaries and can draw a better conclusion, because as such what you're measuring with just 1 file involves other processes which are not cheap at all, in particular linking.

Cheers, Radu

On 21 May 2016 at 17:27, Pavel Moravec [email protected] wrote:

@gabime https://github.com/gabime I'm afraid that pre-compiled headers are not a working solution here. I have evaluated using spdlog for our project few days ago and here is my experience and some numbers for the following minimal code:

#include "spdlog/spdlog.h" int main() { auto console = spdlog::stdout_logger_mt("console", true); console->info("Hello world!") ; return 0; }

With Clang++ 3.9 (i5-4670K @ 3.40GHz, Linux), with and without a pre-compiled header containing only "spdlog/spdlog.h", trying "debug" and "release" builds, the compilation times were the following: no optimizations -O3 direct #include 1.45s 2.4s using PCH 0.95s 1.85s

So this example alone takes almost a second to compile even with PCH and no optimizations. When I want to use a logging library almost in all source files, then every single time I change anything in my source code I need to wait 1-2s more for each affected source file (depending on the setup of course). It is a serious penalty in development time.

Interestingly, when I try to compile only int main() { return 0; } with the PCH, I still get 0.75s in debug mode. So the feature it is not cheap at all.

Besides the not so great compilation speed, pre-compiled headers have plenty of their own problems.

I would hope that with a proper knowledge of the library internals, it should not be so difficult to allow users to select a desired behavior. I think, the ideal solution from user's perspective would be this:

Allow to define a flag like SPDLOG_NO_HEADER_ONLY to force the library to include only the necessary internal parts when included. Then, user could include the "spdlog/spdlog.h" into one selected source file like this:

#define SPDLOG_PLACE_INTERNALS #include "spdlog/spdlog.h"

And the non-header-only parts of the library would be compiled there. So no static library would be needed, build process would by the same and without setting the flag, everything would work as usual.

The beauty of such solution is that simply by changing a flag from your build system, you could toggle between "header only" and "library" options. E.g. fast compilation for development, best optimization for release builds. It would be sexy :)

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/gabime/spdlog/issues/120#issuecomment-220783778

Radu-Adrian Popescu Mobile +31-(0)611 00 45 20

rpopescu avatar May 21 '16 22:05 rpopescu

@rpopescu Thanks for your inputs to the discussion.

For clarification, the presented numbers are my compilation times, not complete build times. And even if you add link time, the compilation time vastly dominates in this scenario.

One can try to compile as many files as wanted but since the compilation is independent for each file the associated price will be paid for every single compilation unit which includes the library, even transitively.

I tried to add the spdlog header to one of our common header files in our project to measure the impact on compilation time for a large set of different files. I tried various scenarios, without actually using spdlog at all (only including the header) or with using it etc. The details are not really interesting here since these measurements are not repeatable in different environments. But the measurements were consistent with the provided numbers. So I wanted to present the simplest example to make the point as clear and repeatable as possible.

I re-ran all the easily repeatable tests on fresh system without any other intensive processes running. Here it is, you can draw your own conclusion.

Basic spdlog usage

#include "spdlog/spdlog.h"
int main() {
  auto console = spdlog::stdout_logger_mt("console", true);
  console->info("Hello world!") ;
  return 0;
}
no optimizations -O3
direct #include 1.38s 2.37s
using PCH 0.87s 1.84s

Only included, not used

#include "spdlog/spdlog.h"
int main() { return 0; }
no optimizations -O3
direct #include 1.22s 1.62s
using PCH 0.68s 1.07s

No include, no usage, only in pre-compiled header

int main() { return 0; }
no optimizations -O3
trivial main only 0.015s 0.017s
using PCH 0.68s 1.07s

The library is great! But I'm sure it could be easily much more usable if the "header only" behavior could be altered.

morpav avatar May 23 '16 07:05 morpav

@morpav If you have clear idea how to conveniently support both modes, a pull request will be most welcome

gabime avatar May 23 '16 12:05 gabime

@gabime Yes, I understand. Since I never saw the internals, I can provide only the idea right now.

For any header-only library, it should be enough to separate the current code into two parts and solve small issues with inlines.

Part 1: Declaration of smallest possible interface to the library. E.g. declaration of public non-templated functions, forward declarations of internal classes etc. Plus templates needed by a library users. This should be as thin as possible.

Part 2: Definitions of items from block 1. Actual implementation of all non-interface stuff.

It's like separating header and implementation file with a focus on hiding as much as possible in the implementation file. Let's assume we have two flags:

  • SPDLOG_LIBRARY - global flag, toggled from build system
  • SPDLOG_PLACE_INTERNALS - A flag used in exactly one compilation unit when one wants to use non-header-only option (SPDLOG_LIBRARY).

Then a pseudo-code of spdlog/spdlog.h could be like this:

#if (!defined SPDLOG_LIBRARY)
#define SPDLOG_INLINE inline
#else
#define SPDLOG_INLINE
#endif

#include part 1.
    .. containing something like
    class SomeClass {
     public:
      SPDLOG_INLINE void SomeMember();
      ...
    };

#if defined SPDLOG_PLACE_INTERNALS || !defined SPDLOG_LIBRARY
   #include  part 2
#endif

The smaller the part 1 is the better. A key thing is that the part 2 can provide all the implementation when used as header-only library, so everything is visible to a compiler to generate the most optimized code. On the other hand, if one include only the first two parts, most of the code should be placed in the one implementation file and a linker will do its work. Declarations from part 1 are typically really cheap. One exception is heavy usage of templates, but then the cost is unavoidable.

The SPDLOG_INLINE stuff solves issues with multiple symbol definitions and/or undefined references in one or the second usage.

I'm sorry that I cannot provide a pull request but maybe someone can read this and help more.

Pavel

morpav avatar May 23 '16 15:05 morpav

you can also read on some strategies to support static, dynamic and header only here https://vittorioromeo.info/index/blog/2016_cpp_library_configuration_api.html

tnovotny avatar Nov 21 '16 21:11 tnovotny

I think this would be a nice option (maybe using the same approach as Loguru). As it is, #including the spdlog header everywhere significantly slows down my build. Pre-compiled headers might help but they are a pain to set up.

Without spdlog:

real	0m40.314s
user	2m13.539s
sys	0m6.525s

Just adding #include <spdlog/spdlog.h> to my logging header:

real	1m8.682s
user	3m57.938s
sys	0m8.981s

That's 40% slower just for including a header!

Timmmm avatar Feb 22 '17 13:02 Timmmm

Help is wanted for this issue. I simply don't have the bandwidth to deal with it

gabime avatar Feb 22 '17 14:02 gabime

Is it maybe a option to use the DLL done by me? I like more to use a DLL, the only drawback I see is that variadic argument list are not supported by good old DLL binding.

vwbij avatar Feb 22 '17 15:02 vwbij

Is it maybe a option to use the DLL done by me

What about .so/.a in Unix?

gabime avatar Feb 22 '17 15:02 gabime

Clearly, I still sit on the island "windows". For me, this does not change, so my DLL works like a charme, and static librarys has some drawbacks. Ok, my DLL is just for people that wants to use it (only) under Windows...

vwbij avatar Feb 22 '17 15:02 vwbij