hrydgard / minitrace

Simple C/C++ library for producing JSON traces suitable for Chrome's built-in trace viewer (about:tracing).
MIT License
373 stars 64 forks source link

Using minitrace in dynamic libraries on Windows #33

Open lukastvrdy opened 1 year ago

lukastvrdy commented 1 year ago

Hi,

Problem: Using minitrace from multiple dynamic libraries and log into the same trace file.

when I try to use minitrace on Windows project which has minitrace compiled as static library and I link minitrace static library to dynamic library foo.dll, bar.dll and baz.dll, the call to lock the mutex (global variable in minitrace.c) is crashing since the mutex is global variable and every dll sees different copy of that mutex variable and thus it does not see initialized mutex (I call mtr_init() only in AppMain.cpp which is linked into App.exe)

But the goal is that every dll in the project traces to the same file stream (calling mtr_init is not solution since it will print out header to the file).

Is there some elegant solution to this problem?

One solution is to create some dynamic library wrapper around minitrace static lib, but that it is a bit involved.

Is there some elegant solution to this problem?

hrydgard commented 1 year ago

Hi,

I don't have any good solution for this. One way would be to change minitrace all over to take a context object that can own the mutex, but that'd make everything a lot less appealing to use...