C++ Pixels Library For Windows
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:
28 static void log(const std::string& message)
29 {
30 static std::ofstream myfile(std::filesystem::temp_directory_path().append("systemic_log.txt").native());
31
32 std::ostringstream ss;
33 time_in_HH_MM_SS_MMM(ss);
34 ss << "[" << std::this_thread::get_id() << "]" << ": " << message << "\n";
35 myfile << ss.str();
36 myfile.flush();
37 }
38
39 private:
40 static void time_in_HH_MM_SS_MMM(std::ostream& out)
41 {
42 using namespace std::chrono;
43
44 // get current time
45 auto now = system_clock::now();
46
47 // get number of milliseconds for the current second
48 // (remainder after division into seconds)
49 auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
50
51 // convert to std::time_t in order to convert to std::tm (broken time)
52 auto timer = system_clock::to_time_t(now);
53
54 // convert to broken time
55 std::tm bt;
56 ::localtime_s(&bt, &timer);
57
58 // save default formatting
59 std::ios init(nullptr);
60 init.copyfmt(out);
61
62 out << std::put_time(&bt, "%H:%M:%S"); // HH:MM:SS
63 out << '.' << std::setfill('0') << std::setw(3) << ms.count() << std::setw(3);
64
65 // restore default formatting
66 out.copyfmt(init);
67 }
68 };
69}