emilk / loguru

A lightweight C++ logging library
The Unlicense
1.79k stars 260 forks source link

Cannot open file while app is running (Windows VS2017) #126

Closed greenkalx closed 2 years ago

greenkalx commented 4 years ago

Hi Emil!

I could not open the file while my app is running. I want to run a tail to see updates.

Debugging in Visual Studio 2017. In my main() I added:

loguru::add_file("c:\myVerboseLog.log", loguru::Append, loguru::Verbosity_MAX); .. LOG_F(INFO, "thread='%d'", threadId);

It does write to the log file though.

I saw your imgui software renderer project too, that could be useful! And a simple coroutines!

Thank you!

lkho commented 4 years ago

I think we can change the fopen_s call for win32 to something like _fsopen to enable shared access? or will it be better if the library provide an overload accepting an already opened fd? I came across this since I found it is quite cumbersome to re-implement the whole add_file function when I just want to change the file open call.

change this line: https://github.com/emilk/loguru/blob/2e1424d575e276e010c072967c0672863c623cbf/loguru.cpp#L751-L752 to:

file = _fsopen(path, mode_str, _SH_DENYNO);
if (!file) {
jfftonsic commented 3 years ago

Did what @lkho suggested here and it worked. Not being able to open the file while the program is running makes little sense. Thanks.

ArsMasiuk commented 2 years ago

Thank you @lkho , this helped me too :)