winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

TestHelper.cpp (1494B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 #include "TestHelper.h"
      5 
      6 #include <chrono>
      7 #include <filesystem>
      8 #include <iomanip>
      9 #include <mutex>
     10 #include <sstream>
     11 #include <time.h>
     12 
     13 #include <catch2/catch_test_macros.hpp>
     14 
     15 static std::mutex s_logMutex;
     16 
     17 namespace
     18 {
     19 std::string TimestampToString(std::chrono::time_point<std::chrono::system_clock> time)
     20 {
     21     using namespace std::chrono;
     22 
     23     // get number of milliseconds for the current second
     24     // (remainder after division into seconds)
     25     auto ms = duration_cast<milliseconds>(time.time_since_epoch()) % 1000;
     26 
     27     auto timer = system_clock::to_time_t(time);
     28 
     29     std::stringstream timeStream;
     30     struct tm gmTime;
     31 #ifdef _WIN32
     32     gmtime_s(&gmTime, &timer); // gmtime_s is the safe version of gmtime, not available on Linux
     33 #else
     34     gmTime = (*std::gmtime(&timer));
     35 #endif
     36     timeStream << std::put_time(&gmTime, "%F %X"); // yyyy-mm-dd HH:MM:SS
     37     timeStream << '.' << std::setfill('0') << std::setw(3) << ms.count();
     38 
     39     return timeStream.str();
     40 }
     41 } // namespace
     42 
     43 void SFS::test::LogCallbackToTest(const SFS::LogData& logData)
     44 {
     45     std::lock_guard guard(s_logMutex);
     46     UNSCOPED_INFO("Log: " << TimestampToString(logData.time) << " [" << ToString(logData.severity) << "]"
     47                           << " " << std::filesystem::path(logData.file).filename().string() << ":" << logData.line
     48                           << " " << logData.message);
     49 }