20#include "sparta/log/Message.hpp"
25#include "sparta/utils/Utils.hpp"
26#include "sparta/log/MessageInfo.hpp"
63 num_msgs_received_(rhp.num_msgs_received_),
64 num_msgs_written_(rhp.num_msgs_written_),
65 num_msg_duplicates_(rhp.num_msg_duplicates_),
66 last_seq_map_(rhp.last_seq_map_)
71 num_msgs_received_(0),
73 num_msg_duplicates_(0)
83 template <std::
size_t N>
84 bool compare(
const char (& arg)[N])
const {
119 template <
typename T>
121 throw SpartaException(
"Logging destination does not now how to compare a Destination "
122 "instance with type: ") <<
demangle(
typeid(T).name());
152 virtual std::string
stringize(
bool pretty=
false)
const = 0;
156 std::lock_guard<std::mutex> lock(write_mutex_);
158 ++num_msgs_received_;
162 if(msg.info.
seq_num <= last_seq){
164 ++num_msg_duplicates_;
206 auto itr = last_seq_map_.find(tid);
207 if(itr != last_seq_map_.end()){
221 uint64_t num_msgs_received_;
222 uint64_t num_msgs_written_;
223 uint64_t num_msg_duplicates_;
225 std::map<thread_id_type, seq_num_type> last_seq_map_;
227 std::mutex write_mutex_;
236 std::ostream& stream_;
245 const char*
const extension;
246 const std::string extname;
247 std::function<
Formatter* (std::ostream&)> factory;
299 stream_ << msg.info <<
copyWithReplace(msg.content,
'\n',
"") << std::endl;
304 sim_info.
write(stream_,
"#",
"\n");
325 sim_info.
write(stream_,
"#",
"\n");
348 sim_info.
write(stream_,
"#",
"\n");
368 sim_info.
write(stream_,
"#",
"\n");
384 template <
typename DestType>
397 std::ostream& stream_;
406 if(stream.good() ==
false){
412 return &o == &stream_;
416 virtual std::string
stringize(
bool pretty=
false)
const override {
418 std::stringstream ss;
419 ss <<
"<" <<
"destination ostream=";
420 if(&stream_ == &std::cout){
422 }
else if(&stream_ == &std::cerr){
452 std::ofstream stream_;
453 const std::string filename_;
454 std::unique_ptr<Formatter> formatter_;
466 stream_(filename, std::ofstream::out),
469 if(stream_.good() ==
false){
470 throw SpartaException(
"Failed to open logging destination file for append \"")
475 stream_.exceptions(std::ostream::eofbit | std::ostream::badbit | std::ostream::failbit | std::ostream::goodbit);
479 if(
nullptr == fmtinfo_->extension){
482 std::string ext = fmtinfo_->extension;
483 size_t pos = filename.find(ext);
484 if(pos != std::string::npos && pos == filename.size() - ext.size()){
489 if(
nullptr == fmtinfo_){
491 <<
"list for file-based destination instance. "
492 <<
"Cannot open file \"" << filename <<
"\"";
494 formatter_.reset(fmtinfo_->factory(stream_));
501 return filename == filename_;
505 virtual std::string
stringize(
bool pretty=
false)
const override {
507 std::stringstream ss;
508 ss <<
"<" <<
"destination file=\"" << filename_ <<
"\" format=\""
509 << fmtinfo_->extname <<
"\" ext=\"";
510 if(
nullptr == fmtinfo_->extension){
513 ss << fmtinfo_->extension;
525 formatter_->write(msg);
566 template <
class DestT>
568 for(std::unique_ptr<Destination>& d : dests_){
576 return dests_.back().get();
580 template <std::
size_t N>
590 template <
class DestT>
595 template <
class DestT>
597 return new DestinationInstance<DestT>(arg);
612 return dests_.size();
621 for(
auto& d : dests_){
622 o <<
" " << d->stringize() << std::endl;
638 if(
nullptr == finf->extension){
639 o <<
" " <<
"(default) -> " << finf->extname << std::endl;
642 o <<
" \"" << finf->extension <<
"\" -> " << finf->extname << std::endl;
Simulation information container for tracking the status of the simulation.
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
Contains information describing the simulation instance for the purpose of identifying the simulation...
void write(StreamType &o, const std::string &line_start="# ", const std::string &line_end="\n", bool show_field_names=true) const
Write this information to an ostream.
static SimulationInfo & getInstance()
Gets the SimulationInfo singleton instance.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
std::string getLocation() const override final
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this Destination.
virtual bool compareOstreams(const std::ostream &o) const override
Returns true if the destination behind this interface was constructed with the ostream o.
virtual bool compareStrings(const std::string &filename) const override
Returns true if the destination behind this interface was constructed with the string s.
virtual std::string stringize(bool pretty=false) const override
Create a string representation of this Destination.
DestinationInstance(const std::string &filename)
Constructs the destination with the given filename. Data will be written in a format based on the ext...
A destination where log messages go to be written to a file or other ostream. Attempts to prevent dup...
Manages a set of destinations representing files or streams. All log messages will be written to at l...
static Destination * createDestination(const char(&arg)[N])
createDestination overload for handling const char[] strings
static const DestinationVector & getDestinations()
Returns vector containing all destinations.
static std::ostream & dumpDestinations(std::ostream &o, bool pretty=false)
Dumps the destinations known by the destination manager to an ostream with each destination on a sepa...
std::vector< std::unique_ptr< Destination > > DestinationVector
Vector of destination pointers.
static uint32_t getNumDestinations()
Returns number of destinations contained in the DestinationManager.
static Destination * createDestination(const char *&arg)
createDestination overload for handling char* strings
static std::ostream & dumpFileExtensions(std::ostream &o, bool pretty=false)
Dumps the supported file extensions for destinations instantiated through the destination manager bas...
static Destination * getDestination(DestT &arg)
Requests an existing Destination* from the manager and allocates a new one if it does not yet have a ...
Generic Logging destination stream interface which writes sparta::log::Message structures to some out...
uint64_t getNumMessagesReceived() const
Get the total number of messages logged through this destination.
uint64_t getNumMessageDuplicates() const
Gets the total number of times that a message has arrived at this destination after already having be...
virtual bool compareOstreams(const std::ostream &) const
Returns true if the destination behind this interface was constructed with the ostream o.
bool compare(const T &) const
Operator= for for other unknown types.
bool compare(const char *&arg) const
Comparison of destination for const char[]. Uses std::string comparison.
bool compare(const std::string &arg) const
Comparison of destination for std::string.
void write(const sparta::log::Message &msg)
bool compare(const std::ostream &arg) const
Handle Destination::operator== on ostreams.
virtual bool compareStrings(const std::string &) const
Returns true if the destination behind this interface was constructed with the string s.
bool compare(const char(&arg)[N]) const
Comparison of destination for const char[]. Uses std::string comparison.
Destination()
Default constructor.
uint64_t getNumMessagesWritten() const
Gets the total number of messages received by this destination and then written to the actual output ...
virtual std::string stringize(bool pretty=false) const =0
Create a string representation of this Destination.
Destination(const Destination &&rhp)
int64_t seq_num_type
Sequence number of a message within a thread ID. Signed so that initial state can be -1.
uint32_t thread_id_type
Identifies a thread in the simulator kernel.
Macros for handling exponential backoff.
std::string copyWithReplace(const std::string &s, char from, const std::string &to)
Copies from 1 string to another with replacement of a single character with a string.
std::string demangle(const std::string &name) noexcept
Demangles a C++ symbol.
seq_num_type seq_num
Sequence number of message within thread.
thread_id_type thread_id
Thread ID of source.
category_id_type category
Category with which this message was created. Must not be NULL.
TreeNode const & origin
Node from which message originated.
Contains a logging message header and content.