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

New logging API #2194

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

New logging API #2194

wants to merge 15 commits into from

Conversation

snej
Copy link
Collaborator

@snej snej commented Dec 16, 2024

API:

  • Implemented new C4LogObserver API as described in the design doc.
  • It was necessary to change some includes: c4Base.h no longer includes c4Log.h, it's now the other way around. Platform code may need to add some explicit #include c4log.h statements.
  • The existing API for logging callbacks and files still exists, but should eventually be deprecated / removed.
  • There is no logging by default: something has to register an observer. c4log_initConsole() adds an observer that logs to the console. CppTests and C4Tests call it at startup.

Implementation:

  • Lots of refactoring of logging code. New class LogObserver is used to receive log messages.
  • File and callback logging are implemented as subclasses LogFiles and LogCallback.

Review notes:

  • The first three commits are just some preparations, moving the c4log code and moving the logging files to a new dir.
  • "Implemented LogObserver. Refactored logging code." is the internal implementation, adding the LogObserver class and moving the file and callback logging code into implementations of it.
  • "Implemented C4LogObserver [API]" implements the C API for LogObserver.
  • "Heavily refactored LogFiles" is where I succumbed to the unnecessary task of cleaning up the file logging code. I merged the three parallel arrays (of file streams, log encoders and rotation counts) into an array of a new LogFile class, and most of the logic went into the new class.
  • "Reimplemented old c4log API using new one" simplifies the implementation of the old C file/callback log APIs by calling the C LogObserver APIs.
  • "c4Tests' logging stub should preserve the domain" just fixes a small mistake in the C4Tests logging glue: it should preserve the log domain instead of routing the logs to the default logger. This also eliminates the lint warning that the method could be made static.

@snej snej force-pushed the feature/log-observer branch 7 times, most recently from 5d98e1e to b081c30 Compare December 18, 2024 20:33
@cbl-bot
Copy link

cbl-bot commented Dec 18, 2024

Code Coverage Results:

Type Percentage
branches 66.32
functions 78.59
instantiations 70.81
lines 77.61
regions 73.38

snej added 11 commits January 7, 2025 11:41
- c4Log.h now includes c4Base.h, not the other way around
  (this is because the new log API requires C4Timestamp)
- Added includes to sources files that now need it
- Moved c4Log implementation out of c4Base.cc to new c4Log.cc
Old API is still present but "semi-deprecated".
The code got a lot cleaner when I replaced the three parallel arrays
with a class `LogFile`.
@snej snej force-pushed the feature/log-observer branch from 7d5d573 to ba1b80c Compare January 7, 2025 19:54
The logging code no longer has to create and tear down a stringstream
every time.
@snej snej force-pushed the feature/log-observer branch from ba1b80c to 3a65c31 Compare January 8, 2025 18:22
snej added 2 commits January 10, 2025 10:02
It was possible for the timestamps to differ by more than the last
3 digits, if the timestamp inside the file was written just after a
new second started; i.e. the filename was constructed at time
12345678999 and the first log was written at 12345679001.
Apparently on Windows "%p" does not print a "0x" before the address.
@snej snej force-pushed the feature/log-observer branch from e91f1f9 to 0b723fc Compare January 10, 2025 21:20
@snej
Copy link
Collaborator Author

snej commented Jan 13, 2025

The Windows build failed in Jenkins, but I can't find an error anywhere in the full build log -- just thousands of linker warnings. Any idea, @borrrden ? It just ends with:

...
[2025-01-10T21:45:17.734Z] LINK : warning LNK4286: symbol 'kC4Cpp_DefaultLog' defined in 'DefaultLogger.obj' is imported by 'ThreadedMailbox.obj' [C:\Jenkins\workspace\line_couchbase-lite-core_PR-2194\couchbase-lite-core\build_cmake\x64\LiteCore\tests\CppTests.vcxproj]
[2025-01-10T21:45:19.287Z]   CppTests.vcxproj -> C:\Jenkins\workspace\line_couchbase-lite-core_PR-2194\couchbase-lite-core\build_cmake\x64\LiteCore\tests\Debug\CppTests.exe
[2025-01-10T21:45:19.287Z]   Building Custom Rule C:/Jenkins/workspace/line_couchbase-lite-core_PR-2194/couchbase-lite-core/CMakeLists.txt
[2025-01-10T21:45:20.007Z] C++ tests failed!

}

void c4log_removeObserver(C4LogObserver* observer) noexcept {
LogObserver::remove(reinterpret_cast<LogObserver*>(observer));
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the reinterpret_cast "toInternal?"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, it could be changed to a call to toInternal().

#pragma mark - INITIALIZATION:

LogDomain::LogDomain(const char* name, LogLevel level, bool internName)
: _level(level), _name(internName ? strdup(name) : name), _observers(new LogObservers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

where is "strdup(name)" freed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It isn't! Log domains are never freed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then, we probably don't need the extra argument, internName.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Most log domains are declared as globals and have names that are string constants. Those don't need to be copied. The internName flag is only for domains created dynamically via the C API.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then, perhaps strdup at the place where C API is called.

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.

4 participants