blob: 36a441176d73782af067b3c295af669408a66310 [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"
21#include "aos/logging/sizes.h"
22#include "aos/macros.h"
Austin Schuh82c0c822019-05-27 19:55:20 -070023#include "aos/time/time.h"
24#include "aos/type_traits/type_traits.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080025
Brian Silvermancb5da1f2015-12-05 22:19:58 -050026// This file has various concrete LogImplementations.
Brian Silvermanf665d692013-02-17 22:11:39 -080027
28namespace aos {
29namespace logging {
30
31// Unless explicitly stated otherwise, format must always be a string constant,
32// args are printf-style arguments for format, and ap is a va_list of args.
Brian Silverman1a572cc2013-03-05 19:58:01 -080033// The validity of format and args together will be checked at compile time
Brian Silvermancb5da1f2015-12-05 22:19:58 -050034// using a function attribute.
Brian Silvermanf665d692013-02-17 22:11:39 -080035
Brian Silvermancb5da1f2015-12-05 22:19:58 -050036// Contains all of the information about a given logging call.
Brian Silverman88471dc2014-02-15 22:35:42 -080037struct LogMessage {
Brian Silverman88471dc2014-02-15 22:35:42 -080038 int32_t seconds, nseconds;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070039 // message_length is just the length of the actual data (which member depends
40 // on the type).
Brian Silverman88471dc2014-02-15 22:35:42 -080041 size_t message_length, name_length;
Brian Silvermanf665d692013-02-17 22:11:39 -080042 pid_t source;
43 static_assert(sizeof(source) == 4, "that's how they get printed");
44 // Per task/thread.
45 uint16_t sequence;
46 log_level level;
Austin Schuh044e18b2015-10-21 20:17:09 -070047 char name[LOG_MESSAGE_NAME_LEN];
Austin Schuh56196432020-10-24 20:15:21 -070048 char message[LOG_MESSAGE_LEN];
Brian Silvermanf665d692013-02-17 22:11:39 -080049};
50static_assert(shm_ok<LogMessage>::value, "it's going in a queue");
51
52// Returns left > right. LOG_UNKNOWN is most important.
53static inline bool log_gt_important(log_level left, log_level right) {
54 if (left == ERROR) left = 3;
55 if (right == ERROR) right = 3;
56 return left > right;
57}
58
59// Returns a string representing level or "unknown".
60static inline const char *log_str(log_level level) {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080061#define DECL_LEVEL(name, value) \
62 if (level == name) return #name;
Brian Silvermanf665d692013-02-17 22:11:39 -080063 DECL_LEVELS;
64#undef DECL_LEVEL
65 return "unknown";
66}
67// Returns the log level represented by str or LOG_UNKNOWN.
68static inline log_level str_log(const char *str) {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080069#define DECL_LEVEL(name, value) \
70 if (!strcmp(str, #name)) return name;
Brian Silvermanf665d692013-02-17 22:11:39 -080071 DECL_LEVELS;
72#undef DECL_LEVEL
73 return LOG_UNKNOWN;
74}
75
Brian Silvermanbe858a12014-04-30 17:37:28 -070076// Implements all of the DoLog* methods in terms of a (pure virtual in this
77// class) HandleMessage method that takes a pointer to the message.
78class HandleMessageLogImplementation : public LogImplementation {
Austin Schuh82c0c822019-05-27 19:55:20 -070079 protected:
80 virtual ::aos::monotonic_clock::time_point monotonic_now() const {
81 return ::aos::monotonic_clock::now();
82 }
83
Brian Silvermancb5da1f2015-12-05 22:19:58 -050084 private:
Tyler Chatow5febd8f2020-01-05 18:25:31 -080085 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog(
86 log_level level, const char *format, va_list ap) override;
Brian Silvermanbe858a12014-04-30 17:37:28 -070087
88 virtual void HandleMessage(const LogMessage &message) = 0;
89};
90
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080091// A log implementation that dumps all messages to a C stdio stream.
Brian Silvermanbe858a12014-04-30 17:37:28 -070092class StreamLogImplementation : public HandleMessageLogImplementation {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -080093 public:
94 StreamLogImplementation(FILE *stream);
95
Austin Schuhad9e5eb2021-11-19 20:33:55 -080096 // Returns the name of this actual thread as the name.
97 std::string_view MyName() override {
98 internal::Context *context = internal::Context::Get();
99 return context->MyName();
100 }
101
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800102 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500103 void HandleMessage(const LogMessage &message) override;
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800104
105 FILE *const stream_;
106};
107
Austin Schuh56196432020-10-24 20:15:21 -0700108// Returns the current implementation.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700109std::shared_ptr<LogImplementation> GetImplementation();
Tyler Chatow4b471e12020-01-05 20:19:36 -0800110
Austin Schuha0c41ba2020-09-10 22:59:14 -0700111// Sets the current implementation.
112void SetImplementation(std::shared_ptr<LogImplementation> implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -0800113
Austin Schuh56196432020-10-24 20:15:21 -0700114// A logging implementation which just uses a callback.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700115class CallbackLogImplementation : public HandleMessageLogImplementation {
116 public:
117 CallbackLogImplementation(
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800118 const ::std::function<void(const LogMessage &)> &callback,
119 const std::string *name)
120 : callback_(callback), name_(name) {}
121
122 // Returns the provided name. This is most likely the event loop name.
123 std::string_view MyName() override { return *name_; }
Austin Schuha0c41ba2020-09-10 22:59:14 -0700124
125 private:
126 void HandleMessage(const LogMessage &message) override { callback_(message); }
127
128 ::std::function<void(const LogMessage &)> callback_;
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800129 const std::string *name_;
Austin Schuha0c41ba2020-09-10 22:59:14 -0700130};
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500131
Tyler Chatow67ddb032020-01-12 14:30:04 -0800132class ScopedLogRestorer {
133 public:
Austin Schuh56196432020-10-24 20:15:21 -0700134 ScopedLogRestorer() : prev_impl_(GetImplementation()) {}
135 ~ScopedLogRestorer() { SetImplementation(std::move(prev_impl_)); }
Austin Schuha0c41ba2020-09-10 22:59:14 -0700136
137 void Swap(std::shared_ptr<LogImplementation> new_impl) {
Austin Schuh56196432020-10-24 20:15:21 -0700138 SetImplementation(std::move(new_impl));
Austin Schuha0c41ba2020-09-10 22:59:14 -0700139 }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800140
141 private:
Austin Schuha0c41ba2020-09-10 22:59:14 -0700142 std::shared_ptr<LogImplementation> prev_impl_;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800143};
Tyler Chatow5febd8f2020-01-05 18:25:31 -0800144
Brian Silvermanf665d692013-02-17 22:11:39 -0800145// This is where all of the code that is only used by actual LogImplementations
146// goes.
147namespace internal {
148
Brian Silverman88471dc2014-02-15 22:35:42 -0800149// Fills in *message according to the given inputs (with type kString).
150// Used for implementing LogImplementation::DoLog.
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800151void FillInMessage(log_level level, std::string_view name,
Austin Schuh82c0c822019-05-27 19:55:20 -0700152 ::aos::monotonic_clock::time_point monotonic_now,
153 const char *format, va_list ap, LogMessage *message)
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800154 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 4, 0)));
Brian Silvermanf665d692013-02-17 22:11:39 -0800155
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800156__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 6))) static inline void
157FillInMessageVarargs(log_level level, std::string_view name,
Austin Schuh82c0c822019-05-27 19:55:20 -0700158 ::aos::monotonic_clock::time_point monotonic_now,
159 LogMessage *message, const char *format, ...) {
Brian Silverman78968542014-03-05 17:03:43 -0800160 va_list ap;
161 va_start(ap, format);
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800162 FillInMessage(level, name, monotonic_now, format, ap, message);
Brian Silverman78968542014-03-05 17:03:43 -0800163 va_end(ap);
164}
165
Brian Silvermanf665d692013-02-17 22:11:39 -0800166// Prints message to output.
167void PrintMessage(FILE *output, const LogMessage &message);
168
Brian Silvermanf665d692013-02-17 22:11:39 -0800169} // namespace internal
170} // namespace logging
171} // namespace aos
172
John Park33858a32018-09-28 23:05:48 -0700173#endif // AOS_LOGGING_IMPLEMENTATIONS_H_