LogStream.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include "LogStream.h"
24 
25 #include <assert.h>
26 #include <iostream>
27 
28 #include "icl_core/os_lxrt.h"
29 #include "icl_core/os_string.h"
33 
34 namespace icl_core {
35 namespace logging {
36 
38 
39 LogStream::LogStream(const std::string& name, icl_core::logging::LogLevel initial_level)
40  : m_initial_level(initial_level),
41  m_name(name),
42  m_active(true),
43  m_mutex(1)
44 {
46 
47  for (size_t i = 0; i < cDEFAULT_LOG_THREAD_STREAM_POOL_SIZE; ++i)
48  {
51  }
52 }
53 
55 {
57 
58  ThreadStreamMap::const_iterator it = m_thread_stream_map.begin();
59  for (; it != m_thread_stream_map.end(); ++it)
60  {
61  delete it->thread_stream;
62  }
63  m_thread_stream_map.clear();
64 }
65 
67 {
68  // TODO: Implement individual log levels for each thread.
69  return m_initial_level;
70 }
71 
73 {
74  if (m_mutex.wait())
75  {
76  m_output_stream_list.insert(new_stream);
77  m_mutex.post();
78  }
79 }
80 
82 {
83  if (m_mutex.wait())
84  {
85  m_output_stream_list.erase(stream);
86  m_mutex.post();
87  }
88 }
89 
91 {
92  icl_core::logging::ThreadStream *thread_stream = NULL;
93 
94  while (!m_mutex.wait())
95  { }
96 
97  ThreadId thread_id = icl_core::os::threadSelf();
98 
99  // Try to find the stream for the current thread, if it has already been assigned.
100  for (ThreadStreamMap::const_iterator find_it = m_thread_stream_map.begin();
101  find_it != m_thread_stream_map.end(); ++find_it)
102  {
103  if (find_it->thread_id == thread_id && find_it->log_level == log_level)
104  {
105  thread_stream = find_it->thread_stream;
106  break;
107  }
108  }
109 
110  // Take a thread stream from the pool, if one is available.
111  if (thread_stream == NULL)
112  {
113  for (ThreadStreamMap::iterator find_it = m_thread_stream_map.begin();
114  find_it != m_thread_stream_map.end(); ++find_it)
115  {
116  if (find_it->thread_id == m_empty_thread_id)
117  {
118  find_it->thread_id = thread_id;
119  find_it->log_level = log_level;
120  thread_stream = find_it->thread_stream;
121  break;
122  }
123  }
124  }
125 
126  // There are no more threads streams available, so create a new one.
127  if (thread_stream == NULL)
128  {
129 #ifdef _SYSTEM_LXRT_
130  // Leave hard real-time to create a new thread stream.
131  bool re_enter_hrt = false;
133  {
135  re_enter_hrt = true;
136  }
137 #endif
138  thread_stream = new icl_core::logging::ThreadStream(this);
139  m_thread_stream_map.push_back(ThreadStreamInfo(thread_id, log_level, thread_stream));
140 #ifdef _SYSTEM_LXRT_
141  if (re_enter_hrt)
142  {
144  }
145 #endif
146  }
147 
148  m_mutex.post();
149 
150  // Set the log level for the thread stream.
151  thread_stream->changeLevel(this->getLogLevel());
152 
153  return *thread_stream;
154 }
155 
157 {
158  for (std::set<LogOutputStream*>::const_iterator it = m_output_stream_list.begin();
159  it != m_output_stream_list.end(); ++it)
160  {
161  std::cerr << (*it)->name() << " ";
162  }
163 }
164 
166 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
167 
171  icl_core::String LogStream::Name() const
172  {
173  return name();
174  }
175 
176  const char *LogStream::NameCStr() const
177  {
178  return nameCStr();
179  }
180 
184  void LogStream::SetActive(bool active)
185  {
186  setActive(active);
187  }
188 
192  icl_core::logging::LogLevel LogStream::InitialLogLevel() const
193  {
194  return initialLogLevel();
195  }
196 
200  bool LogStream::IsActive() const
201  {
202  return isActive();
203  }
204 
207  {
208  return getLogLevel();
209  }
210 
215  void LogStream::AddOutputStream(LogOutputStream *new_stream)
216  {
217  addOutputStream(new_stream);
218  }
219 
224  void LogStream::RemoveOutputStream(LogOutputStream *stream)
225  {
226  removeOutputStream(stream);
227  }
228 
232  void LogStream::PrintConfiguration() const
233  {
235  }
236 
244  {
245  return threadStream(log_level);
246  }
247 
248 #endif
249 
251 
253 {
254  for (ThreadStreamMap::iterator find_it = m_thread_stream_map.begin();
255  find_it != m_thread_stream_map.end(); ++find_it)
256  {
257  if (find_it->thread_stream == thread_stream)
258  {
259  find_it->thread_id = m_empty_thread_id;
260  break;
261  }
262  }
263 }
264 
265 }
266 }
static ThreadId m_empty_thread_id
Definition: LogStream.h:189
ICL_CORE_OS_IMPL_NS::ThreadId ThreadId
Definition: os_thread.h:49
void post()
Increments the semaphore.
Definition: Semaphore.cpp:80
void releaseThreadStream(icl_core::logging::ThreadStream *thread_stream)
Definition: LogStream.cpp:252
#define ICL_CORE_VC_DEPRECATE_STYLE
Definition: Deprecate.h:53
Contains icl_logging::ThreadStream.
void makeHRT()
Definition: os_lxrt.cpp:180
Contains icl_logging::LoggingManager.
Contains global functions for string manipulation, encapsulated into the icl_core::os namespace...
icl_core::logging::ThreadStream & threadStream(icl_core::logging::LogLevel log_level)
Definition: LogStream.cpp:90
icl_core::logging::LogLevel m_initial_level
Definition: LogStream.h:163
static LoggingManager & instance()
void changeLevel(icl_core::logging::LogLevel level)
Definition: ThreadStream.h:64
const char * nameCStr() const
Definition: LogStream.h:69
icl_core::logging::LogLevel initialLogLevel() const
Get the initial log level of this log stream.
Definition: LogStream.h:75
void printConfiguration() const
Prints the list of connected log output streams.
Definition: LogStream.cpp:156
#define ICL_CORE_VC_DEPRECATE_STYLE_USE(arg)
Definition: Deprecate.h:66
void addOutputStream(LogOutputStream *new_stream)
Definition: LogStream.cpp:72
ThreadStreamMap m_thread_stream_map
Definition: LogStream.h:180
void setActive(bool active=true)
Activates or deactivates the log stream.
Definition: LogStream.h:72
LogStream(const icl_core::String &name, icl_core::logging::LogLevel initial_level=cDEFAULT_LOG_LEVEL)
Definition: LogStream.cpp:39
std::set< LogOutputStream * > m_output_stream_list
Definition: LogStream.h:184
bool isActive() const
Checks if the log stream is active.
Definition: LogStream.h:78
bool isThisHRT()
Definition: os_lxrt.cpp:159
Contains global LXRT functions.
void removeLogStream(const icl_core::String &log_stream_name)
This is an output stream class for log messages.
#define cDEFAULT_LOG_THREAD_STREAM_POOL_SIZE
Definition: Constants.h:66
std::string String
Definition: BaseTypes.h:43
Implements the actual logging for an individual thread.
Definition: ThreadStream.h:58
ThreadId threadSelf()
Definition: os_thread.h:54
Contains icl_logging::LogStream.
icl_core::String name() const
Return the name of the log stream.
Definition: LogStream.h:68
icl_core::logging::LogLevel getLogLevel() const
Get the log level of the current thread.
Definition: LogStream.cpp:66
void removeOutputStream(LogOutputStream *stream)
Definition: LogStream.cpp:81
bool ensureNoHRT()
Definition: os_lxrt.cpp:168
Contains icl_logging::LogOutputStream.
icl_core::String m_name
Definition: LogStream.h:182


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58