blob: 25a723340a204b0b1a80e829f14d3739d5044d1d [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 Schuh044e18b2015-10-21 20:17:09 -070011
John Park33858a32018-09-28 23:05:48 -070012#include "aos/logging/sizes.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070013
14namespace aos {
15namespace logging {
16
17class LogImplementation;
18
19// This is where all of the code that is only used by actual LogImplementations
20// goes.
21namespace internal {
22
Austin Schuh044e18b2015-10-21 20:17:09 -070023// 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.
28struct 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 Schuha0c41ba2020-09-10 22:59:14 -070045 static void DeleteNow();
46
Austin Schuh044e18b2015-10-21 20:17:09 -070047 // Which one to log to right now.
Austin Schuh56196432020-10-24 20:15:21 -070048 // Will be NULL if there is no logging implementation to use right now and we
49 // should use stderr instead.
Austin Schuha0c41ba2020-09-10 22:59:14 -070050 std::shared_ptr<LogImplementation> implementation;
Austin Schuh044e18b2015-10-21 20:17:09 -070051
52 // A name representing this task/(process and thread).
53 char name[LOG_MESSAGE_NAME_LEN];
54 size_t name_size;
55
56 // What to assign LogMessage::source to in this task/thread.
57 pid_t source;
58
59 // The sequence value to send out with the next message.
60 uint16_t sequence;
Austin Schuh044e18b2015-10-21 20:17:09 -070061};
62
63} // namespace internal
64} // namespace logging
65} // namespace aos
66
John Park33858a32018-09-28 23:05:48 -070067#endif // AOS_LOGGING_CONTEXT_H_