| 6 | #include "windows.h" |
| 7 | #include <ctime> |
| 8 | #else // WEBRTC_WIN |
| 9 | #include <sys/time.h> |
| 10 | #endif // WEBRTC_WIN |
| 11 |
| 12 | namespace tgcalls { |
| 13 |
| 14 | LogSinkImpl::LogSinkImpl(const FilePath &logPath) { |
| 15 | if (!logPath.data.empty()) { |
| 16 | _file.open(logPath.data); |
| 17 | } |
| 18 | } |
| 19 |
| 20 | void LogSinkImpl::OnLogMessage(const std::string &msg, rtc::LoggingSeverity severity, const char *tag) { |
| 21 | OnLogMessage(std::string(tag) + ": " + msg); |
| 22 | } |
| 23 |
| 24 | void LogSinkImpl::OnLogMessage(const std::string &message, rtc::LoggingSeverity severity) { |
| 25 | OnLogMessage(message); |
| 26 | } |
| 27 |
| 28 | void LogSinkImpl::OnLogMessage(const std::string &message) { |
| 29 | time_t rawTime; |
| 30 | time(&rawTime); |
| 31 | struct tm timeinfo; |
| 32 |
| 33 | #ifdef WEBRTC_WIN |
| 34 | localtime_s(&timeinfo, &rawTime); |
| 35 |
| 36 | FILETIME ft; |
| 37 | unsigned __int64 full = 0; |
| 38 | GetSystemTimeAsFileTime(&ft); |
| 39 |
| 40 | full |= ft.dwHighDateTime; |
| 41 | full <<= 32; |
| 42 | full |= ft.dwLowDateTime; |
| 43 |
| 44 | const auto deltaEpochInMicrosecs = 11644473600000000Ui64; |
| 45 | full -= deltaEpochInMicrosecs; |
| 46 | full /= 10; |
| 47 | int32_t milliseconds = (long)(full % 1000000UL) / 1000; |
| 48 | #else |
| 49 | timeval curTime = { 0 }; |
| 50 | localtime_r(&rawTime, &timeinfo); |
| 51 | gettimeofday(&curTime, nullptr); |
| 52 | int32_t milliseconds = curTime.tv_usec / 1000; |
| 53 | #endif |
| 54 |
| 55 | auto &stream = _file.is_open() ? (std::ostream&)_file : _data; |
| 56 | stream |
| 57 | << (timeinfo.tm_year + 1900) |
| 58 | << "-" << (timeinfo.tm_mon + 1) |
| 59 | << "-" << (timeinfo.tm_mday) |
| 60 | << " " << timeinfo.tm_hour |
| 61 | << ":" << timeinfo.tm_min |
| 62 | << ":" << timeinfo.tm_sec |
| 63 | << ":" << milliseconds |
| 64 | << " " << message; |
| 65 |
| 66 | #if DEBUG |
| 67 | printf("%d-%d-%d %d:%d:%d:%d %s\n", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, milliseconds, message.c_str()); |
| 68 | #endif |
| 69 | } |
| 70 |
| 71 | } // namespace tgcalls |
| 72 |