blob: 27c0472df903c1d5a4833da39b853530c537025d [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>
6#include <stdint.h>
7#include <limits.h>
8#include <string.h>
9#include <stdio.h>
Brian Silverman669669f2014-02-14 16:32:56 -080010#include <stdarg.h>
Brian Silvermanf665d692013-02-17 22:11:39 -080011
12#include <string>
Brian Silvermand6974f42014-02-14 13:39:21 -080013#include <functional>
Brian Silverman6da04272014-05-18 18:47:48 -070014#include <atomic>
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 Silvermand6974f42014-02-14 13:39:21 -080026namespace aos {
27
Brian Silvermanf7986142014-04-21 17:42:35 -070028struct MessageType;
Brian Silvermancb5da1f2015-12-05 22:19:58 -050029class RawQueue;
Brian Silvermand6974f42014-02-14 13:39:21 -080030
31} // namespace aos
32
Brian Silvermancb5da1f2015-12-05 22:19:58 -050033// This file has various concrete LogImplementations.
Brian Silvermanf665d692013-02-17 22:11:39 -080034
35namespace aos {
36namespace logging {
37
38// Unless explicitly stated otherwise, format must always be a string constant,
39// args are printf-style arguments for format, and ap is a va_list of args.
Brian Silverman1a572cc2013-03-05 19:58:01 -080040// The validity of format and args together will be checked at compile time
Brian Silvermancb5da1f2015-12-05 22:19:58 -050041// using a function attribute.
Brian Silvermanf665d692013-02-17 22:11:39 -080042
Brian Silvermancb5da1f2015-12-05 22:19:58 -050043// Contains all of the information about a given logging call.
Brian Silverman88471dc2014-02-15 22:35:42 -080044struct LogMessage {
45 enum class Type : uint8_t {
Alex Perrycb7da4b2019-08-28 19:35:56 -070046 kString
Brian Silverman88471dc2014-02-15 22:35:42 -080047 };
48
49 int32_t seconds, nseconds;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070050 // message_length is just the length of the actual data (which member depends
51 // on the type).
Brian Silverman88471dc2014-02-15 22:35:42 -080052 size_t message_length, name_length;
Brian Silvermanf665d692013-02-17 22:11:39 -080053 pid_t source;
54 static_assert(sizeof(source) == 4, "that's how they get printed");
55 // Per task/thread.
56 uint16_t sequence;
Brian Silverman88471dc2014-02-15 22:35:42 -080057 Type type;
Brian Silvermanf665d692013-02-17 22:11:39 -080058 log_level level;
Austin Schuh044e18b2015-10-21 20:17:09 -070059 char name[LOG_MESSAGE_NAME_LEN];
Brian Silverman88471dc2014-02-15 22:35:42 -080060 union {
61 char message[LOG_MESSAGE_LEN];
62 struct {
63 uint32_t type_id;
64 size_t string_length;
65 // The message string and then the serialized structure.
66 char serialized[LOG_MESSAGE_LEN - sizeof(type) - sizeof(string_length)];
67 } structure;
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080068 struct {
69 // The type ID of the element type.
70 uint32_t type;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070071 int rows, cols;
72 size_t string_length;
73 // The message string and then the serialized matrix.
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080074 char
Brian Silvermanff12c9f2014-03-19 17:53:29 -070075 data[LOG_MESSAGE_LEN - sizeof(type) - sizeof(rows) - sizeof(cols)];
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080076 } matrix;
Brian Silverman88471dc2014-02-15 22:35:42 -080077 };
Brian Silvermanf665d692013-02-17 22:11:39 -080078};
79static_assert(shm_ok<LogMessage>::value, "it's going in a queue");
80
81// Returns left > right. LOG_UNKNOWN is most important.
82static inline bool log_gt_important(log_level left, log_level right) {
83 if (left == ERROR) left = 3;
84 if (right == ERROR) right = 3;
85 return left > right;
86}
87
88// Returns a string representing level or "unknown".
89static inline const char *log_str(log_level level) {
90#define DECL_LEVEL(name, value) if (level == name) return #name;
91 DECL_LEVELS;
92#undef DECL_LEVEL
93 return "unknown";
94}
95// Returns the log level represented by str or LOG_UNKNOWN.
96static inline log_level str_log(const char *str) {
97#define DECL_LEVEL(name, value) if (!strcmp(str, #name)) return name;
98 DECL_LEVELS;
99#undef DECL_LEVEL
100 return LOG_UNKNOWN;
101}
102
Brian Silvermanbe858a12014-04-30 17:37:28 -0700103// Implements all of the DoLog* methods in terms of a (pure virtual in this
104// class) HandleMessage method that takes a pointer to the message.
105class HandleMessageLogImplementation : public LogImplementation {
Austin Schuh82c0c822019-05-27 19:55:20 -0700106 protected:
107 virtual ::aos::monotonic_clock::time_point monotonic_now() const {
108 return ::aos::monotonic_clock::now();
109 }
110
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500111 private:
Brian Silvermanbe858a12014-04-30 17:37:28 -0700112 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500113 void DoLog(log_level level, const char *format, va_list ap) override;
Brian Silvermanbe858a12014-04-30 17:37:28 -0700114
115 virtual void HandleMessage(const LogMessage &message) = 0;
116};
117
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800118// A log implementation that dumps all messages to a C stdio stream.
Brian Silvermanbe858a12014-04-30 17:37:28 -0700119class StreamLogImplementation : public HandleMessageLogImplementation {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800120 public:
121 StreamLogImplementation(FILE *stream);
122
123 private:
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500124 void HandleMessage(const LogMessage &message) override;
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800125
126 FILE *const stream_;
127};
128
Brian Silvermanf665d692013-02-17 22:11:39 -0800129// Adds another implementation to the stack of implementations in this
130// task/thread.
131// Any tasks/threads created after this call will also use this implementation.
132// The cutoff is when the state in a given task/thread is created (either lazily
133// when needed or by calling Load()).
134// The logging system takes ownership of implementation. It will delete it if
135// necessary, so it must be created with new.
136void AddImplementation(LogImplementation *implementation);
137
138// 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
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500152// Returns a queue which deals with LogMessage-sized messages.
153// The caller takes ownership.
154RawQueue *GetLoggingQueue();
155
156// Calls AddImplementation to register the standard linux logging implementation
157// which sends the messages through a queue. This implementation relies on
158// another process(es) to read the log messages that it puts into the queue.
159// This function is usually called by aos::Init*.
160void RegisterQueueImplementation();
161
Brian Silvermanf665d692013-02-17 22:11:39 -0800162// This is where all of the code that is only used by actual LogImplementations
163// goes.
164namespace internal {
165
Brian Silverman88471dc2014-02-15 22:35:42 -0800166// Fills in *message according to the given inputs (with type kString).
167// Used for implementing LogImplementation::DoLog.
Austin Schuh82c0c822019-05-27 19:55:20 -0700168void FillInMessage(log_level level,
169 ::aos::monotonic_clock::time_point monotonic_now,
170 const char *format, va_list ap, LogMessage *message)
171 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)));
Brian Silvermanf665d692013-02-17 22:11:39 -0800172
Austin Schuh82c0c822019-05-27 19:55:20 -0700173__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 4, 5))) static inline void
174FillInMessageVarargs(log_level level,
175 ::aos::monotonic_clock::time_point monotonic_now,
176 LogMessage *message, const char *format, ...) {
Brian Silverman78968542014-03-05 17:03:43 -0800177 va_list ap;
178 va_start(ap, format);
Austin Schuh82c0c822019-05-27 19:55:20 -0700179 FillInMessage(level, monotonic_now, format, ap, message);
Brian Silverman78968542014-03-05 17:03:43 -0800180 va_end(ap);
181}
182
Brian Silvermanf665d692013-02-17 22:11:39 -0800183// Prints message to output.
184void PrintMessage(FILE *output, const LogMessage &message);
185
Brian Silvermanf665d692013-02-17 22:11:39 -0800186} // namespace internal
187} // namespace logging
188} // namespace aos
189
John Park33858a32018-09-28 23:05:48 -0700190#endif // AOS_LOGGING_IMPLEMENTATIONS_H_