| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vdebug.h" |
| 20 |
| 21 | #ifdef LOTTIE_LOGGING_SUPPORT |
| 22 |
| 23 | #include <atomic> |
| 24 | #include <chrono> |
| 25 | #include <cstring> |
| 26 | #include <ctime> |
| 27 | #include <fstream> |
| 28 | #include <memory> |
| 29 | #include <queue> |
| 30 | #include <sstream> |
| 31 | #include <thread> |
| 32 | #include <tuple> |
| 33 |
| 34 | namespace { |
| 35 |
| 36 | /* Returns microseconds since epoch */ |
| 37 | uint64_t timestamp_now() |
| 38 | { |
| 39 | return std::chrono::duration_cast<std::chrono::microseconds>( |
| 40 | std::chrono::high_resolution_clock::now().time_since_epoch()) |
| 41 | .count(); |
| 42 | } |
| 43 |
| 44 | /* I want [2016-10-13 00:01:23.528514] */ |
| 45 | void format_timestamp(std::ostream& os, uint64_t timestamp) |
| 46 | { |
| 47 | // The next 3 lines do not work on MSVC! |
| 48 | // auto duration = std::chrono::microseconds(timestamp); |
| 49 | // std::chrono::high_resolution_clock::time_point time_point(duration); |
| 50 | // std::time_t time_t = |
| 51 | // std::chrono::high_resolution_clock::to_time_t(time_point); |
| 52 | std::time_t time_t = timestamp / 1000000; |
| 53 | auto gmtime = std::gmtime(&time_t); |
| 54 | char buffer[32]; |
| 55 | strftime(buffer, 32, "%Y-%m-%d %T.", gmtime); |
| 56 | char microseconds[7]; |
| 57 | snprintf(microseconds, 7, "%06llu", |
| 58 | (long long unsigned int)timestamp % 1000000); |
| 59 | os << '[' << buffer << microseconds << ']'; |
| 60 | } |
| 61 |
| 62 | std::thread::id this_thread_id() |
| 63 | { |
| 64 | static thread_local const std::thread::id id = std::this_thread::get_id(); |
| 65 | return id; |
| 66 | } |
| 67 |
| 68 | template <typename T, typename Tuple> |
| 69 | struct TupleIndex; |
| 70 |
| 71 | template <typename T, typename... Types> |
| 72 | struct TupleIndex<T, std::tuple<T, Types...> > { |
| 73 | static constexpr const std::size_t value = 0; |
| 74 | }; |
| 75 |
| 76 | template <typename T, typename U, typename... Types> |
| 77 | struct TupleIndex<T, std::tuple<U, Types...> > { |
| 78 | static constexpr const std::size_t value = |
| 79 | 1 + TupleIndex<T, std::tuple<Types...> >::value; |
| 80 | }; |
| 81 |
| 82 | } // anonymous namespace |
| 83 |
| 84 | typedef std::tuple<char, uint32_t, uint64_t, int32_t, int64_t, double, |
| 85 | VDebug::string_literal_t, char*> |
| 86 | SupportedTypes; |
| 87 |
| 88 | char const* to_string(LogLevel loglevel) |
| 89 | { |
| 90 | switch (loglevel) { |
| 91 | case LogLevel::OFF: |
| 92 | return "OFF"; |
| 93 | case LogLevel::INFO: |
| 94 | return "INFO"; |
| 95 | case LogLevel::WARN: |
| 96 | return "WARN"; |
| 97 | case LogLevel::CRIT: |
| 98 | return "CRIT"; |
| 99 | } |
| 100 | return "XXXX"; |
| 101 | } |
| 102 |
| 103 | template <typename Arg> |
| 104 | void VDebug::encode(Arg arg) |
| 105 | { |
| 106 | *reinterpret_cast<Arg*>(buffer()) = arg; |
| 107 | m_bytes_used += sizeof(Arg); |
| 108 | } |
| 109 |
| 110 | template <typename Arg> |
| 111 | void VDebug::encode(Arg arg, uint8_t type_id) |
| 112 | { |
| 113 | resize_buffer_if_needed(sizeof(Arg) + sizeof(uint8_t)); |
| 114 | encode<uint8_t>(type_id); |
| 115 | encode<Arg>(arg); |
| 116 | } |
| 117 |
| 118 | VDebug::VDebug(LogLevel level, char const* file, char const* function, |
| 119 | uint32_t line) |
| 120 | : m_bytes_used(0), m_buffer_size(sizeof(m_stack_buffer)) |
| 121 | { |
| 122 | encode<uint64_t>(timestamp_now()); |
| 123 | encode<std::thread::id>(this_thread_id()); |
| 124 | encode<string_literal_t>(string_literal_t(file)); |
| 125 | encode<string_literal_t>(string_literal_t(function)); |
| 126 | encode<uint32_t>(line); |
| 127 | encode<LogLevel>(level); |
| 128 | if (level == LogLevel::INFO) { |
| 129 | m_logAll = false; |
| 130 | } else { |
| 131 | m_logAll = true; |
| 132 | } |
| 133 | } |
| 134 |
| 135 | VDebug::~VDebug() = default; |
| 136 |
| 137 | void VDebug::stringify(std::ostream& os) |
| 138 | { |
| 139 | char* b = !m_heap_buffer ? m_stack_buffer : m_heap_buffer.get(); |
| 140 | char const* const end = b + m_bytes_used; |
| 141 | uint64_t timestamp = *reinterpret_cast<uint64_t*>(b); |
| 142 | b += sizeof(uint64_t); |
| 143 | std::thread::id threadid = *reinterpret_cast<std::thread::id*>(b); |
| 144 | b += sizeof(std::thread::id); |
| 145 | string_literal_t file = *reinterpret_cast<string_literal_t*>(b); |
| 146 | b += sizeof(string_literal_t); |
| 147 | string_literal_t function = *reinterpret_cast<string_literal_t*>(b); |
| 148 | b += sizeof(string_literal_t); |
| 149 | uint32_t line = *reinterpret_cast<uint32_t*>(b); |
| 150 | b += sizeof(uint32_t); |
| 151 | LogLevel loglevel = *reinterpret_cast<LogLevel*>(b); |
| 152 | b += sizeof(LogLevel); |
| 153 | if (m_logAll) { |
| 154 | format_timestamp(os, timestamp); |
| 155 |
| 156 | os << '[' << to_string(loglevel) << ']' << '[' << threadid << ']' << '[' |
| 157 | << file.m_s << ':' << function.m_s << ':' << line << "] "; |
| 158 | } |
| 159 |
| 160 | stringify(os, b, end); |
| 161 | os << std::endl; |
| 162 |
| 163 | if (loglevel >= LogLevel::CRIT) os.flush(); |
| 164 | } |
| 165 |
| 166 | template <typename Arg> |
| 167 | char* decode(std::ostream& os, char* b, Arg* /*dummy*/) |
| 168 | { |
| 169 | Arg arg = *reinterpret_cast<Arg*>(b); |
| 170 | os << arg; |
| 171 | return b + sizeof(Arg); |
| 172 | } |
| 173 |
| 174 | template <> |
| 175 | char* decode(std::ostream& os, char* b, VDebug::string_literal_t* /*dummy*/) |
| 176 | { |
| 177 | VDebug::string_literal_t s = |
| 178 | *reinterpret_cast<VDebug::string_literal_t*>(b); |
| 179 | os << s.m_s; |
| 180 | return b + sizeof(VDebug::string_literal_t); |
| 181 | } |
| 182 |
| 183 | template <> |
| 184 | char* decode(std::ostream& os, char* b, char** /*dummy*/) |
| 185 | { |
| 186 | while (*b != '\0') { |
| 187 | os << *b; |
| 188 | ++b; |
| 189 | } |
| 190 | return ++b; |
| 191 | } |
| 192 |
| 193 | void VDebug::stringify(std::ostream& os, char* start, char const* const end) |
| 194 | { |
| 195 | if (start == end) return; |
| 196 |
| 197 | int type_id = static_cast<int>(*start); |
| 198 | start++; |
| 199 |
| 200 | switch (type_id) { |
| 201 | case 0: |
| 202 | stringify( |
| 203 | os, |
| 204 | decode(os, start, |
| 205 | static_cast<std::tuple_element<0, SupportedTypes>::type*>( |
| 206 | nullptr)), |
| 207 | end); |
| 208 | return; |
| 209 | case 1: |
| 210 | stringify( |
| 211 | os, |
| 212 | decode(os, start, |
| 213 | static_cast<std::tuple_element<1, SupportedTypes>::type*>( |
| 214 | nullptr)), |
| 215 | end); |
| 216 | return; |
| 217 | case 2: |
| 218 | stringify( |
| 219 | os, |
| 220 | decode(os, start, |
| 221 | static_cast<std::tuple_element<2, SupportedTypes>::type*>( |
| 222 | nullptr)), |
| 223 | end); |
| 224 | return; |
| 225 | case 3: |
| 226 | stringify( |
| 227 | os, |
| 228 | decode(os, start, |
| 229 | static_cast<std::tuple_element<3, SupportedTypes>::type*>( |
| 230 | nullptr)), |
| 231 | end); |
| 232 | return; |
| 233 | case 4: |
| 234 | stringify( |
| 235 | os, |
| 236 | decode(os, start, |
| 237 | static_cast<std::tuple_element<4, SupportedTypes>::type*>( |
| 238 | nullptr)), |
| 239 | end); |
| 240 | return; |
| 241 | case 5: |
| 242 | stringify( |
| 243 | os, |
| 244 | decode(os, start, |
| 245 | static_cast<std::tuple_element<5, SupportedTypes>::type*>( |
| 246 | nullptr)), |
| 247 | end); |
| 248 | return; |
| 249 | case 6: |
| 250 | stringify( |
| 251 | os, |
| 252 | decode(os, start, |
| 253 | static_cast<std::tuple_element<6, SupportedTypes>::type*>( |
| 254 | nullptr)), |
| 255 | end); |
| 256 | return; |
| 257 | case 7: |
| 258 | stringify( |
| 259 | os, |
| 260 | decode(os, start, |
| 261 | static_cast<std::tuple_element<7, SupportedTypes>::type*>( |
| 262 | nullptr)), |
| 263 | end); |
| 264 | return; |
| 265 | } |
| 266 | } |
| 267 |
| 268 | char* VDebug::buffer() |
| 269 | { |
| 270 | return !m_heap_buffer ? &m_stack_buffer[m_bytes_used] |
| 271 | : &(m_heap_buffer.get())[m_bytes_used]; |
| 272 | } |
| 273 |
| 274 | void VDebug::resize_buffer_if_needed(size_t additional_bytes) |
| 275 | { |
| 276 | size_t const required_size = m_bytes_used + additional_bytes; |
| 277 |
| 278 | if (required_size <= m_buffer_size) return; |
| 279 |
| 280 | if (!m_heap_buffer) { |
| 281 | m_buffer_size = std::max(static_cast<size_t>(512), required_size); |
| 282 | m_heap_buffer = std::make_unique<char[]>(m_buffer_size); |
| 283 | memcpy(m_heap_buffer.get(), m_stack_buffer, m_bytes_used); |
| 284 | return; |
| 285 | } else { |
| 286 | m_buffer_size = |
| 287 | std::max(static_cast<size_t>(2 * m_buffer_size), required_size); |
| 288 | std::unique_ptr<char[]> new_heap_buffer(new char[m_buffer_size]); |
| 289 | memcpy(new_heap_buffer.get(), m_heap_buffer.get(), m_bytes_used); |
| 290 | m_heap_buffer.swap(new_heap_buffer); |
| 291 | } |
| 292 | } |
| 293 |
| 294 | void VDebug::encode(char const* arg) |
| 295 | { |
| 296 | if (arg != nullptr) encode_c_string(arg, strlen(arg)); |
| 297 | } |
| 298 |
| 299 | void VDebug::encode(char* arg) |
| 300 | { |
| 301 | if (arg != nullptr) encode_c_string(arg, strlen(arg)); |
| 302 | } |
| 303 |
| 304 | void VDebug::encode_c_string(char const* arg, size_t length) |
| 305 | { |
| 306 | if (length == 0) return; |
| 307 |
| 308 | resize_buffer_if_needed(1 + length + 1); |
| 309 | char* b = buffer(); |
| 310 | auto type_id = TupleIndex<char*, SupportedTypes>::value; |
| 311 | *reinterpret_cast<uint8_t*>(b++) = static_cast<uint8_t>(type_id); |
| 312 | memcpy(b, arg, length + 1); |
| 313 | m_bytes_used += 1 + length + 1; |
| 314 | } |
| 315 |
| 316 | void VDebug::encode(string_literal_t arg) |
| 317 | { |
| 318 | encode<string_literal_t>( |
| 319 | arg, TupleIndex<string_literal_t, SupportedTypes>::value); |
| 320 | } |
| 321 |
| 322 | VDebug& VDebug::operator<<(std::string const& arg) |
| 323 | { |
| 324 | encode_c_string(arg.c_str(), arg.length()); |
| 325 | return *this; |
| 326 | } |
| 327 |
| 328 | VDebug& VDebug::operator<<(int32_t arg) |
| 329 | { |
| 330 | encode<int32_t>(arg, TupleIndex<int32_t, SupportedTypes>::value); |
| 331 | return *this; |
| 332 | } |
| 333 |
| 334 | VDebug& VDebug::operator<<(uint32_t arg) |
| 335 | { |
| 336 | encode<uint32_t>(arg, TupleIndex<uint32_t, SupportedTypes>::value); |
| 337 | return *this; |
| 338 | } |
| 339 |
| 340 | // VDebug& VDebug::operator<<(int64_t arg) |
| 341 | // { |
| 342 | // encode < int64_t >(arg, TupleIndex < int64_t, SupportedTypes >::value); |
| 343 | // return *this; |
| 344 | // } |
| 345 |
| 346 | // VDebug& VDebug::operator<<(uint64_t arg) |
| 347 | // { |
| 348 | // encode < uint64_t >(arg, TupleIndex < uint64_t, SupportedTypes >::value); |
| 349 | // return *this; |
| 350 | // } |
| 351 | VDebug& VDebug::operator<<(unsigned long arg) |
| 352 | { |
| 353 | encode<uint64_t>(arg, TupleIndex<uint64_t, SupportedTypes>::value); |
| 354 | return *this; |
| 355 | } |
| 356 |
| 357 | VDebug& VDebug::operator<<(long arg) |
| 358 | { |
| 359 | encode<int64_t>(arg, TupleIndex<int64_t, SupportedTypes>::value); |
| 360 | return *this; |
| 361 | } |
| 362 |
| 363 | VDebug& VDebug::operator<<(double arg) |
| 364 | { |
| 365 | encode<double>(arg, TupleIndex<double, SupportedTypes>::value); |
| 366 | return *this; |
| 367 | } |
| 368 |
| 369 | VDebug& VDebug::operator<<(char arg) |
| 370 | { |
| 371 | encode<char>(arg, TupleIndex<char, SupportedTypes>::value); |
| 372 | return *this; |
| 373 | } |
| 374 |
| 375 | struct BufferBase { |
| 376 | virtual ~BufferBase() = default; |
| 377 | virtual void push(VDebug&& logline) = 0; |
| 378 | virtual bool try_pop(VDebug& logline) = 0; |
| 379 | }; |
| 380 |
| 381 | struct SpinLock { |
| 382 | SpinLock(std::atomic_flag& flag) : m_flag(flag) |
| 383 | { |
| 384 | while (m_flag.test_and_set(std::memory_order_acquire)) |
| 385 | ; |
| 386 | } |
| 387 |
| 388 | ~SpinLock() { m_flag.clear(std::memory_order_release); } |
| 389 |
| 390 | private: |
| 391 | std::atomic_flag& m_flag; |
| 392 | }; |
| 393 |
| 394 | /* Multi Producer Single Consumer Ring Buffer */ |
| 395 | class RingBuffer : public BufferBase { |
| 396 | public: |
| 397 | struct alignas(64) Item { |
| 398 | Item() |
| 399 | : flag(), written(0), logline(LogLevel::INFO, nullptr, nullptr, 0) |
| 400 | { |
| 401 | } |
| 402 |
| 403 | std::atomic_flag flag; |
| 404 | char written; |
| 405 | char padding[256 - sizeof(std::atomic_flag) - sizeof(char) - |
| 406 | sizeof(VDebug)]; |
| 407 | VDebug logline; |
| 408 | }; |
| 409 |
| 410 | RingBuffer(size_t const size) |
| 411 | : m_size(size), |
| 412 | m_ring(static_cast<Item*>(std::malloc(size * sizeof(Item)))), |
| 413 | m_write_index(0), |
| 414 | m_read_index(0) |
| 415 | { |
| 416 | for (size_t i = 0; i < m_size; ++i) { |
| 417 | new (&m_ring[i]) Item(); |
| 418 | } |
| 419 | static_assert(sizeof(Item) == 256, "Unexpected size != 256"); |
| 420 | } |
| 421 |
| 422 | ~RingBuffer() override |
| 423 | { |
| 424 | for (size_t i = 0; i < m_size; ++i) { |
| 425 | m_ring[i].~Item(); |
| 426 | } |
| 427 | std::free(m_ring); |
| 428 | } |
| 429 |
| 430 | void push(VDebug&& logline) override |
| 431 | { |
| 432 | unsigned int write_index = |
| 433 | m_write_index.fetch_add(1, std::memory_order_relaxed) % m_size; |
| 434 | Item& item = m_ring[write_index]; |
| 435 | SpinLock spinlock(item.flag); |
| 436 | item.logline = std::move(logline); |
| 437 | item.written = 1; |
| 438 | } |
| 439 |
| 440 | bool try_pop(VDebug& logline) override |
| 441 | { |
| 442 | Item& item = m_ring[m_read_index % m_size]; |
| 443 | SpinLock spinlock(item.flag); |
| 444 | if (item.written == 1) { |
| 445 | logline = std::move(item.logline); |
| 446 | item.written = 0; |
| 447 | ++m_read_index; |
| 448 | return true; |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 |
| 453 | RingBuffer(RingBuffer const&) = delete; |
| 454 | RingBuffer& operator=(RingBuffer const&) = delete; |
| 455 |
| 456 | private: |
| 457 | size_t const m_size; |
| 458 | Item* m_ring; |
| 459 | std::atomic<unsigned int> m_write_index; |
| 460 |
| 461 | public: |
| 462 | char pad[64]; |
| 463 |
| 464 | private: |
| 465 | unsigned int m_read_index; |
| 466 | }; |
| 467 |
| 468 | class Buffer { |
| 469 | public: |
| 470 | struct Item { |
| 471 | Item(VDebug&& logline) : logline(std::move(logline)) {} |
| 472 | char padding[256 - sizeof(VDebug)]; |
| 473 | VDebug logline; |
| 474 | }; |
| 475 |
| 476 | static constexpr const size_t size = |
| 477 | 32768; // 8MB. Helps reduce memory fragmentation |
| 478 |
| 479 | Buffer() : m_buffer(static_cast<Item*>(std::malloc(size * sizeof(Item)))) |
| 480 | { |
| 481 | for (size_t i = 0; i <= size; ++i) { |
| 482 | m_write_state[i].store(0, std::memory_order_relaxed); |
| 483 | } |
| 484 | static_assert(sizeof(Item) == 256, "Unexpected size != 256"); |
| 485 | } |
| 486 |
| 487 | ~Buffer() |
| 488 | { |
| 489 | unsigned int write_count = m_write_state[size].load(); |
| 490 | for (size_t i = 0; i < write_count; ++i) { |
| 491 | m_buffer[i].~Item(); |
| 492 | } |
| 493 | std::free(m_buffer); |
| 494 | } |
| 495 |
| 496 | // Returns true if we need to switch to next buffer |
| 497 | bool push(VDebug&& logline, unsigned int const write_index) |
| 498 | { |
| 499 | new (&m_buffer[write_index]) Item(std::move(logline)); |
| 500 | m_write_state[write_index].store(1, std::memory_order_release); |
| 501 | return m_write_state[size].fetch_add(1, std::memory_order_acquire) + |
| 502 | 1 == |
| 503 | size; |
| 504 | } |
| 505 |
| 506 | bool try_pop(VDebug& logline, unsigned int const read_index) |
| 507 | { |
| 508 | if (m_write_state[read_index].load(std::memory_order_acquire)) { |
| 509 | Item& item = m_buffer[read_index]; |
| 510 | logline = std::move(item.logline); |
| 511 | return true; |
| 512 | } |
| 513 | return false; |
| 514 | } |
| 515 |
| 516 | Buffer(Buffer const&) = delete; |
| 517 | Buffer& operator=(Buffer const&) = delete; |
| 518 |
| 519 | private: |
| 520 | Item* m_buffer; |
| 521 | std::atomic<unsigned int> m_write_state[size + 1]; |
| 522 | }; |
| 523 |
| 524 | class QueueBuffer : public BufferBase { |
| 525 | public: |
| 526 | QueueBuffer(QueueBuffer const&) = delete; |
| 527 | QueueBuffer& operator=(QueueBuffer const&) = delete; |
| 528 |
| 529 | QueueBuffer() |
| 530 | : m_current_read_buffer{nullptr}, |
| 531 | m_write_index(0), |
| 532 | m_flag(), |
| 533 | m_read_index(0) |
| 534 | { |
| 535 | setup_next_write_buffer(); |
| 536 | } |
| 537 |
| 538 | void push(VDebug&& logline) override |
| 539 | { |
| 540 | unsigned int write_index = |
| 541 | m_write_index.fetch_add(1, std::memory_order_relaxed); |
| 542 | if (write_index < Buffer::size) { |
| 543 | if (m_current_write_buffer.load(std::memory_order_acquire) |
| 544 | ->push(std::move(logline), write_index)) { |
| 545 | setup_next_write_buffer(); |
| 546 | } |
| 547 | } else { |
| 548 | while (m_write_index.load(std::memory_order_acquire) >= |
| 549 | Buffer::size) |
| 550 | ; |
| 551 | push(std::move(logline)); |
| 552 | } |
| 553 | } |
| 554 |
| 555 | bool try_pop(VDebug& logline) override |
| 556 | { |
| 557 | if (m_current_read_buffer == nullptr) |
| 558 | m_current_read_buffer = get_next_read_buffer(); |
| 559 |
| 560 | Buffer* read_buffer = m_current_read_buffer; |
| 561 |
| 562 | if (read_buffer == nullptr) return false; |
| 563 |
| 564 | if (read_buffer->try_pop(logline, m_read_index)) { |
| 565 | m_read_index++; |
| 566 | if (m_read_index == Buffer::size) { |
| 567 | m_read_index = 0; |
| 568 | m_current_read_buffer = nullptr; |
| 569 | SpinLock spinlock(m_flag); |
| 570 | m_buffers.pop(); |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 |
| 575 | return false; |
| 576 | } |
| 577 |
| 578 | private: |
| 579 | void setup_next_write_buffer() |
| 580 | { |
| 581 | std::unique_ptr<Buffer> next_write_buffer(new Buffer()); |
| 582 | m_current_write_buffer.store(next_write_buffer.get(), |
| 583 | std::memory_order_release); |
| 584 | SpinLock spinlock(m_flag); |
| 585 | m_buffers.push(std::move(next_write_buffer)); |
| 586 | m_write_index.store(0, std::memory_order_relaxed); |
| 587 | } |
| 588 |
| 589 | Buffer* get_next_read_buffer() |
| 590 | { |
| 591 | SpinLock spinlock(m_flag); |
| 592 | return m_buffers.empty() ? nullptr : m_buffers.front().get(); |
| 593 | } |
| 594 |
| 595 | private: |
| 596 | std::queue<std::unique_ptr<Buffer> > m_buffers; |
| 597 | std::atomic<Buffer*> m_current_write_buffer; |
| 598 | Buffer* m_current_read_buffer; |
| 599 | std::atomic<unsigned int> m_write_index; |
| 600 | std::atomic_flag m_flag; |
| 601 | unsigned int m_read_index; |
| 602 | }; |
| 603 |
| 604 | class FileWriter { |
| 605 | public: |
| 606 | FileWriter(std::string const& log_directory, |
| 607 | std::string const& log_file_name, uint32_t log_file_roll_size_mb) |
| 608 | : m_log_file_roll_size_bytes(log_file_roll_size_mb * 1024 * 1024), |
| 609 | m_name(log_directory + log_file_name) |
| 610 | { |
| 611 | roll_file(); |
| 612 | } |
| 613 |
| 614 | void write(VDebug& logline) |
| 615 | { |
| 616 | auto pos = m_os->tellp(); |
| 617 | logline.stringify(*m_os); |
| 618 | m_bytes_written += m_os->tellp() - pos; |
| 619 | if (m_bytes_written > m_log_file_roll_size_bytes) { |
| 620 | roll_file(); |
| 621 | } |
| 622 | } |
| 623 |
| 624 | private: |
| 625 | void roll_file() |
| 626 | { |
| 627 | if (m_os) { |
| 628 | m_os->flush(); |
| 629 | m_os->close(); |
| 630 | } |
| 631 |
| 632 | m_bytes_written = 0; |
| 633 | m_os = std::make_unique<std::ofstream>(); |
| 634 | // TODO Optimize this part. Does it even matter ? |
| 635 | std::string log_file_name = m_name; |
| 636 | log_file_name.append("."); |
| 637 | log_file_name.append(std::to_string(++m_file_number)); |
| 638 | log_file_name.append(".txt"); |
| 639 | m_os->open(log_file_name, std::ofstream::out | std::ofstream::trunc); |
| 640 | } |
| 641 |
| 642 | private: |
| 643 | uint32_t m_file_number = 0; |
| 644 | std::streamoff m_bytes_written = 0; |
| 645 | uint32_t const m_log_file_roll_size_bytes; |
| 646 | std::string const m_name; |
| 647 | std::unique_ptr<std::ofstream> m_os; |
| 648 | }; |
| 649 |
| 650 | class NanoLogger { |
| 651 | public: |
| 652 | NanoLogger(NonGuaranteedLogger ngl, std::string const& log_directory, |
| 653 | std::string const& log_file_name, uint32_t log_file_roll_size_mb) |
| 654 | : m_state(State::INIT), |
| 655 | m_buffer_base( |
| 656 | new RingBuffer(std::max(1u, ngl.ring_buffer_size_mb) * 1024 * 4)), |
| 657 | m_file_writer(log_directory, log_file_name, |
| 658 | std::max(1u, log_file_roll_size_mb)), |
| 659 | m_thread(&NanoLogger::pop, this) |
| 660 | { |
| 661 | m_state.store(State::READY, std::memory_order_release); |
| 662 | } |
| 663 |
| 664 | NanoLogger(GuaranteedLogger /*gl*/, std::string const& log_directory, |
| 665 | std::string const& log_file_name, uint32_t log_file_roll_size_mb) |
| 666 | : m_state(State::INIT), |
| 667 | m_buffer_base(new QueueBuffer()), |
| 668 | m_file_writer(log_directory, log_file_name, |
| 669 | std::max(1u, log_file_roll_size_mb)), |
| 670 | m_thread(&NanoLogger::pop, this) |
| 671 | { |
| 672 | m_state.store(State::READY, std::memory_order_release); |
| 673 | } |
| 674 |
| 675 | ~NanoLogger() |
| 676 | { |
| 677 | m_state.store(State::SHUTDOWN); |
| 678 | m_thread.join(); |
| 679 | } |
| 680 |
| 681 | void add(VDebug&& logline) { m_buffer_base->push(std::move(logline)); } |
| 682 |
| 683 | void pop() |
| 684 | { |
| 685 | // Wait for constructor to complete and pull all stores done there to |
| 686 | // this thread / core. |
| 687 | while (m_state.load(std::memory_order_acquire) == State::INIT) |
| 688 | std::this_thread::sleep_for(std::chrono::microseconds(50)); |
| 689 |
| 690 | VDebug logline(LogLevel::INFO, nullptr, nullptr, 0); |
| 691 |
| 692 | while (m_state.load() == State::READY) { |
| 693 | if (m_buffer_base->try_pop(logline)) |
| 694 | m_file_writer.write(logline); |
| 695 | else |
| 696 | std::this_thread::sleep_for(std::chrono::microseconds(50)); |
| 697 | } |
| 698 |
| 699 | // Pop and log all remaining entries |
| 700 | while (m_buffer_base->try_pop(logline)) { |
| 701 | m_file_writer.write(logline); |
| 702 | } |
| 703 | } |
| 704 |
| 705 | private: |
| 706 | enum class State { INIT, READY, SHUTDOWN }; |
| 707 |
| 708 | std::atomic<State> m_state; |
| 709 | std::unique_ptr<BufferBase> m_buffer_base; |
| 710 | FileWriter m_file_writer; |
| 711 | std::thread m_thread; |
| 712 | }; |
| 713 |
| 714 | std::unique_ptr<NanoLogger> nanologger; |
| 715 | std::atomic<NanoLogger*> atomic_nanologger; |
| 716 |
| 717 | bool VDebugServer::operator==(VDebug& logline) |
| 718 | { |
| 719 | atomic_nanologger.load(std::memory_order_acquire)->add(std::move(logline)); |
| 720 | return true; |
| 721 | } |
| 722 |
| 723 | void initialize(NonGuaranteedLogger ngl, std::string const& log_directory, |
| 724 | std::string const& log_file_name, |
| 725 | uint32_t log_file_roll_size_mb) |
| 726 | { |
| 727 | nanologger = std::make_unique<NanoLogger>(ngl, log_directory, log_file_name, |
| 728 | log_file_roll_size_mb); |
| 729 | atomic_nanologger.store(nanologger.get(), std::memory_order_seq_cst); |
| 730 | } |
| 731 |
| 732 | void initialize(GuaranteedLogger gl, std::string const& log_directory, |
| 733 | std::string const& log_file_name, |
| 734 | uint32_t log_file_roll_size_mb) |
| 735 | { |
| 736 | nanologger = std::make_unique<NanoLogger>(gl, log_directory, log_file_name, |
| 737 | log_file_roll_size_mb); |
| 738 | atomic_nanologger.store(nanologger.get(), std::memory_order_seq_cst); |
| 739 | } |
| 740 |
| 741 | std::atomic<unsigned int> loglevel = {0}; |
| 742 |
| 743 | void set_log_level(LogLevel level) |
| 744 | { |
| 745 | loglevel.store(static_cast<unsigned int>(level), std::memory_order_release); |
| 746 | } |
| 747 |
| 748 | bool is_logged(LogLevel level) |
| 749 | { |
| 750 | return static_cast<unsigned int>(level) >= |
| 751 | loglevel.load(std::memory_order_relaxed); |
| 752 | } |
| 753 |
| 754 | #endif // LOTTIE_LOGGING_SUPPORT |
| 755 |