blob: bb53b997e1a3a697c6278d0d738d66555238f51a [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
Austin Schuh044e18b2015-10-21 20:17:09 -07004#include <sys/types.h>
Austin Schuh044e18b2015-10-21 20:17:09 -07005
6#include <atomic>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <cinttypes>
8#include <climits>
9#include <cstddef>
10#include <memory>
Austin Schuhad9e5eb2021-11-19 20:33:55 -080011#include <string_view>
Austin Schuh044e18b2015-10-21 20:17:09 -070012
Austin Schuh044e18b2015-10-21 20:17:09 -070013namespace aos {
14namespace logging {
15
Austin Schuh7bb558e2023-08-30 20:50:51 -070016#define LOG_MESSAGE_LEN 512
17#define LOG_MESSAGE_NAME_LEN 100
18
Austin Schuh044e18b2015-10-21 20:17:09 -070019class LogImplementation;
20
21// This is where all of the code that is only used by actual LogImplementations
22// goes.
23namespace internal {
24
Austin Schuh044e18b2015-10-21 20:17:09 -070025// An separate instance of this class is accessible from each task/thread.
26// NOTE: It will get deleted in the child of a fork.
27//
28// Get() and Delete() are implemented in the platform-specific interface.cc
29// file.
30struct Context {
31 Context();
32
33 // Gets the Context object for this task/thread. Will create one the first
34 // time it is called.
35 //
36 // The implementation for each platform will lazily instantiate a new instance
37 // and then initialize name the first time.
38 // IMPORTANT: The implementation of this can not use logging.
39 static Context *Get();
40 // Deletes the Context object for this task/thread so that the next Get() is
41 // called it will create a new one.
42 // It is valid to call this when Get() has never been called.
43 // This also gets called after a fork(2) in the new process, where it should
44 // still work to clean up any state.
45 static void Delete();
46
Austin Schuha0c41ba2020-09-10 22:59:14 -070047 static void DeleteNow();
48
Austin Schuh044e18b2015-10-21 20:17:09 -070049 // Which one to log to right now.
Austin Schuh56196432020-10-24 20:15:21 -070050 // Will be NULL if there is no logging implementation to use right now and we
51 // should use stderr instead.
Austin Schuha0c41ba2020-09-10 22:59:14 -070052 std::shared_ptr<LogImplementation> implementation;
Austin Schuh044e18b2015-10-21 20:17:09 -070053
54 // A name representing this task/(process and thread).
Austin Schuhad9e5eb2021-11-19 20:33:55 -080055 std::string_view MyName();
56 // Clears the cached name so MyName re-checks the thread's actual name.
57 void ClearName();
Austin Schuh044e18b2015-10-21 20:17:09 -070058
59 // What to assign LogMessage::source to in this task/thread.
60 pid_t source;
61
62 // The sequence value to send out with the next message.
63 uint16_t sequence;
Austin Schuhad9e5eb2021-11-19 20:33:55 -080064
65 private:
66 size_t name_size;
67 char name[LOG_MESSAGE_NAME_LEN];
Austin Schuh044e18b2015-10-21 20:17:09 -070068};
69
70} // namespace internal
71} // namespace logging
72} // namespace aos
73
John Park33858a32018-09-28 23:05:48 -070074#endif // AOS_LOGGING_CONTEXT_H_