blob: 4e740f5f3482bd5b3ca174a3c5270957fafdfc77 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_LOGGING_IMPLEMENTATIONS_H_
2#define AOS_LOGGING_IMPLEMENTATIONS_H_
Brian Silvermanf665d692013-02-17 22:11:39 -08003
4#include <sys/types.h>
5#include <unistd.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08006
Brian Silverman6da04272014-05-18 18:47:48 -07007#include <atomic>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008#include <climits>
9#include <cstdarg>
10#include <cstdint>
11#include <cstdio>
12#include <cstring>
Tyler Chatow5febd8f2020-01-05 18:25:31 -080013#include <functional>
Austin Schuha0c41ba2020-09-10 22:59:14 -070014#include <memory>
Tyler Chatow5febd8f2020-01-05 18:25:31 -080015#include <string>
Austin Schuhad9e5eb2021-11-19 20:33:55 -080016#include <string_view>
Brian Silvermanf665d692013-02-17 22:11:39 -080017
John Park33858a32018-09-28 23:05:48 -070018#include "aos/logging/context.h"
19#include "aos/logging/interface.h"
20#include "aos/logging/logging.h"
John Park33858a32018-09-28 23:05:48 -070021#include "aos/macros.h"
Austin Schuh82c0c822019-05-27 19:55:20 -070022#include "aos/time/time.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080023
Brian Silvermancb5da1f2015-12-05 22:19:58 -050024// This file has various concrete LogImplementations.
Brian Silvermanf665d692013-02-17 22:11:39 -080025
26namespace aos {
27namespace logging {
28
29// Unless explicitly stated otherwise, format must always be a string constant,
30// args are printf-style arguments for format, and ap is a va_list of args.
Brian Silverman1a572cc2013-03-05 19:58:01 -080031// The validity of format and args together will be checked at compile time
Brian Silvermancb5da1f2015-12-05 22:19:58 -050032// using a function attribute.
Brian Silvermanf665d692013-02-17 22:11:39 -080033
Brian Silvermancb5da1f2015-12-05 22:19:58 -050034// Contains all of the information about a given logging call.
Brian Silverman88471dc2014-02-15 22:35:42 -080035struct LogMessage {
Brian Silverman88471dc2014-02-15 22:35:42 -080036 int32_t seconds, nseconds;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070037 // message_length is just the length of the actual data (which member depends
38 // on the type).
Brian Silverman88471dc2014-02-15 22:35:42 -080039 size_t message_length, name_length;
Brian Silvermanf665d692013-02-17 22:11:39 -080040 pid_t source;
41 static_assert(sizeof(source) == 4, "that's how they get printed");
42 // Per task/thread.
43 uint16_t sequence;
44 log_level level;
Austin Schuh044e18b2015-10-21 20:17:09 -070045 char name[LOG_MESSAGE_NAME_LEN];
Austin Schuh56196432020-10-24 20:15:21 -070046 char message[LOG_MESSAGE_LEN];
Brian Silvermanf665d692013-02-17 22:11:39 -080047};
Brian Silvermanf665d692013-02-17 22:11:39 -080048
49// Returns left > right. LOG_UNKNOWN is most important.
50static inline bool log_gt_important(log_level left, log_level right) {
51 if (left == ERROR) left = 3;
52 if (right == ERROR) right = 3;
53 return left > right;
54}
55
56// Returns a string representing level or "unknown".
57static inline const char *log_str(log_level level) {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080058#define DECL_LEVEL(name, value) \
59 if (level == name) return #name;
Brian Silvermanf665d692013-02-17 22:11:39 -080060 DECL_LEVELS;
61#undef DECL_LEVEL
62 return "unknown";
63}
64// Returns the log level represented by str or LOG_UNKNOWN.
65static inline log_level str_log(const char *str) {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080066#define DECL_LEVEL(name, value) \
67 if (!strcmp(str, #name)) return name;
Brian Silvermanf665d692013-02-17 22:11:39 -080068 DECL_LEVELS;
69#undef DECL_LEVEL
70 return LOG_UNKNOWN;
71}
72
Brian Silvermanbe858a12014-04-30 17:37:28 -070073// Implements all of the DoLog* methods in terms of a (pure virtual in this
74// class) HandleMessage method that takes a pointer to the message.
75class HandleMessageLogImplementation : public LogImplementation {
Austin Schuh82c0c822019-05-27 19:55:20 -070076 protected:
77 virtual ::aos::monotonic_clock::time_point monotonic_now() const {
78 return ::aos::monotonic_clock::now();
79 }
80
Brian Silvermancb5da1f2015-12-05 22:19:58 -050081 private:
Tyler Chatow5febd8f2020-01-05 18:25:31 -080082 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog(
83 log_level level, const char *format, va_list ap) override;
Brian Silvermanbe858a12014-04-30 17:37:28 -070084
85 virtual void HandleMessage(const LogMessage &message) = 0;
86};
87
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080088// A log implementation that dumps all messages to a C stdio stream.
Brian Silvermanbe858a12014-04-30 17:37:28 -070089class StreamLogImplementation : public HandleMessageLogImplementation {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080090 public:
91 StreamLogImplementation(FILE *stream);
92
Austin Schuhad9e5eb2021-11-19 20:33:55 -080093 // Returns the name of this actual thread as the name.
94 std::string_view MyName() override {
95 internal::Context *context = internal::Context::Get();
96 return context->MyName();
97 }
98
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080099 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500100 void HandleMessage(const LogMessage &message) override;
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800101
102 FILE *const stream_;
103};
104
Austin Schuh56196432020-10-24 20:15:21 -0700105// Returns the current implementation.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700106std::shared_ptr<LogImplementation> GetImplementation();
Tyler Chatow4b471e12020-01-05 20:19:36 -0800107
Austin Schuha0c41ba2020-09-10 22:59:14 -0700108// Sets the current implementation.
109void SetImplementation(std::shared_ptr<LogImplementation> implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -0800110
Austin Schuh56196432020-10-24 20:15:21 -0700111// A logging implementation which just uses a callback.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700112class CallbackLogImplementation : public HandleMessageLogImplementation {
113 public:
114 CallbackLogImplementation(
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800115 const ::std::function<void(const LogMessage &)> &callback,
116 const std::string *name)
117 : callback_(callback), name_(name) {}
118
119 // Returns the provided name. This is most likely the event loop name.
120 std::string_view MyName() override { return *name_; }
Austin Schuha0c41ba2020-09-10 22:59:14 -0700121
122 private:
123 void HandleMessage(const LogMessage &message) override { callback_(message); }
124
125 ::std::function<void(const LogMessage &)> callback_;
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800126 const std::string *name_;
Austin Schuha0c41ba2020-09-10 22:59:14 -0700127};
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500128
Tyler Chatow67ddb032020-01-12 14:30:04 -0800129class ScopedLogRestorer {
130 public:
Austin Schuh56196432020-10-24 20:15:21 -0700131 ScopedLogRestorer() : prev_impl_(GetImplementation()) {}
132 ~ScopedLogRestorer() { SetImplementation(std::move(prev_impl_)); }
Austin Schuha0c41ba2020-09-10 22:59:14 -0700133
134 void Swap(std::shared_ptr<LogImplementation> new_impl) {
Austin Schuh56196432020-10-24 20:15:21 -0700135 SetImplementation(std::move(new_impl));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700136 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800137
138 private:
Austin Schuha0c41ba2020-09-10 22:59:14 -0700139 std::shared_ptr<LogImplementation> prev_impl_;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800140};
Tyler Chatow5febd8f2020-01-05 18:25:31 -0800141
Brian Silvermanf665d692013-02-17 22:11:39 -0800142// This is where all of the code that is only used by actual LogImplementations
143// goes.
144namespace internal {
145
Brian Silverman88471dc2014-02-15 22:35:42 -0800146// Fills in *message according to the given inputs (with type kString).
147// Used for implementing LogImplementation::DoLog.
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800148void FillInMessage(log_level level, std::string_view name,
Austin Schuh82c0c822019-05-27 19:55:20 -0700149 ::aos::monotonic_clock::time_point monotonic_now,
150 const char *format, va_list ap, LogMessage *message)
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800151 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 4, 0)));
Brian Silvermanf665d692013-02-17 22:11:39 -0800152
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800153__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 6))) static inline void
154FillInMessageVarargs(log_level level, std::string_view name,
Austin Schuh82c0c822019-05-27 19:55:20 -0700155 ::aos::monotonic_clock::time_point monotonic_now,
156 LogMessage *message, const char *format, ...) {
Brian Silverman78968542014-03-05 17:03:43 -0800157 va_list ap;
158 va_start(ap, format);
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800159 FillInMessage(level, name, monotonic_now, format, ap, message);
Brian Silverman78968542014-03-05 17:03:43 -0800160 va_end(ap);
161}
162
Brian Silvermanf665d692013-02-17 22:11:39 -0800163// Prints message to output.
164void PrintMessage(FILE *output, const LogMessage &message);
165
Brian Silvermanf665d692013-02-17 22:11:39 -0800166} // namespace internal
167} // namespace logging
168} // namespace aos
169
John Park33858a32018-09-28 23:05:48 -0700170#endif // AOS_LOGGING_IMPLEMENTATIONS_H_