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

Add logging #331

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Add logging #331

wants to merge 19 commits into from

Conversation

psyomn
Copy link
Contributor

@psyomn psyomn commented Apr 24, 2021

Description of Change(s)

Adds a very primitive and simple way to log stuff. Currently I see we don't use a lot of qDebug
and friends, but maybe we can leverage around this in the future, if we need to debug some
sort of weird behavior.

Feel free to request any sort of changes -- I'd like to keep the style and thoughts as close to the rest
of the repo, and I feel I may have overlooked something.

Thanks!

Fixes Issue(s)

N/A

check logs exist on:

  • Windows
  • Mac
  • Linux

@cameronwhite
Copy link
Member

I think the additions look good so far! My main comment would be that we might want some logging in parts of the codebase that don't have a Qt dependency (e.g. looking for existing usage of std::cerr) so perhaps some of this should be split out and moved into a lower level library like Util?
There also is a boost logging library, but that's probably overkill for what we need :)

@psyomn
Copy link
Contributor Author

psyomn commented Apr 24, 2021

Ah good point! I'll check that out, and poke you again for a check, thanks!

@luzpaz
Copy link
Contributor

luzpaz commented Dec 5, 2023

Any traction here ?

@psyomn
Copy link
Contributor Author

psyomn commented Dec 6, 2023 via email

@psyomn psyomn force-pushed the feature/add-logging branch 7 times, most recently from 162ab39 to 3961ac8 Compare December 30, 2023 20:02
Copy link
Member

@cameronwhite cameronwhite left a comment

Choose a reason for hiding this comment

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

Looks great overall so far! Just left a few miscellaneous comments

source/app/powertabeditor.cpp Outdated Show resolved Hide resolved
@@ -260,14 +261,14 @@ void PowerTabEditor::openFile(QString filename)
int validationResult = myDocumentManager->findDocument(path);
if (validationResult > -1)
{
qDebug() << "File: " << filename << " is already open";
Log::d("file: {} is already open", filename.toStdString());
Copy link
Member

Choose a reason for hiding this comment

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

One possible extension would be adding format support for QString (e.g. https://www.cppstories.com/2022/custom-stdformat-cpp20/#single-values) to avoid the toStdString() calls

This could go in an <app/log.h> wrapper which adds Qt-specific stuff that doesn't belong in the core util library

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I tried this out but I'm still getting compilation errors. I'm assuming that I should include this (app/log.h) after including util/log.h:

template <>
struct std::formatter<QString> : std::formatter<std::string>
{
    auto format(const QString& qstr, std::format_context& ctx) const {
        return std::formatter<std::string>(qstr.toStdString(), ctx);
    }
};

This gives me a compilation error; I'm going to go over this this weekend if I have the time.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, maybe it needs to be in the fmt namespace rather than std to work with the fmt library?

I'd also be fine having app/log.h include util/log.h so that code in the UI libraries just needs to include one header to get access to logging plus the Qt format conversions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I just realized that I basically forgot to rename the guard of the previous app/log.h in util/log.h. This resulted into a few hours of insanity of why do these two headers flip each other off or on (haha).

Got this to work!

Copy link
Member

Choose a reason for hiding this comment

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

Great!

source/app/powertabeditor.cpp Outdated Show resolved Hide resolved
source/util/CMakeLists.txt Outdated Show resolved Hide resolved
test/CMakeLists.txt Outdated Show resolved Hide resolved
#ifndef APP_LOG_H
#define APP_LOG_H

#include <chrono>
Copy link
Member

Choose a reason for hiding this comment

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

To avoid pulling in a bunch of includes in the header, one thought is that the backend function could just take the final formatted string (e.g. call vformat() beforehand), and then it can just go in the .cpp file since it doesn't need to be templated. Then most of the related members like the mutex don't need to be accessible in the header

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed this too! I have to AFK for a bit, but once I'm back, I'll make sure that this works on windows too and tick that.

@psyomn psyomn force-pushed the feature/add-logging branch 3 times, most recently from 4942db1 to 387f58b Compare January 5, 2024 03:32
Partially inspired by rfc5424, along with a few other idioms I picked up
along the way.  This should allow one to tweak the log level by setting
a minimum log level in the `Log' namespace.  Right now I've set the log
level to debug in the main entry point.
I'm leaving this commit here instead of squashing, so that in the
future, we could technically just reverse this commit.
@psyomn psyomn force-pushed the feature/add-logging branch 2 times, most recently from 12318dd to e11f0b4 Compare January 7, 2024 22:06
This implements a very simple circular buffer that will take the last N
lines in a log file, and keep those instead.  Right now the maximum N is
hardcoded, but the code is written in a fashion that we can revisit some
of this, and add toggles where we see fit (for example in a settings
dialog or so).

I tested out the functionality this way:

        $ yes some-log-line | nl | head -5000 > log.txt

And then running PTE2 with a max log size of 1000, I noticed the logfile
like so:

        $ head -10 log.txt
        4001  some-log-line
        4002  some-log-line
        4003  some-log-line
        4004  some-log-line
        4005  some-log-line
        4006  some-log-line
        4007  some-log-line
        4008  some-log-line
        4009  some-log-line
        4010  some-log-line

and the bottom:

        $ tail -10 log.txt
        2023-12-30T14:38:32Z: [debug]: finding translations for locale:
        2023-12-30T14:38:32Z: [debug]:   locale: en-US
        2023-12-30T14:38:32Z: [debug]:   locale: en
        2023-12-30T14:38:32Z: [debug]:   locale: en-Latn-US
        2023-12-30T14:38:32Z: [debug]:   - checking: /home/psyomn/.local/share/powertab/powertabeditor/translations
        2023-12-30T14:38:32Z: [debug]:   - checking: /usr/local/share/powertab/powertabeditor/translations
        2023-12-30T14:38:32Z: [debug]:   - checking: /usr/share/powertab/powertabeditor/translations
        2023-12-30T14:38:32Z: [debug]:   - checking: /home/psyomn/programming/cc/fork/powertabeditor/build/bin/data/translations
        2023-12-30T14:38:32Z: [debug]:   - checking: /usr/share/qt/translations
        2023-12-30T14:38:32Z: [debug]: loaded qt base translations from /usr/share/qt/translations/qtbase_en.qm

and last checks:

        $ nl log.txt | head -1
             1    4001  some-log-line
        $ nl log.txt | tail -1
          1011  2023-12-30T14:38:32Z: [debug]: loaded qt base translations from /usr/share/qt/translations/qtbase_en.qm
This adds a custom format wrapper for QString objects, so that they can
be logged directly.
@psyomn
Copy link
Contributor Author

psyomn commented Jan 8, 2024

Windows: so I'm getting radio silence from logs on windows, and I don't get it. I'm guessing stdout/stderr don't emit like in sane, normal, programming environments too, so I really can't gauge where exactly the fail is happening. I've read here and there that on Windows GUI programs can actually mute these unless you > them to files (but that didn't work either). I'll revisit this next weekend and see what's up.

Linux works fine.

@cameronwhite
Copy link
Member

Yeah I think that's probably the expected behaviour and is kinda tricky to change: e.g. https://stackoverflow.com/questions/3360548/console-output-in-a-qt-gui-app

IIRC, with qDebug() the behaviour was the same, but the logs did show up in the Visual Studio debugger. I think it might be using a Windows API like OutputDebugString()

@psyomn
Copy link
Contributor Author

psyomn commented Jan 8, 2024 via email

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.

3 participants