Pixels Plugin for Unity
Enable communications with Pixels dice using Bluetooth Low Energy.
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <fstream>
9#include <sstream>
10#include <thread>
11#include <ctime>
12#include <chrono>
13#include <iomanip>
14#include <filesystem>
15
16namespace Systemic::Internal
17{
21 class Logger
22 {
23 public:
24 static void Log(const std::string& str)
25 {
26 static std::ofstream myfile(std::filesystem::temp_directory_path().append("systemic_log.txt").native());
27
28 std::ostringstream ss;
29 time_in_HH_MM_SS_MMM(ss);
30 ss << "[" << std::this_thread::get_id() << "]" << ": " << str << "\n";
31 myfile << ss.str();
32 myfile.flush();
33 }
34
35 private:
36 static void time_in_HH_MM_SS_MMM(std::ostream& out)
37 {
38 using namespace std::chrono;
39
40 // get current time
41 auto now = system_clock::now();
42
43 // get number of milliseconds for the current second
44 // (remainder after division into seconds)
45 auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
46
47 // convert to std::time_t in order to convert to std::tm (broken time)
48 auto timer = system_clock::to_time_t(now);
49
50 // convert to broken time
51 std::tm bt;
52 ::localtime_s(&bt, &timer);
53
54 // save default formatting
55 std::ios init(nullptr);
56 init.copyfmt(out);
57
58 out << std::put_time(&bt, "%H:%M:%S"); // HH:MM:SS
59 out << '.' << std::setfill('0') << std::setw(3) << ms.count() << std::setw(3);
60
61 // restore default formatting
62 out.copyfmt(init);
63 }
64 };
65}