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