blob: 721b04b7b5e735a628dfa1917a9e9c2c6f2c46ab [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
John Park33858a32018-09-28 23:05:48 -070013#include "aos/logging/sizes.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070014
15namespace aos {
16namespace logging {
17
18class LogImplementation;
19
20// This is where all of the code that is only used by actual LogImplementations
21// goes.
22namespace internal {
23
Austin Schuh044e18b2015-10-21 20:17:09 -070024// 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
Austin Schuha0c41ba2020-09-10 22:59:14 -070046 static void DeleteNow();
47
Austin Schuh044e18b2015-10-21 20:17:09 -070048 // Which one to log to right now.
Austin Schuh56196432020-10-24 20:15:21 -070049 // Will be NULL if there is no logging implementation to use right now and we
50 // should use stderr instead.
Austin Schuha0c41ba2020-09-10 22:59:14 -070051 std::shared_ptr<LogImplementation> implementation;
Austin Schuh044e18b2015-10-21 20:17:09 -070052
53 // A name representing this task/(process and thread).
Austin Schuhad9e5eb2021-11-19 20:33:55 -080054 std::string_view MyName();
55 // Clears the cached name so MyName re-checks the thread's actual name.
56 void ClearName();
Austin Schuh044e18b2015-10-21 20:17:09 -070057
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 Schuhad9e5eb2021-11-19 20:33:55 -080063
64 private:
65 size_t name_size;
66 char name[LOG_MESSAGE_NAME_LEN];
Austin Schuh044e18b2015-10-21 20:17:09 -070067};
68
69} // namespace internal
70} // namespace logging
71} // namespace aos
72
John Park33858a32018-09-28 23:05:48 -070073#endif // AOS_LOGGING_CONTEXT_H_