Arion 1.0.2-alpha
A high-performance C++ framework for emulating executable binaries.
 
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1#ifndef ARION_LOGGER_HPP
2#define ARION_LOGGER_HPP
3
5#include <map>
6#include <memory>
7#include <sstream>
8#ifdef ARION_ONLY
9// spdlog should only be considered when compiling libarion.so as it is header-only
10#include <arion/spdlog/spdlog.h>
11#endif
13#include <stack>
14
15namespace arion
16{
17
18class Arion;
19
41
43inline std::map<LOG_COLOR, std::string> arion_log_colors_str = {
44 {LOG_COLOR::DEFAULT, "\033[39m"}, {LOG_COLOR::BLACK, "\033[30m"}, {LOG_COLOR::DARK_RED, "\033[31m"},
45 {LOG_COLOR::DARK_GREEN, "\033[32m"}, {LOG_COLOR::DARK_YELLOW, "\033[33m"}, {LOG_COLOR::DARK_BLUE, "\033[34m"},
46 {LOG_COLOR::DARK_MAGENTA, "\033[35m"}, {LOG_COLOR::DARK_CYAN, "\033[36m"}, {LOG_COLOR::LIGHT_GRAY, "\033[37m"},
47 {LOG_COLOR::DARK_GRAY, "\033[90m"}, {LOG_COLOR::RED, "\033[91m"}, {LOG_COLOR::GREEN, "\033[92m"},
48 {LOG_COLOR::ORANGE, "\033[93m"}, {LOG_COLOR::BLUE, "\033[94m"}, {LOG_COLOR::MAGENTA, "\033[95m"},
49 {LOG_COLOR::CYAN, "\033[96m"}, {LOG_COLOR::WHITE, "\033[97m"},
50
51};
52
54class colorstream : public std::stringstream
55{
56 public:
62 inline colorstream &operator<<(const LOG_COLOR &color)
63 {
64 (*static_cast<std::stringstream *>(this)) << arion_log_colors_str.at(color);
65 return *this;
66 }
67
75 template <typename T> inline colorstream &operator<<(const T &value)
76 {
77 static_cast<std::stringstream &>(*this) << value;
78 return *this;
79 }
80
82 using std::stringstream::operator<<;
83};
84
87{
88 private:
90 static uint64_t curr_id;
92 static std::stack<uint64_t> free_logger_ids;
94 static uint64_t gen_next_id();
96 uint64_t id;
98 pid_t curr_pid;
100 pid_t curr_tid;
104 std::weak_ptr<Arion> arion;
105#ifdef ARION_ONLY
106 // spdlog should only be considered when compiling libarion.so as it is header-only
108 std::shared_ptr<spdlog::logger> logger;
109#else
111 std::shared_ptr<void> logger;
112#endif
117 void refresh_prefix(bool force = false);
118
119 public:
126 static std::unique_ptr<Logger> initialize(std::weak_ptr<Arion> arion, LOG_LEVEL lvl = LOG_LEVEL::INFO);
131 Logger(std::weak_ptr<Arion> arion);
146 void trace(std::string str);
151 void debug(std::string str);
156 void info(std::string str);
161 void warn(std::string str);
166 void error(std::string str);
171 void critical(std::string str);
172};
173
174}; // namespace arion
175
176#endif // ARION_LOGGER_HPP
This class is responsible for managing logging in the Arion emulation framework.
Definition logger.hpp:87
static std::unique_ptr< Logger > initialize(std::weak_ptr< Arion > arion, LOG_LEVEL lvl=LOG_LEVEL::INFO)
LOG_LEVEL log_lvl
The current log level.
Definition logger.hpp:102
pid_t curr_pid
The process ID of the current process.
Definition logger.hpp:98
void info(std::string str)
void set_log_level(LOG_LEVEL lvl)
uint64_t id
The unique identifier for this logger instance.
Definition logger.hpp:96
Logger(std::weak_ptr< Arion > arion)
void error(std::string str)
LOG_LEVEL get_log_level()
pid_t curr_tid
The thread ID of the current thread.
Definition logger.hpp:100
static uint64_t curr_id
The unique identifier for this logger instance.
Definition logger.hpp:90
void debug(std::string str)
void refresh_prefix(bool force=false)
std::weak_ptr< Arion > arion
The Arion instance associated to this instance.
Definition logger.hpp:104
void warn(std::string str)
std::shared_ptr< void > logger
The spdlog logger instance.
Definition logger.hpp:111
static uint64_t gen_next_id()
Generates the next free logger ID.
void critical(std::string str)
void trace(std::string str)
static std::stack< uint64_t > free_logger_ids
A stack of free logger IDs that can be reused.
Definition logger.hpp:92
A stringstream extended class, which allows the use of LOG_COLOR colors for logging.
Definition logger.hpp:55
colorstream & operator<<(const T &value)
Definition logger.hpp:75
colorstream & operator<<(const LOG_COLOR &color)
Definition logger.hpp:62
#define ARION_EXPORT
Defines which symbols should be exported from the library.
Definition global_defs.hpp:13
Definition arch_x86-64.hpp:11
LOG_LEVEL
Identifies a logging level.
Definition global_defs.hpp:156
LOG_COLOR
These colors can be used for logging.
Definition logger.hpp:22
@ LIGHT_GRAY
Light gray.
Definition logger.hpp:31
@ DARK_MAGENTA
Dark magenta.
Definition logger.hpp:29
@ DARK_BLUE
Dark blue.
Definition logger.hpp:28
@ RED
Red.
Definition logger.hpp:33
@ ORANGE
Orange.
Definition logger.hpp:35
@ DARK_YELLOW
Dark yellow.
Definition logger.hpp:27
@ DARK_CYAN
Dark cyan.
Definition logger.hpp:30
@ DARK_RED
Dark red.
Definition logger.hpp:25
@ BLACK
Black.
Definition logger.hpp:24
@ DARK_GREEN
Dark green.
Definition logger.hpp:26
@ MAGENTA
Magenta.
Definition logger.hpp:37
@ WHITE
White.
Definition logger.hpp:39
@ DARK_GRAY
Dark gray.
Definition logger.hpp:32
@ GREEN
Green.
Definition logger.hpp:34
@ CYAN
Cyan.
Definition logger.hpp:38
@ DEFAULT
Default foreground color.
Definition logger.hpp:23
@ BLUE
Blue.
Definition logger.hpp:36
std::map< LOG_COLOR, std::string > arion_log_colors_str
A map identifying an ANSI color code given its associated Arion LOG_COLOR.
Definition logger.hpp:43