blob: b2abc0d915985727225595b96982a1ce0ac7ecbb [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_LOGGING_CONTEXT_H_
2#define AOS_LOGGING_CONTEXT_H_
Austin Schuh044e18b2015-10-21 20:17:09 -07003
4#include <inttypes.h>
5#include <stddef.h>
6#include <sys/types.h>
7#include <limits.h>
8
9#include <atomic>
10
John Park33858a32018-09-28 23:05:48 -070011#include "aos/logging/sizes.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070012
13namespace aos {
14namespace logging {
15
16class LogImplementation;
17
18// This is where all of the code that is only used by actual LogImplementations
19// goes.
20namespace internal {
21
22extern ::std::atomic<LogImplementation *> global_top_implementation;
23
24// An separate instance of this class is accessible from each task/thread.
25// NOTE: It will get deleted in the child of a fork.
26//
27// Get() and Delete() are implemented in the platform-specific interface.cc
28// file.
29struct Context {
30 Context();
31
32 // Gets the Context object for this task/thread. Will create one the first
33 // time it is called.
34 //
35 // The implementation for each platform will lazily instantiate a new instance
36 // and then initialize name the first time.
37 // IMPORTANT: The implementation of this can not use logging.
38 static Context *Get();
39 // Deletes the Context object for this task/thread so that the next Get() is
40 // called it will create a new one.
41 // It is valid to call this when Get() has never been called.
42 // This also gets called after a fork(2) in the new process, where it should
43 // still work to clean up any state.
44 static void Delete();
45
46 // Which one to log to right now.
47 // Will be NULL if there is no logging implementation to use right now.
48 LogImplementation *implementation;
49
50 // A name representing this task/(process and thread).
51 char name[LOG_MESSAGE_NAME_LEN];
52 size_t name_size;
53
54 // What to assign LogMessage::source to in this task/thread.
55 pid_t source;
56
57 // The sequence value to send out with the next message.
58 uint16_t sequence;
59
60 // Contains all of the information related to implementing LOG_CORK and
61 // LOG_UNCORK.
62 struct {
63 char message[LOG_MESSAGE_LEN];
64 int line_min, line_max;
65 // Sets the data up to record a new series of corked logs.
66 void Reset() {
67 message[0] = '\0'; // make strlen of it 0
68 line_min = INT_MAX;
69 line_max = -1;
70 function = NULL;
71 }
72 // The function that the calls are in.
73 // REMEMBER: While the compiler/linker will probably optimize all of the
74 // identical strings to point to the same data, it might not, so using == to
75 // compare this with another value is a bad idea.
76 const char *function;
77 } cork_data;
78};
79
80} // namespace internal
81} // namespace logging
82} // namespace aos
83
John Park33858a32018-09-28 23:05:48 -070084#endif // AOS_LOGGING_CONTEXT_H_