36d31c530d
Previously, we would split messages by line and call the logger function for each line. We would hold a lock during this, to ensure that multiple threads would not interleave their messages. There are a few problems with this approach: 1) Using a lock is not efficient and is not fork safe 2) With APEX, there is one lock per instance of libbase, so we must move the lock to a location where all instances can access it, or perform the line splitting in a way that does not require the lock. To solve these issues, we reimagine line splitting. 1) We move the lock out of the LogMessage::~LogMessage() and make it the logger's responsibility to split lines, giving the logger the option to lock or not. 2) We do not need any locks at all for StderrLogger. Instead, we generate a single string that contains all of the lines with their appropriate log header. A single write() call is used to output this at once. 3) Logd handles log messages with newlines correctly, however it only accepts up to a maximum size of log message. Therefore we separate the incoming log message into chunks, delimited by new lines, up to that maximum size, and send each of those to logd. Note that this is the strategy used in android.util.Log.printlns(). This should solve a majority of use cases, since the maximum size that logd accepts is nearly 4K, while remaining lock free. If interleaving messages absolutely must be avoided, a lock can still be used given 1) above. Bug: 65062446 Bug: 153824050 Test: logging, particularly multi-line stack traces, show correctly Test: existing and new unit tests Change-Id: Id0cb5669bee7f912da1e17f7010f0ee4c93be1e3 |
||
---|---|---|
.. | ||
include/android-base | ||
tidy | ||
.clang-format | ||
abi_compatibility.cpp | ||
Android.bp | ||
chrono_utils.cpp | ||
chrono_utils_test.cpp | ||
cmsg.cpp | ||
cmsg_test.cpp | ||
CPPLINT.cfg | ||
endian_test.cpp | ||
errors_test.cpp | ||
errors_unix.cpp | ||
errors_windows.cpp | ||
expected_test.cpp | ||
file.cpp | ||
file_test.cpp | ||
format_benchmark.cpp | ||
liblog_symbols.cpp | ||
liblog_symbols.h | ||
logging.cpp | ||
logging_splitters.h | ||
logging_splitters_test.cpp | ||
logging_test.cpp | ||
macros_test.cpp | ||
mapped_file.cpp | ||
mapped_file_test.cpp | ||
no_destructor_test.cpp | ||
OWNERS | ||
parsebool.cpp | ||
parsebool_test.cpp | ||
parsedouble_test.cpp | ||
parseint_test.cpp | ||
parsenetaddress.cpp | ||
parsenetaddress_test.cpp | ||
process.cpp | ||
process_test.cpp | ||
properties.cpp | ||
properties_test.cpp | ||
README.md | ||
result_test.cpp | ||
scopeguard_test.cpp | ||
stringprintf.cpp | ||
stringprintf_test.cpp | ||
strings.cpp | ||
strings_test.cpp | ||
test_main.cpp | ||
test_utils.cpp | ||
test_utils_test.cpp | ||
threads.cpp | ||
utf8.cpp | ||
utf8_test.cpp |
libbase
Who is this library for?
This library is a collection of convenience functions to make common tasks easier and less error-prone.
In this context, "error-prone" covers both "hard to do correctly" and "hard to do with good performance", but as a general purpose library, libbase's primary focus is on making it easier to do things easily and correctly when a compromise has to be made between "simplest API" on the one hand and "fastest implementation" on the other. Though obviously the ideal is to have both.
Should my routine be added?
The intention is to cover the 80% use cases, not be all things to all users.
If you have a routine that's really useful in your project, congratulations. But that doesn't mean it should be here rather than just in your project.
The question for libbase is "should everyone be doing this?"/"does this make everyone's code cleaner/safer?". Historically we've considered the bar for inclusion to be "are there at least three unrelated projects that would be cleaned up by doing so".
If your routine is actually something from a future C++ standard (that isn't yet in libc++), or it's widely used in another library, that helps show that there's precedent. Being able to say "so-and-so has used this API for n years" is a good way to reduce concerns about API choices.
Any other restrictions?
Unlike most Android code, code in libbase has to build for Mac and Windows too.
Code here is also expected to have good test coverage.
By its nature, it's difficult to change libbase API. It's often best to start using your routine just in your project, and let it "graduate" after you're certain that the API is solid.