19 #include <argos3/core/config.h>
28 #ifdef ARGOS_THREADSAFE_LOG
39 #include <argos3/core/utility/logging/argos_colored_text.h>
48 #define DEBUG(MSG, ...) { fprintf(stderr, "[DEBUG] "); for(size_t ARGOS_I = 0; ARGOS_I < DEBUG_INDENTATION; ++ARGOS_I) fprintf(stderr, " "); fprintf(stderr, MSG, ##__VA_ARGS__); }
50 #define DEBUG_FUNCTION_ENTER { ++DEBUG_INDENTATION; DEBUG("%s - START\n", __PRETTY_FUNCTION__ ); }
52 #define DEBUG_FUNCTION_EXIT { DEBUG("%s - END\n", __PRETTY_FUNCTION__ ); --DEBUG_INDENTATION; }
54 #define TRACE(LINE) LINE; DEBUG(#LINE "\n");
64 std::ostream& m_cStream;
70 bool m_bColoredOutput;
72 #ifdef ARGOS_THREADSAFE_LOG
74 std::map<pthread_t, size_t> m_mapStreamOrder;
77 std::vector<std::stringstream*> m_vecStreams;
80 pthread_mutex_t m_tMutex;
87 bool b_colored_output_enabled =
true) :
89 m_sLogColor(s_log_color),
90 m_bColoredOutput(b_colored_output_enabled) {
91 #ifdef ARGOS_THREADSAFE_LOG
92 pthread_mutex_init(&m_tMutex, NULL);
93 AddThreadSafeBuffer();
98 #ifdef ARGOS_THREADSAFE_LOG
99 pthread_mutex_destroy(&m_tMutex);
100 while(!m_vecStreams.empty()) {
101 delete m_vecStreams.back();
102 m_vecStreams.pop_back();
105 if(m_bColoredOutput) {
111 m_bColoredOutput =
true;
115 m_bColoredOutput =
false;
119 return m_bColoredOutput;
127 m_cStream.rdbuf(std::ofstream(str_fname.c_str(), std::ios::out | std::ios::trunc).rdbuf());
130 #ifdef ARGOS_THREADSAFE_LOG
131 inline void Flush() {
132 pthread_mutex_lock(&m_tMutex);
133 for(
size_t i = 0; i < m_vecStreams.size(); ++i) {
134 m_cStream << m_vecStreams[i]->str();
135 m_vecStreams[i]->str(
"");
137 pthread_mutex_unlock(&m_tMutex);
140 inline void AddThreadSafeBuffer() {
141 pthread_mutex_lock(&m_tMutex);
142 m_mapStreamOrder.insert(std::make_pair<pthread_t, size_t>(pthread_self(), m_vecStreams.size()));
143 m_vecStreams.push_back(
new std::stringstream);
144 pthread_mutex_unlock(&m_tMutex);
151 #ifdef ARGOS_THREADSAFE_LOG
152 *(m_vecStreams[m_mapStreamOrder.find(pthread_self())->second]) << c_stream;
154 m_cStream << c_stream;
160 if(m_bColoredOutput) {
161 #ifdef ARGOS_THREADSAFE_LOG
162 *(m_vecStreams[m_mapStreamOrder.find(pthread_self())->second]) << m_sLogColor << t_msg <<
reset;
164 m_cStream << m_sLogColor << t_msg <<
reset;
168 #ifdef ARGOS_THREADSAFE_LOG
169 *(m_vecStreams[m_mapStreamOrder.find(pthread_self())->second]) << t_msg;
171 m_cStream << m_sLogColor << t_msg <<
reset;
187 #define RLOG LOG << "[" << GetId() << "] "
188 #define RLOGERR LOGERR << "[" << GetId() << "] "
The namespace containing all the ARGoS related code.
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
std::ostream & reset(std::ostream &c_os)
Resets the text to the default settings.
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
Stream modifier to set attribute and color of the subsequent text.
CARGoSLog & operator<<(const T t_msg)
bool IsColoredOutput() const
void RedirectToFile(const std::string &str_fname)
std::ostream & GetStream()
CARGoSLog & operator<<(std::ostream &(*c_stream)(std::ostream &))
CARGoSLog(std::ostream &c_stream, const SLogColor &s_log_color, bool b_colored_output_enabled=true)
void DisableColoredOutput()
void EnableColoredOutput()