blob: 0e171658fb2c59c3a2fa514174e63d81d170076d [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
Tyler Chatow5febd8f2020-01-05 18:25:31 -08004#include <limits.h>
5#include <stdarg.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <string.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08009#include <sys/types.h>
10#include <unistd.h>
Brian Silvermanf665d692013-02-17 22:11:39 -080011
Brian Silverman6da04272014-05-18 18:47:48 -070012#include <atomic>
Tyler Chatow5febd8f2020-01-05 18:25:31 -080013#include <functional>
14#include <string>
Brian Silvermanf665d692013-02-17 22:11:39 -080015
John Park33858a32018-09-28 23:05:48 -070016#include "aos/logging/context.h"
17#include "aos/logging/interface.h"
18#include "aos/logging/logging.h"
19#include "aos/logging/sizes.h"
20#include "aos/macros.h"
21#include "aos/mutex/mutex.h"
Sabina Davis2ed5ea22017-09-26 22:27:42 -070022#include "aos/once.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 {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080038 enum class Type : uint8_t { kString };
Brian Silverman88471dc2014-02-15 22:35:42 -080039
40 int32_t seconds, nseconds;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070041 // message_length is just the length of the actual data (which member depends
42 // on the type).
Brian Silverman88471dc2014-02-15 22:35:42 -080043 size_t message_length, name_length;
Brian Silvermanf665d692013-02-17 22:11:39 -080044 pid_t source;
45 static_assert(sizeof(source) == 4, "that's how they get printed");
46 // Per task/thread.
47 uint16_t sequence;
Brian Silverman88471dc2014-02-15 22:35:42 -080048 Type type;
Brian Silvermanf665d692013-02-17 22:11:39 -080049 log_level level;
Austin Schuh044e18b2015-10-21 20:17:09 -070050 char name[LOG_MESSAGE_NAME_LEN];
Brian Silverman88471dc2014-02-15 22:35:42 -080051 union {
52 char message[LOG_MESSAGE_LEN];
53 struct {
54 uint32_t type_id;
55 size_t string_length;
56 // The message string and then the serialized structure.
57 char serialized[LOG_MESSAGE_LEN - sizeof(type) - sizeof(string_length)];
58 } structure;
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080059 struct {
60 // The type ID of the element type.
61 uint32_t type;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070062 int rows, cols;
63 size_t string_length;
64 // The message string and then the serialized matrix.
Tyler Chatow5febd8f2020-01-05 18:25:31 -080065 char data[LOG_MESSAGE_LEN - sizeof(type) - sizeof(rows) - sizeof(cols)];
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080066 } matrix;
Brian Silverman88471dc2014-02-15 22:35:42 -080067 };
Brian Silvermanf665d692013-02-17 22:11:39 -080068};
69static_assert(shm_ok<LogMessage>::value, "it's going in a queue");
70
71// Returns left > right. LOG_UNKNOWN is most important.
72static inline bool log_gt_important(log_level left, log_level right) {
73 if (left == ERROR) left = 3;
74 if (right == ERROR) right = 3;
75 return left > right;
76}
77
78// Returns a string representing level or "unknown".
79static inline const char *log_str(log_level level) {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080080#define DECL_LEVEL(name, value) \
81 if (level == name) return #name;
Brian Silvermanf665d692013-02-17 22:11:39 -080082 DECL_LEVELS;
83#undef DECL_LEVEL
84 return "unknown";
85}
86// Returns the log level represented by str or LOG_UNKNOWN.
87static inline log_level str_log(const char *str) {
Tyler Chatow5febd8f2020-01-05 18:25:31 -080088#define DECL_LEVEL(name, value) \
89 if (!strcmp(str, #name)) return name;
Brian Silvermanf665d692013-02-17 22:11:39 -080090 DECL_LEVELS;
91#undef DECL_LEVEL
92 return LOG_UNKNOWN;
93}
94
Brian Silvermanbe858a12014-04-30 17:37:28 -070095// Implements all of the DoLog* methods in terms of a (pure virtual in this
96// class) HandleMessage method that takes a pointer to the message.
97class HandleMessageLogImplementation : public LogImplementation {
Austin Schuh82c0c822019-05-27 19:55:20 -070098 protected:
99 virtual ::aos::monotonic_clock::time_point monotonic_now() const {
100 return ::aos::monotonic_clock::now();
101 }
102
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500103 private:
Tyler Chatow5febd8f2020-01-05 18:25:31 -0800104 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0))) void DoLog(
105 log_level level, const char *format, va_list ap) override;
Brian Silvermanbe858a12014-04-30 17:37:28 -0700106
107 virtual void HandleMessage(const LogMessage &message) = 0;
108};
109
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800110// A log implementation that dumps all messages to a C stdio stream.
Brian Silvermanbe858a12014-04-30 17:37:28 -0700111class StreamLogImplementation : public HandleMessageLogImplementation {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800112 public:
113 StreamLogImplementation(FILE *stream);
114
115 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500116 void HandleMessage(const LogMessage &message) override;
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800117
118 FILE *const stream_;
119};
120
Brian Silvermanf665d692013-02-17 22:11:39 -0800121// Adds another implementation to the stack of implementations in this
122// task/thread.
123// Any tasks/threads created after this call will also use this implementation.
124// The cutoff is when the state in a given task/thread is created (either lazily
125// when needed or by calling Load()).
126// The logging system takes ownership of implementation. It will delete it if
127// necessary, so it must be created with new.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800128// TODO: Log implementations are never deleted. Need means to safely deregister.
Tyler Chatow4b471e12020-01-05 20:19:36 -0800129void SetImplementation(LogImplementation *implementation,
130 bool update_global = true);
131
132// Updates the log implementation for the current thread, returning the current
133// implementation.
134LogImplementation *SwapImplementation(LogImplementation *implementation);
Brian Silvermanf665d692013-02-17 22:11:39 -0800135
Tyler Chatow67ddb032020-01-12 14:30:04 -0800136LogImplementation *GetImplementation();
137
Brian Silvermanf665d692013-02-17 22:11:39 -0800138// Must be called at least once per process/load before anything else is
139// called. This function is safe to call multiple times from multiple
140// tasks/threads.
141void Init();
142
143// Forces all of the state that is usually lazily created when first needed to
144// be created when called. Cleanup() will delete it.
145void Load();
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500146
Brian Silvermanf665d692013-02-17 22:11:39 -0800147// Resets all information in this task/thread to its initial state.
148// NOTE: This is not the opposite of Init(). The state that this deletes is
149// lazily created when needed. It is actually the opposite of Load().
150void Cleanup();
151
Tyler Chatow5febd8f2020-01-05 18:25:31 -0800152void RegisterCallbackImplementation(
Tyler Chatow67ddb032020-01-12 14:30:04 -0800153 const ::std::function<void(const LogMessage &)> &callback,
154 bool update_global = true);
155
156class ScopedLogRestorer {
157 public:
158 ScopedLogRestorer() { prev_impl_ = GetImplementation(); }
159
160 ~ScopedLogRestorer() { SetImplementation(prev_impl_); }
161
162 private:
163 LogImplementation *prev_impl_;
164};
Tyler Chatow5febd8f2020-01-05 18:25:31 -0800165
Brian Silvermanf665d692013-02-17 22:11:39 -0800166// This is where all of the code that is only used by actual LogImplementations
167// goes.
168namespace internal {
169
Brian Silverman88471dc2014-02-15 22:35:42 -0800170// Fills in *message according to the given inputs (with type kString).
171// Used for implementing LogImplementation::DoLog.
Austin Schuh82c0c822019-05-27 19:55:20 -0700172void FillInMessage(log_level level,
173 ::aos::monotonic_clock::time_point monotonic_now,
174 const char *format, va_list ap, LogMessage *message)
175 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)));
Brian Silvermanf665d692013-02-17 22:11:39 -0800176
Austin Schuh82c0c822019-05-27 19:55:20 -0700177__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 4, 5))) static inline void
178FillInMessageVarargs(log_level level,
179 ::aos::monotonic_clock::time_point monotonic_now,
180 LogMessage *message, const char *format, ...) {
Brian Silverman78968542014-03-05 17:03:43 -0800181 va_list ap;
182 va_start(ap, format);
Austin Schuh82c0c822019-05-27 19:55:20 -0700183 FillInMessage(level, monotonic_now, format, ap, message);
Brian Silverman78968542014-03-05 17:03:43 -0800184 va_end(ap);
185}
186
Brian Silvermanf665d692013-02-17 22:11:39 -0800187// Prints message to output.
188void PrintMessage(FILE *output, const LogMessage &message);
189
Brian Silvermanf665d692013-02-17 22:11:39 -0800190} // namespace internal
191} // namespace logging
192} // namespace aos
193
John Park33858a32018-09-28 23:05:48 -0700194#endif // AOS_LOGGING_IMPLEMENTATIONS_H_