ThreadStream.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 //----------------------------------------------------------------------
24 
26 #include <icl_core/os_lxrt.h>
30 
31 #include <stdarg.h>
32 #include <string.h>
33 
34 #if defined _SYSTEM_POSIX_
36 #elif defined _SYSTEM_WIN32_
37 // No special implementation here because ThreadId is an unsigned long
38 // on Win32 platforms.
39 #else
40 # error "No ::icl_core::logging namespace implementation defined for this platform."
41 #endif
42 
43 #if defined ICL_CORE_QT_SUPPORT
44 #include <QtCore/QString>
45 #endif
46 
47 namespace icl_core {
48 namespace logging {
49 
50 void ThreadStream::setClassname(const char *classname)
51 {
52  strncpy(m_classname, classname, cDEFAULT_LOG_SIZE);
53 }
54 
55 void ThreadStream::setFilename(const char *filename)
56 {
57  strncpy(m_filename, filename, cDEFAULT_LOG_SIZE);
58 }
59 
60 void ThreadStream::setObjectname(const char *objectname)
61 {
62  strncpy(m_objectname, objectname, cDEFAULT_LOG_SIZE);
63 }
64 
65 void ThreadStream::setFunction(const char *function)
66 {
67  strncpy(m_function, function, cDEFAULT_LOG_SIZE);
68 }
69 
70 void ThreadStream::setLine(size_t line)
71 {
72  m_line = line;
73 }
74 
76 {
77  m_line_log_level = line_log_level;
78 }
79 
80 void ThreadStream::write(const char *source, size_t number_of_bytes, size_t protected_buffer_size)
81 {
82  // Protect the last byte in the thread stream's buffer!
83  size_t writable_length = cDEFAULT_LOG_SIZE - m_write_index - 1;
84  if (number_of_bytes + protected_buffer_size > writable_length)
85  {
86  if (writable_length > protected_buffer_size)
87  {
88  number_of_bytes = writable_length - protected_buffer_size;
89  }
90  else
91  {
92  number_of_bytes = 0;
93  }
94  }
95  memcpy(&m_data[m_write_index], source, number_of_bytes);
96 
97  m_write_index += number_of_bytes;
98 }
99 
100 void ThreadStream::printf(const char *fmt, ...)
101 {
102  // Protect the last byte in the thread stream's buffer!
103  size_t writable_length = cDEFAULT_LOG_SIZE - m_write_index - 1;
104 
105  va_list argptr;
106  va_start(argptr, fmt);
107  int32_t bytes_printed = vsnprintf(&m_data[m_write_index], writable_length, fmt, argptr);
108  va_end(argptr);
109 
110  if (bytes_printed >= 0)
111  {
112  if (size_t(bytes_printed) > writable_length)
113  {
114  m_write_index += writable_length;
115  }
116  else
117  {
118  m_write_index += bytes_printed;
119  }
120  }
121 
122  flush();
123 }
124 
126 {
127  m_data[m_write_index] = '\0';
128  if (m_parent->m_mutex.wait())
129  {
130  for (std::set<LogOutputStream*>::const_iterator iter = m_parent->m_output_stream_list.begin();
131  iter != m_parent->m_output_stream_list.end();
132  ++iter)
133  {
134  (*iter)->push(m_line_log_level, m_parent->nameCStr(), m_filename, m_line,
136  }
137 
139 
140  m_parent->m_mutex.post();
141  }
142  else
143  {
144  PRINTF("ThreadStream(%s)::Flush: mutex lock failed\n", m_parent->nameCStr());
145  }
146  m_write_index = 0;
147 }
148 
150  : m_parent(parent),
151  m_level(parent->m_initial_level),
152  m_line_log_level(parent->m_initial_level),
153  m_line(0),
154  m_write_index(0)
155 {
159 }
160 
161 
163 {
164  char local_buffer[8];
165  size_t length = os::snprintf(local_buffer, 8, "%hu", static_cast<uint8_t>(value));
166  stream.write(local_buffer, length);
167  return stream;
168 }
169 
171 {
172  char local_buffer[8];
173  size_t length = os::snprintf(local_buffer, 8, "%hu", value);
174  stream.write(local_buffer, length);
175  return stream;
176 }
177 
179 {
180  char local_buffer[13];
181  size_t length = os::snprintf(local_buffer, 13, "%u", value);
182  stream.write(local_buffer, length);
183  return stream;
184 }
185 
186 #if __WORDSIZE != 64
187 ThreadStream& operator << (ThreadStream& stream, unsigned long value)
188 {
189  char local_buffer[13];
190  size_t length = os::snprintf(local_buffer, 13, "%lu", value);
191  stream.write(local_buffer, length);
192  return stream;
193 }
194 #endif
195 
197 {
198  char local_buffer[23];
199  size_t length = os::snprintf(local_buffer, 23, "%llu", value);
200  stream.write(local_buffer, length);
201  return stream;
202 }
203 
205 {
206  char local_buffer[8];
207  size_t length = os::snprintf(local_buffer, 8, "%hd", static_cast<int16_t>(value));
208  stream.write(local_buffer, length);
209  return stream;
210 }
211 
213 {
214  char local_buffer[8];
215  size_t length = os::snprintf(local_buffer, 8, "%hd", value);
216  stream.write(local_buffer, length);
217  return stream;
218 }
219 
221 {
222  char local_buffer[13];
223  size_t length = os::snprintf(local_buffer, 13, "%d", value);
224  stream.write(local_buffer, length);
225  return stream;
226 }
227 
228 #if __WORDSIZE != 64
230 {
231  char local_buffer[13];
232  size_t length = os::snprintf(local_buffer, 13, "%ld", value);
233  stream.write(local_buffer, length);
234  return stream;
235 }
236 #endif
237 
239 {
240  char local_buffer[23];
241  size_t length = os::snprintf(local_buffer, 23, "%lld", value);
242  stream.write(local_buffer, length);
243  return stream;
244 }
245 
246 #ifdef _SYSTEM_DARWIN_
247 ThreadStream& operator << (ThreadStream& stream, size_t value)
248 {
249  char local_buffer[23];
250  size_t length = os::snprintf(local_buffer, 23, "%zu", value);
251  stream.write(local_buffer, length);
252  return stream;
253 }
254 #endif
255 
256 ThreadStream& operator << (ThreadStream& stream, const char *text)
257 {
258  stream.write(text, strlen(text));
259  return stream;
260 }
261 
262 ThreadStream& operator << (ThreadStream& stream, const std::string& text)
263 {
264  stream.write(text.c_str(), text.size());
265  return stream;
266 }
267 
268 ThreadStream& operator << (ThreadStream& stream, double value)
269 {
270  char local_buffer[100];
271  size_t length = os::snprintf(local_buffer, 100, "%f", value);
272  stream.write(local_buffer, length);
273  return stream;
274 }
275 
277 {
278  if (value)
279  {
280  stream.write("True", 4);
281  }
282  else
283  {
284  stream.write("False", 5);
285  }
286  return stream;
287 }
288 
289 ThreadStream& operator << (ThreadStream& stream, void * value)
290 {
291  char local_buffer[25];
292  size_t length = os::snprintf(local_buffer, 25, "%p", value);
293  stream.write(local_buffer, length);
294  return stream;
295 }
296 
297 #ifndef _SYSTEM_WIN32_
298 ThreadStream& operator << (ThreadStream& stream, const ThreadId& thread_id)
299 {
300  return ICL_CORE_LOGGING_IMPL_NS::operator << (stream, thread_id);
301 }
302 #endif
303 
304 #if defined ICL_CORE_QT_SUPPORT
305 ThreadStream& operator << (ThreadStream& stream, const QString& value)
306 {
307  return operator << (stream, value.toLatin1().constData());
308 }
309 #endif
310 
311 
313 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
314 
318  void ThreadStream::ChangeLevel(icl_core::logging::LogLevel level)
319  {
320  changeLevel(level);
321  }
322 
328  {
329  return getLogLevel();
330  }
331 
335  void ThreadStream::SetClassname(const char *classname)
336  {
337  setClassname(classname);
338  }
339 
343  void ThreadStream::SetObjectname(const char *objectname)
344  {
345  setObjectname(objectname);
346  }
347 
351  void ThreadStream::SetFilename(const char *filename)
352  {
353  setFilename(filename);
354  }
355 
359  void ThreadStream::SetFunction(const char *function)
360  {
361  setFunction(function);
362  }
363 
367  void ThreadStream::SetLine(size_t line)
368  {
369  setLine(line);
370  }
371 
375  void ThreadStream::SetLineLogLevel(icl_core::logging::LogLevel line_log_level)
376  {
377  setLineLogLevel(line_log_level);
378  }
379 
384  void ThreadStream::Write(const char *buffer, size_t number_of_bytes,
385  size_t protected_buffer_size)
386  {
387  write(buffer, number_of_bytes, protected_buffer_size);
388  }
389 
394  void ThreadStream::Printf(const char *fmt, ...)
395  {
396  // Protect the last byte in the thread stream's buffer!
397  size_t writable_length = cDEFAULT_LOG_SIZE - m_write_index - 1;
398 
399  va_list argptr;
400  va_start(argptr, fmt);
401  int32_t bytes_printed = vsnprintf(&m_data[m_write_index], writable_length, fmt, argptr);
402  va_end(argptr);
403 
404  if (bytes_printed >= 0)
405  {
406  if (size_t(bytes_printed) > writable_length)
407  {
408  m_write_index += writable_length;
409  }
410  else
411  {
412  m_write_index += bytes_printed;
413  }
414  }
415 
416  flush();
417  }
418 
423  void ThreadStream::Flush()
424  {
425  flush();
426  }
427 
428 #endif
429 
431 }
432 }
ICL_CORE_OS_IMPL_NS::ThreadId ThreadId
Definition: os_thread.h:49
void post()
Increments the semaphore.
Definition: Semaphore.cpp:80
signed int int32_t
Definition: msvc_stdint.h:90
unsigned int uint32_t
Definition: msvc_stdint.h:93
void printf(const char *fmt,...)
void releaseThreadStream(icl_core::logging::ThreadStream *thread_stream)
Definition: LogStream.cpp:252
Contains icl_logging::ThreadStream.
Contains ACE specific implementations of logging stream operators.
void * memcpy(void *dest, void *src, size_t count)
Definition: os_mem.h:41
signed char int8_t
Definition: msvc_stdint.h:88
char m_classname[cDEFAULT_LOG_SIZE+1]
Definition: ThreadStream.h:229
void changeLevel(icl_core::logging::LogLevel level)
Definition: ThreadStream.h:64
const char * nameCStr() const
Definition: LogStream.h:69
#define ICL_CORE_VC_DEPRECATE_STYLE_USE(arg)
Definition: Deprecate.h:66
void setFilename(const char *filename)
signed __int64 int64_t
Definition: msvc_stdint.h:102
Contains a system independet PRINTF macro.
void setClassname(const char *classname)
unsigned __int64 uint64_t
Definition: msvc_stdint.h:103
icl_core::logging::LogLevel m_line_log_level
Definition: ThreadStream.h:226
unsigned char uint8_t
Definition: msvc_stdint.h:91
Implements a thread-safe logging framework.
Definition: LogStream.h:54
ThreadStream & operator<<(ThreadStream &stream, const ThreadId &thread_id)
std::set< LogOutputStream * > m_output_stream_list
Definition: LogStream.h:184
void setFunction(const char *function)
void write(const char *buffer, size_t number_of_bytes, size_t protected_buffer_size=1)
char m_filename[cDEFAULT_LOG_SIZE+1]
Definition: ThreadStream.h:227
char m_data[cDEFAULT_LOG_SIZE+1]
Definition: ThreadStream.h:232
char m_function[cDEFAULT_LOG_SIZE+1]
Definition: ThreadStream.h:231
Definition of the implementation namespace for global functions.
char m_objectname[cDEFAULT_LOG_SIZE+1]
Definition: ThreadStream.h:230
static char buffer[2000]
icl_core::logging::LogLevel m_level
Definition: ThreadStream.h:224
int vsnprintf(char *buffer, size_t maxlen, const char *format, va_list argptr)
Contains global LXRT functions.
void setObjectname(const char *objectname)
Implements the actual logging for an individual thread.
Definition: ThreadStream.h:58
Contains icl_logging::LogStream.
void * memset(void *dest, int c, size_t count)
Definition: os_mem.h:46
signed short int16_t
Definition: msvc_stdint.h:89
#define PRINTF
icl_core::logging::LogLevel getLogLevel() const
Definition: ThreadStream.h:68
void setLineLogLevel(icl_core::logging::LogLevel line_log_level)
#define cDEFAULT_LOG_SIZE
Definition: Constants.h:36
ThreadStream & operator<<(ThreadStreamFunc func)
Definition: ThreadStream.h:134
unsigned short uint16_t
Definition: msvc_stdint.h:92
Contains icl_logging::LogOutputStream.
int snprintf(char *buffer, size_t maxlen, const char *format,...)
Definition: os_string.cpp:28


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