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 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 4 | #include <sys/types.h> |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 5 | |
| 6 | #include <atomic> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cinttypes> |
| 8 | #include <climits> |
| 9 | #include <cstddef> |
| 10 | #include <memory> |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame^] | 11 | #include <string_view> |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 12 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 13 | #include "aos/logging/sizes.h" |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
| 16 | namespace logging { |
| 17 | |
| 18 | class LogImplementation; |
| 19 | |
| 20 | // This is where all of the code that is only used by actual LogImplementations |
| 21 | // goes. |
| 22 | namespace internal { |
| 23 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 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. |
| 29 | struct 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 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 46 | static void DeleteNow(); |
| 47 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 48 | // Which one to log to right now. |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 49 | // Will be NULL if there is no logging implementation to use right now and we |
| 50 | // should use stderr instead. |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 51 | std::shared_ptr<LogImplementation> implementation; |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 52 | |
| 53 | // A name representing this task/(process and thread). |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame^] | 54 | std::string_view MyName(); |
| 55 | // Clears the cached name so MyName re-checks the thread's actual name. |
| 56 | void ClearName(); |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 57 | |
| 58 | // What to assign LogMessage::source to in this task/thread. |
| 59 | pid_t source; |
| 60 | |
| 61 | // The sequence value to send out with the next message. |
| 62 | uint16_t sequence; |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame^] | 63 | |
| 64 | private: |
| 65 | size_t name_size; |
| 66 | char name[LOG_MESSAGE_NAME_LEN]; |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace internal |
| 70 | } // namespace logging |
| 71 | } // namespace aos |
| 72 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 73 | #endif // AOS_LOGGING_CONTEXT_H_ |