sim_logger.h Source File

yasimavr: sim_logger.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_logger.h
Go to the documentation of this file.
1/*
2 * sim_logger.h
3 *
4 * Copyright 2022 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_LOGGER_H__
25#define __YASIMAVR_LOGGER_H__
26
27#include "sim_types.h"
28#include "sim_cycle_timer.h"
29#include <cstdarg>
30
32
33
34//=======================================================================================
35
37
38public:
39
40 virtual ~LogWriter() = default;
41
42 virtual void write(cycle_count_t cycle,
43 int level,
44 ctl_id_t id,
45 const char* format,
46 std::va_list args);
47
48 static LogWriter* default_writer();
49};
50
51
52//=======================================================================================
53
54class Logger;
55
57
58 friend class Logger;
59
60public:
61
62 LogHandler();
63
64 void init(CycleManager& cycle_manager);
65
66 void set_writer(LogWriter& w);
67 LogWriter& writer();
68
69private:
70
71 CycleManager* m_cycle_manager;
72 LogWriter* m_writer;
73
74 void write(int lvl, ctl_id_t id, const char* fmt, std::va_list args);
75
76};
77
79{
80 m_writer = &writer;
81}
82
84{
85 return *m_writer;
86}
87
88
89//=======================================================================================
90
92
93public:
94
103
104 Logger(ctl_id_t id, LogHandler& hdl);
105 explicit Logger(ctl_id_t id, Logger* prt = nullptr);
106
107 void set_level(int lvl);
108 int level() const;
109
110 void set_parent(Logger* p);
111 Logger* parent() const;
112
113 void log(int level, const char* format, ...);
114
115 void err(const char* format, ...);
116 void wng(const char* format, ...);
117 void dbg(const char* format, ...);
118
119protected:
120
121 ctl_id_t id() const;
122
123 void filtered_write(int lvl, const char* fmt, std::va_list args);
124 void write(int lvl, ctl_id_t id, const char* fmt, std::va_list args);
125
126private:
127
128 ctl_id_t m_id;
129 int m_level;
130 Logger* m_parent;
131 LogHandler* m_handler;
132
133};
134
135inline void Logger::set_level(int lvl)
136{
137 m_level = lvl;
138}
139
140inline int Logger::level() const
141{
142 return m_level;
143}
144
145inline ctl_id_t Logger::id() const
146{
147 return m_id;
148}
149
151{
152 m_parent = p;
153}
154
155inline Logger* Logger::parent() const
156{
157 return m_parent;
158}
159
160
162
163
165
166#endif //__YASIMAVR_LOGGER_H__
Definition sim_cycle_timer.h:134
Definition sim_logger.h:56
LogWriter & writer()
Definition sim_logger.h:83
void set_writer(LogWriter &w)
Definition sim_logger.h:78
Definition sim_logger.h:36
virtual ~LogWriter()=default
Definition sim_logger.h:91
ctl_id_t id() const
Definition sim_logger.h:145
void set_level(int lvl)
Definition sim_logger.h:135
int level() const
Definition sim_logger.h:140
void set_parent(Logger *p)
Definition sim_logger.h:150
Logger * parent() const
Definition sim_logger.h:155
Level
Definition sim_logger.h:95
@ Level_Trace
Definition sim_logger.h:101
@ Level_Error
Definition sim_logger.h:98
@ Level_Output
Definition sim_logger.h:97
@ Level_Debug
Definition sim_logger.h:100
@ Level_Warning
Definition sim_logger.h:99
Representation of a ID internally represented as a 64-bits integer but can be initialised with a stri...
Definition sim_types.h:627
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
Logger & global_logger() AVR_CORE_PUBLIC_API
Definition sim_logger.cpp:157
YASIMAVR_BEGIN_NAMESPACE typedef long long cycle_count_t
Definition sim_types.h:40