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