00001
00002
00009
00010 #include "icl_core_logging/ThreadStream.h"
00011
00012 #include <icl_core/internal_raw_debug.h>
00013 #include <icl_core/os_lxrt.h>
00014 #include "icl_core_logging/logging_ns.h"
00015 #include "icl_core_logging/LogStream.h"
00016 #include "icl_core_logging/LogOutputStream.h"
00017
00018 #include <stdarg.h>
00019 #include <string.h>
00020
00021 #if defined _SYSTEM_POSIX_
00022 # include "icl_core_logging/stream_operators_impl_posix.h"
00023 #elif defined _SYSTEM_WIN32_
00024
00025
00026 #else
00027 # error "No ::icl_core::logging namespace implementation defined for this platform."
00028 #endif
00029
00030 #if defined ICL_CORE_QT_SUPPORT
00031 #include <QtCore/QString>
00032 #endif
00033
00034 namespace icl_core {
00035 namespace logging {
00036
00037 void ThreadStream::setClassname(const char *classname)
00038 {
00039 strncpy(m_classname, classname, cDEFAULT_LOG_SIZE);
00040 }
00041
00042 void ThreadStream::setFilename(const char *filename)
00043 {
00044 strncpy(m_filename, filename, cDEFAULT_LOG_SIZE);
00045 }
00046
00047 void ThreadStream::setObjectname(const char *objectname)
00048 {
00049 strncpy(m_objectname, objectname, cDEFAULT_LOG_SIZE);
00050 }
00051
00052 void ThreadStream::setFunction(const char *function)
00053 {
00054 strncpy(m_function, function, cDEFAULT_LOG_SIZE);
00055 }
00056
00057 void ThreadStream::setLine(size_t line)
00058 {
00059 m_line = line;
00060 }
00061
00062 void ThreadStream::setLineLogLevel(icl_core::logging::LogLevel line_log_level)
00063 {
00064 m_line_log_level = line_log_level;
00065 }
00066
00067 void ThreadStream::write(const char *source, size_t number_of_bytes, size_t protected_buffer_size)
00068 {
00069
00070 size_t writable_length = cDEFAULT_LOG_SIZE - m_write_index - 1;
00071 if (number_of_bytes + protected_buffer_size > writable_length)
00072 {
00073 if (writable_length > protected_buffer_size)
00074 {
00075 number_of_bytes = writable_length - protected_buffer_size;
00076 }
00077 else
00078 {
00079 number_of_bytes = 0;
00080 }
00081 }
00082 memcpy(&m_data[m_write_index], source, number_of_bytes);
00083
00084 m_write_index += number_of_bytes;
00085 }
00086
00087 void ThreadStream::printf(const char *fmt, ...)
00088 {
00089
00090 size_t writable_length = cDEFAULT_LOG_SIZE - m_write_index - 1;
00091
00092 va_list argptr;
00093 va_start(argptr, fmt);
00094 int32_t bytes_printed = vsnprintf(&m_data[m_write_index], writable_length, fmt, argptr);
00095 va_end(argptr);
00096
00097 if (bytes_printed >= 0)
00098 {
00099 if (size_t(bytes_printed) > writable_length)
00100 {
00101 m_write_index += writable_length;
00102 }
00103 else
00104 {
00105 m_write_index += bytes_printed;
00106 }
00107 }
00108
00109 flush();
00110 }
00111
00112 void ThreadStream::flush()
00113 {
00114 m_data[m_write_index] = '\0';
00115 if (m_parent->m_mutex.wait())
00116 {
00117 for (std::set<LogOutputStream*>::const_iterator iter = m_parent->m_output_stream_list.begin();
00118 iter != m_parent->m_output_stream_list.end();
00119 ++iter)
00120 {
00121 (*iter)->push(m_line_log_level, m_parent->nameCStr(), m_filename, m_line,
00122 m_classname, m_objectname, m_function, m_data);
00123 }
00124
00125 m_parent->releaseThreadStream(this);
00126
00127 m_parent->m_mutex.post();
00128 }
00129 else
00130 {
00131 PRINTF("ThreadStream(%s)::Flush: mutex lock failed\n", m_parent->nameCStr());
00132 }
00133 m_write_index = 0;
00134 }
00135
00136 ThreadStream::ThreadStream(LogStream *parent)
00137 : m_parent(parent),
00138 m_level(parent->m_initial_level),
00139 m_line_log_level(parent->m_initial_level),
00140 m_line(0),
00141 m_write_index(0)
00142 {
00143 memset(m_classname, 0, cDEFAULT_LOG_SIZE + 1);
00144 memset(m_function, 0, cDEFAULT_LOG_SIZE + 1);
00145 memset(m_data, 0, cDEFAULT_LOG_SIZE + 1);
00146 }
00147
00148
00149 ThreadStream& operator << (ThreadStream& stream, uint8_t value)
00150 {
00151 char local_buffer[8];
00152 size_t length = os::snprintf(local_buffer, 8, "%hu", static_cast<uint8_t>(value));
00153 stream.write(local_buffer, length);
00154 return stream;
00155 }
00156
00157 ThreadStream& operator << (ThreadStream& stream, uint16_t value)
00158 {
00159 char local_buffer[8];
00160 size_t length = os::snprintf(local_buffer, 8, "%hu", value);
00161 stream.write(local_buffer, length);
00162 return stream;
00163 }
00164
00165 ThreadStream& operator << (ThreadStream& stream, uint32_t value)
00166 {
00167 char local_buffer[13];
00168 size_t length = os::snprintf(local_buffer, 13, "%u", value);
00169 stream.write(local_buffer, length);
00170 return stream;
00171 }
00172
00173 #if __WORDSIZE != 64
00174 ThreadStream& operator << (ThreadStream& stream, unsigned long value)
00175 {
00176 char local_buffer[13];
00177 size_t length = os::snprintf(local_buffer, 13, "%lu", value);
00178 stream.write(local_buffer, length);
00179 return stream;
00180 }
00181 #endif
00182
00183 ThreadStream& operator << (ThreadStream& stream, uint64_t value)
00184 {
00185 char local_buffer[23];
00186 size_t length = os::snprintf(local_buffer, 23, "%llu", value);
00187 stream.write(local_buffer, length);
00188 return stream;
00189 }
00190
00191 ThreadStream& operator << (ThreadStream& stream, int8_t value)
00192 {
00193 char local_buffer[8];
00194 size_t length = os::snprintf(local_buffer, 8, "%hd", static_cast<int16_t>(value));
00195 stream.write(local_buffer, length);
00196 return stream;
00197 }
00198
00199 ThreadStream& operator << (ThreadStream& stream, int16_t value)
00200 {
00201 char local_buffer[8];
00202 size_t length = os::snprintf(local_buffer, 8, "%hd", value);
00203 stream.write(local_buffer, length);
00204 return stream;
00205 }
00206
00207 ThreadStream& operator << (ThreadStream& stream, int32_t value)
00208 {
00209 char local_buffer[13];
00210 size_t length = os::snprintf(local_buffer, 13, "%d", value);
00211 stream.write(local_buffer, length);
00212 return stream;
00213 }
00214
00215 #if __WORDSIZE != 64
00216 ThreadStream& operator << (ThreadStream& stream, long value)
00217 {
00218 char local_buffer[13];
00219 size_t length = os::snprintf(local_buffer, 13, "%ld", value);
00220 stream.write(local_buffer, length);
00221 return stream;
00222 }
00223 #endif
00224
00225 ThreadStream& operator << (ThreadStream& stream, int64_t value)
00226 {
00227 char local_buffer[23];
00228 size_t length = os::snprintf(local_buffer, 23, "%lld", value);
00229 stream.write(local_buffer, length);
00230 return stream;
00231 }
00232
00233 #ifdef _SYSTEM_DARWIN_
00234 ThreadStream& operator << (ThreadStream& stream, size_t value)
00235 {
00236 char local_buffer[23];
00237 size_t length = os::snprintf(local_buffer, 23, "%zu", value);
00238 stream.write(local_buffer, length);
00239 return stream;
00240 }
00241 #endif
00242
00243 ThreadStream& operator << (ThreadStream& stream, const char *text)
00244 {
00245 stream.write(text, strlen(text));
00246 return stream;
00247 }
00248
00249 ThreadStream& operator << (ThreadStream& stream, const std::string& text)
00250 {
00251 stream.write(text.c_str(), text.size());
00252 return stream;
00253 }
00254
00255 ThreadStream& operator << (ThreadStream& stream, double value)
00256 {
00257 char local_buffer[100];
00258 size_t length = os::snprintf(local_buffer, 100, "%f", value);
00259 stream.write(local_buffer, length);
00260 return stream;
00261 }
00262
00263 ThreadStream& operator << (ThreadStream& stream, bool value)
00264 {
00265 if (value)
00266 {
00267 stream.write("True", 4);
00268 }
00269 else
00270 {
00271 stream.write("False", 5);
00272 }
00273 return stream;
00274 }
00275
00276 ThreadStream& operator << (ThreadStream& stream, void * value)
00277 {
00278 char local_buffer[25];
00279 size_t length = os::snprintf(local_buffer, 25, "%p", value);
00280 stream.write(local_buffer, length);
00281 return stream;
00282 }
00283
00284 #ifndef _SYSTEM_WIN32_
00285 ThreadStream& operator << (ThreadStream& stream, const ThreadId& thread_id)
00286 {
00287 return ICL_CORE_LOGGING_IMPL_NS::operator << (stream, thread_id);
00288 }
00289 #endif
00290
00291 #if defined ICL_CORE_QT_SUPPORT
00292 ThreadStream& operator << (ThreadStream& stream, const QString& value)
00293 {
00294 return operator << (stream, value.toLatin1().constData());
00295 }
00296 #endif
00297
00298
00300 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00301
00305 void ThreadStream::ChangeLevel(icl_core::logging::LogLevel level)
00306 {
00307 changeLevel(level);
00308 }
00309
00313 ICL_CORE_VC_DEPRECATE_STYLE_USE(ThreadStream::getLogLevel)
00314 icl_core::logging::LogLevel ThreadStream::LogLevel() const
00315 {
00316 return getLogLevel();
00317 }
00318
00322 void ThreadStream::SetClassname(const char *classname)
00323 {
00324 setClassname(classname);
00325 }
00326
00330 void ThreadStream::SetObjectname(const char *objectname)
00331 {
00332 setObjectname(objectname);
00333 }
00334
00338 void ThreadStream::SetFilename(const char *filename)
00339 {
00340 setFilename(filename);
00341 }
00342
00346 void ThreadStream::SetFunction(const char *function)
00347 {
00348 setFunction(function);
00349 }
00350
00354 void ThreadStream::SetLine(size_t line)
00355 {
00356 setLine(line);
00357 }
00358
00362 void ThreadStream::SetLineLogLevel(icl_core::logging::LogLevel line_log_level)
00363 {
00364 setLineLogLevel(line_log_level);
00365 }
00366
00371 void ThreadStream::Write(const char *buffer, size_t number_of_bytes,
00372 size_t protected_buffer_size)
00373 {
00374 write(buffer, number_of_bytes, protected_buffer_size);
00375 }
00376
00381 void ThreadStream::Printf(const char *fmt, ...)
00382 {
00383
00384 size_t writable_length = cDEFAULT_LOG_SIZE - m_write_index - 1;
00385
00386 va_list argptr;
00387 va_start(argptr, fmt);
00388 int32_t bytes_printed = vsnprintf(&m_data[m_write_index], writable_length, fmt, argptr);
00389 va_end(argptr);
00390
00391 if (bytes_printed >= 0)
00392 {
00393 if (size_t(bytes_printed) > writable_length)
00394 {
00395 m_write_index += writable_length;
00396 }
00397 else
00398 {
00399 m_write_index += bytes_printed;
00400 }
00401 }
00402
00403 flush();
00404 }
00405
00410 void ThreadStream::Flush()
00411 {
00412 flush();
00413 }
00414
00415 #endif
00416
00417
00418 }
00419 }