Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ColorCoutSink for WIN32 #132

Merged
merged 4 commits into from
Nov 30, 2022
Merged

Update ColorCoutSink for WIN32 #132

merged 4 commits into from
Nov 30, 2022

Conversation

lakor64
Copy link
Contributor

@lakor64 lakor64 commented Nov 29, 2022

The following PR adds support of the Color sink for WIN32 consoles (by using WIN32 api), I have also enabled the test for Windows.

A preview of how it looks like can be seen here: https://i.imgur.com/YJ3BC6n.png

Notes:

  • I'm not 100% sure if it supposed to show one white and one red (judging from the comment in the example it seems yes?)
  • I've done some other changes of this file (which I copied it from my own local fork) specifically for log filters like the logrotate example, I could submit a PR with that code as well if you are interested in merging such funcionality into this sink (it's actually more or less a copy of what you did in the LogRotateWIthFilters sink)

Copy link
Owner

@KjellKod KjellKod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI is updated now and it'll run the colored output snippet if it exists. Looking forward to seeing your update and the CI clearing it.

#ifdef _WIN32

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to be used. please remove or explain the purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is to avoid extra inclusions to speed up the compilation, see https://learn.microsoft.com/en-us/windows/win32/winprog/using-the-windows-headers

LOG(FATAL) << "This call will exit the example. A failed CHECK call, \n"
<< "a LOG(FATAL) or a fatal crash (seg fault or similar) will show up in RED\n"
<< "\n\nThis call will also be DUPLICATED. One FATAL LOG message will be shown through the \n"
<< "the crash handler and sent to cout, the LOG call will asynchronously send the LOG output to cout.\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only formatting difference. It's only a snippet and the change doesn't have a big impact so I'm fine with the formatting changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ops the formatting wasn't supposed to be pushed, the code is that it has a missing ";" so that's why the line was modified. I can keep it that way if you want.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the observation and fix

struct ColorCoutSink {
#ifdef _WIN32
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a snippet I think this is fine. For more production-type code I'd suggest avoiding #ifdef separated like the plague. A much cleaner approach is to do something like

#ifdef _WIN32
#include <ColoredCoutSinkWindowsImpl.h>
#else 
.... this file ... or include <ColoredCoutSinkNixImpl.h>

You can consider this but in case you don't want to spend time on cleaning it up, I think that is fine

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e. the approach would potentially add two .h files more but each file and both Nix and Wind code would stay clean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could make two different header files if you want to, originally it was supposed to have the ifdefs because I also added the filter code inside the ColorCoutSink (much like LogRotateWithFilter), hence why it was easier for me to have the ifdef.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, if you don't mind. Either two header files or one file but keeping all the content separate.
it's a headache to have to read #ifdef's inside functions

struct ColorCoutSink {
#ifdef _WIN32
ColorCoutSink() : hHandle_(GetStdHandle(STD_OUTPUT_HANDLE)) {}
Copy link
Owner

@KjellKod KjellKod Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's annoying that the WriteConsole output is completely hidden in the console
Maybe an artifact of the appveyor?

Do you know if it's possible to see this displayed to cout?
https://ci.appveyor.com/project/KjellKod/g3sinks/builds/45527002

@lakor64
Copy link
Contributor Author

lakor64 commented Nov 29, 2022

I've now divided the two sinks into two different files as discussed in the comments.
I've also changed the WriteConsole function which writes directly to the console buffer into the normal cout, this would allow AppVeyor to, at least, show the command line (I have tested that this change doesn't seem to create any regression), but I'm unsure if AppVeyor would actually show it colored because it has to deal/intercept WINAPIs and I'm not familiar with such CI.

Copy link
Owner

@KjellKod KjellKod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@KjellKod KjellKod merged commit aee2a63 into KjellKod:master Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants