blob: 28ffd992d542cd18b96fda3cfcc5d8e3928dc85b [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>
10
11#include <string>
12
13#include "aos/common/logging/logging.h"
14#include "aos/common/type_traits.h"
15#include "aos/common/mutex.h"
16
17// This file has all of the logging implementation. It can't be #included by C
18// code like logging.h can.
Brian Silverman1a572cc2013-03-05 19:58:01 -080019// It is useful for the rest of the logging implementation and other C++ code
20// that needs to do special things with logging.
Brian Silvermanf665d692013-02-17 22:11:39 -080021
22namespace aos {
23namespace logging {
24
25// Unless explicitly stated otherwise, format must always be a string constant,
26// args are printf-style arguments for format, and ap is a va_list of args.
Brian Silverman1a572cc2013-03-05 19:58:01 -080027// The validity of format and args together will be checked at compile time
Brian Silvermanf665d692013-02-17 22:11:39 -080028// using a gcc function attribute.
29
30// The struct that the code uses for making logging calls.
31// Packed so that it ends up the same under both linux and vxworks.
32struct __attribute__((packed)) LogMessage {
33#ifdef __VXWORKS__
34 static_assert(sizeof(pid_t) == sizeof(int),
35 "we use task IDs (aka ints) and pid_t interchangeably");
36#endif
37 // Actually the task ID (aka a pointer to the TCB) on the cRIO.
38 pid_t source;
39 static_assert(sizeof(source) == 4, "that's how they get printed");
40 // Per task/thread.
41 uint16_t sequence;
42 log_level level;
43 int32_t seconds, nseconds;
44 char name[100];
45 char message[LOG_MESSAGE_LEN];
46};
47static_assert(shm_ok<LogMessage>::value, "it's going in a queue");
48
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) {
58#define DECL_LEVEL(name, value) if (level == name) return #name;
59 DECL_LEVELS;
60#undef DECL_LEVEL
61 return "unknown";
62}
63// Returns the log level represented by str or LOG_UNKNOWN.
64static inline log_level str_log(const char *str) {
65#define DECL_LEVEL(name, value) if (!strcmp(str, #name)) return name;
66 DECL_LEVELS;
67#undef DECL_LEVEL
68 return LOG_UNKNOWN;
69}
70
71// Takes a message and logs it. It will set everything up and then call DoLog
72// for the current LogImplementation.
73void VLog(log_level level, const char *format, va_list ap);
74// Adds to the saved up message.
75void VCork(int line, const char *format, va_list ap);
76// Actually logs the saved up message.
77void VUnCork(int line, log_level level, const char *file,
78 const char *format, va_list ap);
79
80// Will call VLog with the given arguments for the next logger in the chain.
81void LogNext(log_level level, const char *format, ...)
82 __attribute__((format(LOG_PRINTF_FORMAT_TYPE, 2, 3)));
83
84// Represents a system that can actually take log messages and do something
85// useful with them.
86// All of the code (transitively too!) in the DoLog here can make
87// normal LOG and LOG_DYNAMIC calls but can NOT call LOG_CORK/LOG_UNCORK. These
88// calls will not result in DoLog recursing. However, implementations must be
89// safe to call from multiple threads/tasks at the same time. Also, any other
90// overriden methods may end up logging through a given implementation's DoLog.
91class LogImplementation {
92 public:
93 LogImplementation() : next_(NULL) {}
94
95 // The one that this one's implementation logs to.
96 // NULL means that there is no next one.
97 LogImplementation *next() { return next_; }
98 // Virtual in case a subclass wants to perform checks. There will be a valid
99 // logger other than this one available while this is called.
100 virtual void set_next(LogImplementation *next) { next_ = next; }
101
102 private:
103 // Actually logs the given message. Implementations should somehow create a
104 // LogMessage and then call internal::FillInMessage.
105 virtual void DoLog(log_level level, const char *format, va_list ap) = 0;
106
107 // Function of this class so that it can access DoLog.
108 // Levels is how many LogImplementations to not use off the stack.
109 static void DoVLog(log_level, const char *format, va_list ap, int levels);
110 // Friends so that they can access DoVLog.
111 friend void VLog(log_level, const char *, va_list);
112 friend void LogNext(log_level, const char *, ...);
113
114 LogImplementation *next_;
115};
116
117// Adds another implementation to the stack of implementations in this
118// task/thread.
119// Any tasks/threads created after this call will also use this implementation.
120// The cutoff is when the state in a given task/thread is created (either lazily
121// when needed or by calling Load()).
122// The logging system takes ownership of implementation. It will delete it if
123// necessary, so it must be created with new.
124void AddImplementation(LogImplementation *implementation);
125
126// Must be called at least once per process/load before anything else is
127// called. This function is safe to call multiple times from multiple
128// tasks/threads.
129void Init();
130
131// Forces all of the state that is usually lazily created when first needed to
132// be created when called. Cleanup() will delete it.
133void Load();
134// Resets all information in this task/thread to its initial state.
135// NOTE: This is not the opposite of Init(). The state that this deletes is
136// lazily created when needed. It is actually the opposite of Load().
137void Cleanup();
138
139// This is where all of the code that is only used by actual LogImplementations
140// goes.
141namespace internal {
142
143// An separate instance of this class is accessible from each task/thread.
Brian Silverman1a572cc2013-03-05 19:58:01 -0800144// NOTE: It will get deleted in the child of a fork.
Brian Silvermanf665d692013-02-17 22:11:39 -0800145struct Context {
146 Context();
147
148 // Gets the Context object for this task/thread. Will create one the first
149 // time it is called.
150 //
151 // The implementation for each platform will lazily instantiate a new instance
152 // and then initialize name the first time.
153 // IMPORTANT: The implementation of this can not use logging.
154 static Context *Get();
155 // Deletes the Context object for this task/thread so that the next Get() is
156 // called it will create a new one.
157 // It is valid to call this when Get() has never been called.
158 static void Delete();
159
160 // Which one to log to right now.
161 // Will be NULL if there is no logging implementation to use right now.
162 LogImplementation *implementation;
163
164 // A string representing this task/(process and thread).
165 const char *name;
166 // The number of bytes in name (including the terminating '\0').
167 // Must be <= sizeof(LogMessage::name).
168 size_t name_size;
169
170 // What to assign LogMessage::source to in this task/thread.
171 pid_t source;
172
173 // The sequence value to send out with the next message.
174 uint16_t sequence;
175
176 // Contains all of the information related to implementing LOG_CORK and
177 // LOG_UNCORK.
178 struct {
179 char message[LOG_MESSAGE_LEN];
180 int line_min, line_max;
181 // Sets the data up to record a new series of corked logs.
182 void Reset() {
183 message[0] = '\0'; // make strlen of it 0
184 line_min = INT_MAX;
185 line_max = -1;
186 function = NULL;
187 }
188 // The function that the calls are in.
189 // REMEMBER: While the compiler/linker will probably optimize all of the
190 // identical strings to point to the same data, it might not, so using == to
191 // compare this with another value is a bad idea.
192 const char *function;
193 } cork_data;
194};
195
196// Fills in *message according to the given inputs. Used for implementing
197// LogImplementation::DoLog.
198void FillInMessage(log_level level, const char *format, va_list ap,
199 LogMessage *message);
200
201// Prints message to output.
202void PrintMessage(FILE *output, const LogMessage &message);
203
204} // namespace internal
205} // namespace logging
206} // namespace aos
207
208#endif // AOS_COMMON_LOGGING_LOGGING_IMPL_H_