blob: 5fdeec73b8e34fdae0c23abdafe949acb053d63f [file] [log] [blame]
Brian Silvermanf665d692013-02-17 22:11:39 -08001#ifndef AOS_COMMON_LOGGING_LOGGING_IMPL_H_
2#define AOS_COMMON_LOGGING_LOGGING_IMPL_H_
3
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
16#include "aos/common/logging/logging.h"
17#include "aos/common/type_traits.h"
18#include "aos/common/mutex.h"
Brian Silvermanf7986142014-04-21 17:42:35 -070019#include "aos/common/macros.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070020#include "aos/common/logging/sizes.h"
21#include "aos/common/logging/logging_interface.h"
22#include "aos/common/logging/context.h"
23#include "aos/common/once.h"
Brian Silvermanf665d692013-02-17 22:11:39 -080024
Brian Silvermand6974f42014-02-14 13:39:21 -080025namespace aos {
26
Brian Silvermanf7986142014-04-21 17:42:35 -070027struct MessageType;
Brian Silvermand6974f42014-02-14 13:39:21 -080028
29} // namespace aos
30
Brian Silvermanf665d692013-02-17 22:11:39 -080031// This file has all of the logging implementation. It can't be #included by C
32// code like logging.h can.
Brian Silverman1a572cc2013-03-05 19:58:01 -080033// It is useful for the rest of the logging implementation and other C++ code
34// that needs to do special things with logging.
Brian Silvermanb0893882014-02-10 14:48:30 -080035//
36// It is implemented in logging_impl.cc and logging_interface.cc. They are
37// separate so that code used by logging_impl.cc can link in
Brian Silverman6da04272014-05-18 18:47:48 -070038// logging_interface.cc to use logging without creating a circular dependency.
39// However, any executables with such code still need logging_impl.cc linked in.
Brian Silvermanf665d692013-02-17 22:11:39 -080040
41namespace aos {
42namespace logging {
43
44// Unless explicitly stated otherwise, format must always be a string constant,
45// args are printf-style arguments for format, and ap is a va_list of args.
Brian Silverman1a572cc2013-03-05 19:58:01 -080046// The validity of format and args together will be checked at compile time
Brian Silvermanf665d692013-02-17 22:11:39 -080047// using a gcc function attribute.
48
49// The struct that the code uses for making logging calls.
Brian Silverman88471dc2014-02-15 22:35:42 -080050struct LogMessage {
51 enum class Type : uint8_t {
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080052 kString, kStruct, kMatrix
Brian Silverman88471dc2014-02-15 22:35:42 -080053 };
54
55 int32_t seconds, nseconds;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070056 // message_length is just the length of the actual data (which member depends
57 // on the type).
Brian Silverman88471dc2014-02-15 22:35:42 -080058 size_t message_length, name_length;
Brian Silvermanf665d692013-02-17 22:11:39 -080059 pid_t source;
60 static_assert(sizeof(source) == 4, "that's how they get printed");
61 // Per task/thread.
62 uint16_t sequence;
Brian Silverman88471dc2014-02-15 22:35:42 -080063 Type type;
Brian Silvermanf665d692013-02-17 22:11:39 -080064 log_level level;
Austin Schuh044e18b2015-10-21 20:17:09 -070065 char name[LOG_MESSAGE_NAME_LEN];
Brian Silverman88471dc2014-02-15 22:35:42 -080066 union {
67 char message[LOG_MESSAGE_LEN];
68 struct {
69 uint32_t type_id;
70 size_t string_length;
71 // The message string and then the serialized structure.
72 char serialized[LOG_MESSAGE_LEN - sizeof(type) - sizeof(string_length)];
73 } structure;
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080074 struct {
75 // The type ID of the element type.
76 uint32_t type;
Brian Silvermanff12c9f2014-03-19 17:53:29 -070077 int rows, cols;
78 size_t string_length;
79 // The message string and then the serialized matrix.
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080080 char
Brian Silvermanff12c9f2014-03-19 17:53:29 -070081 data[LOG_MESSAGE_LEN - sizeof(type) - sizeof(rows) - sizeof(cols)];
Brian Silvermanfd5e2a32014-02-22 20:02:39 -080082 } matrix;
Brian Silverman88471dc2014-02-15 22:35:42 -080083 };
Brian Silvermanf665d692013-02-17 22:11:39 -080084};
85static_assert(shm_ok<LogMessage>::value, "it's going in a queue");
86
87// Returns left > right. LOG_UNKNOWN is most important.
88static inline bool log_gt_important(log_level left, log_level right) {
89 if (left == ERROR) left = 3;
90 if (right == ERROR) right = 3;
91 return left > right;
92}
93
94// Returns a string representing level or "unknown".
95static inline const char *log_str(log_level level) {
96#define DECL_LEVEL(name, value) if (level == name) return #name;
97 DECL_LEVELS;
98#undef DECL_LEVEL
99 return "unknown";
100}
101// Returns the log level represented by str or LOG_UNKNOWN.
102static inline log_level str_log(const char *str) {
103#define DECL_LEVEL(name, value) if (!strcmp(str, #name)) return name;
104 DECL_LEVELS;
105#undef DECL_LEVEL
106 return LOG_UNKNOWN;
107}
108
Brian Silvermanf665d692013-02-17 22:11:39 -0800109// Will call VLog with the given arguments for the next logger in the chain.
110void LogNext(log_level level, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -0700111 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 3)));
Brian Silvermanf665d692013-02-17 22:11:39 -0800112
Brian Silvermanfd5e2a32014-02-22 20:02:39 -0800113// Takes a structure and log it.
Brian Silvermand6974f42014-02-14 13:39:21 -0800114template <class T>
115void DoLogStruct(log_level, const ::std::string &, const T &);
Brian Silvermanfd5e2a32014-02-22 20:02:39 -0800116// Takes a matrix and logs it.
117template <class T>
118void DoLogMatrix(log_level, const ::std::string &, const T &);
Brian Silvermand6974f42014-02-14 13:39:21 -0800119
Brian Silvermanf665d692013-02-17 22:11:39 -0800120
Brian Silvermanbe858a12014-04-30 17:37:28 -0700121// Implements all of the DoLog* methods in terms of a (pure virtual in this
122// class) HandleMessage method that takes a pointer to the message.
123class HandleMessageLogImplementation : public LogImplementation {
124 public:
125 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 0)))
126 virtual void DoLog(log_level level, const char *format, va_list ap) override;
127 virtual void LogStruct(log_level level, const ::std::string &message_string,
128 size_t size, const MessageType *type,
129 const ::std::function<size_t(char *)> &serialize)
130 override;
131 virtual void LogMatrix(log_level level, const ::std::string &message_string,
132 uint32_t type_id, int rows, int cols, const void *data)
133 override;
134
135 virtual void HandleMessage(const LogMessage &message) = 0;
136};
137
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800138// A log implementation that dumps all messages to a C stdio stream.
Brian Silvermanbe858a12014-04-30 17:37:28 -0700139class StreamLogImplementation : public HandleMessageLogImplementation {
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800140 public:
141 StreamLogImplementation(FILE *stream);
142
143 private:
Brian Silvermanbe858a12014-04-30 17:37:28 -0700144 virtual void HandleMessage(const LogMessage &message) override;
Brian Silverman1e8ddfe2013-12-19 16:20:53 -0800145
146 FILE *const stream_;
147};
148
Brian Silvermanf665d692013-02-17 22:11:39 -0800149// Adds another implementation to the stack of implementations in this
150// task/thread.
151// Any tasks/threads created after this call will also use this implementation.
152// The cutoff is when the state in a given task/thread is created (either lazily
153// when needed or by calling Load()).
154// The logging system takes ownership of implementation. It will delete it if
155// necessary, so it must be created with new.
156void AddImplementation(LogImplementation *implementation);
157
158// Must be called at least once per process/load before anything else is
159// called. This function is safe to call multiple times from multiple
160// tasks/threads.
161void Init();
162
163// Forces all of the state that is usually lazily created when first needed to
164// be created when called. Cleanup() will delete it.
165void Load();
166// Resets all information in this task/thread to its initial state.
167// NOTE: This is not the opposite of Init(). The state that this deletes is
168// lazily created when needed. It is actually the opposite of Load().
169void Cleanup();
170
171// This is where all of the code that is only used by actual LogImplementations
172// goes.
173namespace internal {
174
Brian Silverman88471dc2014-02-15 22:35:42 -0800175// Fills in all the parts of message according to the given inputs (with type
176// kStruct).
177void FillInMessageStructure(log_level level,
178 const ::std::string &message_string, size_t size,
179 const MessageType *type,
180 const ::std::function<size_t(char *)> &serialize,
181 LogMessage *message);
182
Brian Silverman664db1a2014-03-20 17:06:29 -0700183// Fills in all the parts of the message according to the given inputs (with
184// type kMatrix).
185void FillInMessageMatrix(log_level level,
186 const ::std::string &message_string, uint32_t type_id,
187 int rows, int cols, const void *data,
188 LogMessage *message);
189
Brian Silverman88471dc2014-02-15 22:35:42 -0800190// Fills in *message according to the given inputs (with type kString).
191// Used for implementing LogImplementation::DoLog.
Brian Silvermanf665d692013-02-17 22:11:39 -0800192void FillInMessage(log_level level, const char *format, va_list ap,
Brian Silvermanf7986142014-04-21 17:42:35 -0700193 LogMessage *message)
194 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 0)));
Brian Silvermanf665d692013-02-17 22:11:39 -0800195
Brian Silvermanf7986142014-04-21 17:42:35 -0700196__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4)))
Brian Silverman78968542014-03-05 17:03:43 -0800197static inline void FillInMessageVarargs(log_level level, LogMessage *message,
198 const char *format, ...) {
199 va_list ap;
200 va_start(ap, format);
201 FillInMessage(level, format, ap, message);
202 va_end(ap);
203}
204
Brian Silvermanf665d692013-02-17 22:11:39 -0800205// Prints message to output.
206void PrintMessage(FILE *output, const LogMessage &message);
207
Brian Silvermanf665d692013-02-17 22:11:39 -0800208} // namespace internal
209} // namespace logging
210} // namespace aos
211
212#endif // AOS_COMMON_LOGGING_LOGGING_IMPL_H_