blob: 326f28eee1163069b01002b6099e9b2d83562758 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/logging/interface.h"
Brian Silvermanb0893882014-02-10 14:48:30 -08002
Stephan Pleines0960c262024-05-31 20:29:24 -07003#include <algorithm>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdarg>
5#include <cstdio>
6#include <cstring>
Stephan Pleines0960c262024-05-31 20:29:24 -07007#include <memory>
8#include <ostream>
9#include <string>
Tyler Chatow4b471e12020-01-05 20:19:36 -080010#include <type_traits>
Brian Silverman88471dc2014-02-15 22:35:42 -080011
Philipp Schrader790cb542023-07-05 21:06:52 -070012#include "glog/logging.h"
13
John Park33858a32018-09-28 23:05:48 -070014#include "aos/die.h"
15#include "aos/logging/context.h"
Austin Schuh56196432020-10-24 20:15:21 -070016#include "aos/logging/implementations.h"
Stephan Pleines0960c262024-05-31 20:29:24 -070017#include "aos/time/time.h"
Brian Silvermanb0893882014-02-10 14:48:30 -080018
Stephan Pleinesf63bde82024-01-13 15:59:33 -080019namespace aos::logging {
Brian Silvermanb0893882014-02-10 14:48:30 -080020namespace internal {
21
Brian Silverman88471dc2014-02-15 22:35:42 -080022size_t ExecuteFormat(char *output, size_t output_size, const char *format,
23 va_list ap) {
24 static const char *const continued = "...\n";
Brian Silvermanb0893882014-02-10 14:48:30 -080025 const size_t size = output_size - strlen(continued);
26 const int ret = vsnprintf(output, size, format, ap);
Alex Perrycb7da4b2019-08-28 19:35:56 -070027 typedef ::std::common_type<int, size_t>::type RetType;
Brian Silvermanb0893882014-02-10 14:48:30 -080028 if (ret < 0) {
Austin Schuh56196432020-10-24 20:15:21 -070029 PLOG(FATAL) << "vsnprintf(" << output << ", " << size << ", " << format
30 << ", args) failed";
Brian Silverman88471dc2014-02-15 22:35:42 -080031 } else if (static_cast<RetType>(ret) >= static_cast<RetType>(size)) {
Brian Silvermanb0893882014-02-10 14:48:30 -080032 // Overwrite the '\0' at the end of the existing data and
33 // copy in the one on the end of continued.
34 memcpy(&output[size - 1], continued, strlen(continued) + 1);
35 }
Brian Silverman88471dc2014-02-15 22:35:42 -080036 return ::std::min<RetType>(ret, size);
Brian Silvermanb0893882014-02-10 14:48:30 -080037}
38
Brian Silvermand6974f42014-02-14 13:39:21 -080039} // namespace internal
40
41using internal::Context;
42
Brian Silvermanb0893882014-02-10 14:48:30 -080043void VLog(log_level level, const char *format, va_list ap) {
Austin Schuh56196432020-10-24 20:15:21 -070044 va_list ap1;
45 va_copy(ap1, ap);
46
47 Context *context = Context::Get();
48
49 const std::shared_ptr<LogImplementation> implementation =
50 context->implementation;
51 // Log to the implementation if we have it, and stderr as a backup.
52 if (implementation) {
53 implementation->DoLog(level, format, ap1);
54 } else {
55 aos::logging::LogMessage message;
Austin Schuhad9e5eb2021-11-19 20:33:55 -080056 aos::logging::internal::FillInMessage(level, context->MyName(),
57 aos::monotonic_clock::now(), format,
58 ap, &message);
Austin Schuh56196432020-10-24 20:15:21 -070059 aos::logging::internal::PrintMessage(stderr, message);
60 }
61 va_end(ap1);
62
63 if (level == FATAL) {
64 VDie(format, ap);
65 }
Brian Silvermanb0893882014-02-10 14:48:30 -080066}
67
Stephan Pleinesf63bde82024-01-13 15:59:33 -080068} // namespace aos::logging
Brian Silvermanb0893882014-02-10 14:48:30 -080069
70void log_do(log_level level, const char *format, ...) {
71 va_list ap;
72 va_start(ap, format);
73 aos::logging::VLog(level, format, ap);
74 va_end(ap);
75}