| 86 | } |
| 87 | |
| 88 | void AsyncWaitProcessor::ProcessThread(const std::function<void ()>& on_thread_initialize, const std::function<void ()>& on_thread_clenup) |
| 89 | { |
| 90 | // Call the thread initialize handler |
| 91 | assert((on_thread_initialize) && "Thread initialize handler must be valid!"); |
| 92 | if (on_thread_initialize) |
| 93 | on_thread_initialize(); |
| 94 | |
| 95 | try |
| 96 | { |
| 97 | // Thread local logger records to process |
| 98 | thread_local std::vector<Record> records; |
| 99 | thread_local uint64_t previous = 0; |
| 100 | |
| 101 | // Reserve initial space for logging records |
| 102 | records.reserve(_queue.capacity()); |
| 103 | |
| 104 | while (_started) |
| 105 | { |
| 106 | // Dequeue the next logging record |
| 107 | if (!_queue.Dequeue(records)) |
| 108 | return; |
| 109 | |
| 110 | // Current timestamp |
| 111 | uint64_t current = 0; |
| 112 | |
| 113 | // Process all logging records |
| 114 | for (auto& record : records) |
| 115 | { |
| 116 | // Handle stop operation record |
| 117 | if (record.timestamp == 0) |
| 118 | return; |
| 119 | |
| 120 | // Handle flush operation record |
| 121 | if (record.timestamp == 1) |
| 122 | { |
| 123 | // Flush the logging processor |
| 124 | Processor::Flush(); |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // Process logging record |
| 129 | Processor::ProcessRecord(record); |
| 130 | |
| 131 | // Find the latest record timestamp |
| 132 | if (record.timestamp > current) |
| 133 | current = record.timestamp; |
| 134 | } |
| 135 | |
| 136 | // Handle auto-flush period |
| 137 | if (CppCommon::Timespan((int64_t)(current - previous)).seconds() > 1) |
| 138 | { |
| 139 | // Flush the logging processor |
| 140 | Processor::Flush(); |
| 141 | |
| 142 | // Update the previous timestamp |
| 143 | previous = current; |
| 144 | } |
| 145 | } |