John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/logging/interface.h" |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 2 | |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 3 | #include <algorithm> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdarg> |
| 5 | #include <cstdio> |
| 6 | #include <cstring> |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <ostream> |
| 9 | #include <string> |
Tyler Chatow | 4b471e1 | 2020-01-05 20:19:36 -0800 | [diff] [blame] | 10 | #include <type_traits> |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 12 | #include "absl/log/check.h" |
| 13 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 14 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 15 | #include "aos/die.h" |
| 16 | #include "aos/logging/context.h" |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 17 | #include "aos/logging/implementations.h" |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 18 | #include "aos/time/time.h" |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 19 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 20 | namespace aos::logging { |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 21 | namespace internal { |
| 22 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 23 | size_t ExecuteFormat(char *output, size_t output_size, const char *format, |
| 24 | va_list ap) { |
| 25 | static const char *const continued = "...\n"; |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 26 | const size_t size = output_size - strlen(continued); |
| 27 | const int ret = vsnprintf(output, size, format, ap); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | typedef ::std::common_type<int, size_t>::type RetType; |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 29 | if (ret < 0) { |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 30 | PLOG(FATAL) << "vsnprintf(" << output << ", " << size << ", " << format |
| 31 | << ", args) failed"; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 32 | } else if (static_cast<RetType>(ret) >= static_cast<RetType>(size)) { |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 33 | // Overwrite the '\0' at the end of the existing data and |
| 34 | // copy in the one on the end of continued. |
| 35 | memcpy(&output[size - 1], continued, strlen(continued) + 1); |
| 36 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 37 | return ::std::min<RetType>(ret, size); |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 40 | } // namespace internal |
| 41 | |
| 42 | using internal::Context; |
| 43 | |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 44 | void VLog(log_level level, const char *format, va_list ap) { |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 45 | va_list ap1; |
| 46 | va_copy(ap1, ap); |
| 47 | |
| 48 | Context *context = Context::Get(); |
| 49 | |
| 50 | const std::shared_ptr<LogImplementation> implementation = |
| 51 | context->implementation; |
| 52 | // Log to the implementation if we have it, and stderr as a backup. |
| 53 | if (implementation) { |
| 54 | implementation->DoLog(level, format, ap1); |
| 55 | } else { |
| 56 | aos::logging::LogMessage message; |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 57 | aos::logging::internal::FillInMessage(level, context->MyName(), |
| 58 | aos::monotonic_clock::now(), format, |
| 59 | ap, &message); |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 60 | aos::logging::internal::PrintMessage(stderr, message); |
| 61 | } |
| 62 | va_end(ap1); |
| 63 | |
| 64 | if (level == FATAL) { |
| 65 | VDie(format, ap); |
| 66 | } |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 69 | } // namespace aos::logging |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 70 | |
| 71 | void log_do(log_level level, const char *format, ...) { |
| 72 | va_list ap; |
| 73 | va_start(ap, format); |
| 74 | aos::logging::VLog(level, format, ap); |
| 75 | va_end(ap); |
| 76 | } |