Skip to content

Commit 9c0c684

Browse files
committed
feat: add toast notifications
* bumped version to 1.0.3 * separated some concerns into their own classes * added WinToast for toast notifications * users are now asked on first startup of the program to run on startup, to avoid an additional task
1 parent 60af153 commit 9c0c684

15 files changed

+2720
-577
lines changed

ThreeFingerDrag/Logger.h

Lines changed: 66 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,80 @@
11
#pragma once
22

3-
43
#include <fstream>
54
#include <chrono>
6-
#include <ctime>
7-
#include <iomanip>
85
#include <string>
9-
#include <sstream>
106
#include <filesystem>
117

8+
/**
9+
* @brief A logger class for writing log messages to a file.
10+
*/
1211
class Logger
1312
{
1413
public:
15-
Logger(const Logger&) = delete;
16-
Logger& operator=(const Logger&) = delete;
17-
Logger(Logger&&) = default;
18-
Logger& operator=(Logger&&) = default;
19-
20-
/**
21-
* \brief Constructs the logger and opens the file for writing log lines to.
22-
* \param localAppDataFolder The application folder name.
23-
* \remarks If the folder or file do not exist, they will be created.
24-
*/
25-
Logger(const std::string& localAppDataFolder)
26-
{
27-
size_t len;
28-
char* env_path;
29-
const errno_t err = _dupenv_s(&env_path, &len, "LOCALAPPDATA");
30-
31-
// If the environment variable exists and can be retrieved successfully, use it to construct the path to the log file.
32-
if (err == 0 && env_path != nullptr)
33-
{
34-
log_file_path_ = std::string(env_path);
35-
log_file_path_ += "\\";
36-
log_file_path_ += localAppDataFolder;
37-
38-
// Check if the folder exists, and create it if necessary
39-
if (!std::filesystem::exists(log_file_path_))
40-
std::filesystem::create_directory(log_file_path_);
41-
42-
log_file_path_ += "\\log.txt";
43-
44-
// Check if the log file exists, and create it if necessary
45-
if (!std::filesystem::exists(log_file_path_))
46-
{
47-
std::ofstream file(log_file_path_, std::ios::out);
48-
file.close();
49-
}
50-
51-
log_file_.open(log_file_path_, std::ios::out | std::ios::app);
52-
free(env_path);
53-
}
54-
}
55-
56-
void Info(const std::string& message)
57-
{
58-
WriteLog("[INFO]", message);
59-
}
6014

61-
void Debug(const std::string& message)
62-
{
63-
WriteLog("[DEBUG]", message);
64-
}
65-
66-
void Warning(const std::string& message)
67-
{
68-
Info("[WARNING], message");
69-
}
70-
71-
void Error(const std::string& message)
72-
{
73-
WriteLog("[ERROR]", message);
74-
}
75-
76-
~Logger()
77-
{
78-
log_file_.close();
79-
}
15+
/**
16+
* @brief Writes an info log message to the file.
17+
* @param message The log message.
18+
*/
19+
void Info(const std::string& message);
20+
21+
/**
22+
* @brief Writes a debug log message to the file.
23+
* @param message The log message.
24+
*/
25+
void Debug(const std::string& message);
26+
27+
/**
28+
* @brief Writes a warning log message to the file.
29+
* @param message The log message.
30+
*/
31+
void Warning(const std::string& message);
32+
33+
/**
34+
* @brief Writes an error log message to the file.
35+
* @param message The log message.
36+
*/
37+
void Error(const std::string& message);
38+
39+
/**
40+
* @brief Destructor that closes the log file.
41+
*/
42+
~Logger();
43+
44+
Logger(const Logger&) = delete;
45+
Logger& operator=(const Logger&) = delete;
46+
Logger(Logger&&) noexcept = default;
47+
Logger& operator=(Logger&&) noexcept = default;
48+
49+
/**
50+
* @brief Returns the singleton instance of the logger.
51+
* @param localAppDataFolderName The application folder name.
52+
* @param logFileName The name of the log file.
53+
* @return The logger instance.
54+
*/
55+
static Logger& GetInstance();
8056

8157
private:
82-
std::ofstream log_file_;
83-
std::string log_file_path_;
84-
85-
void WriteLog(const std::string& type, const std::string& message)
86-
{
87-
// Check if the log file has exceeded the threshold size of 5 MB
88-
const std::streampos current_pos = log_file_.tellp();
89-
const std::streampos max_size = 5 * 1024 * 1024; // 5MB
90-
if (current_pos >= max_size)
91-
{
92-
// Truncate the log file to zero size
93-
log_file_.close();
94-
std::ofstream file(log_file_path_, std::ios::out | std::ios::trunc);
95-
file.close();
96-
}
97-
98-
// Get the current system time
99-
const auto now = std::chrono::system_clock::now();
100-
const std::time_t now_time = std::chrono::system_clock::to_time_t(now);
101-
102-
// Convert the time to a string
103-
std::tm time_info;
104-
if (localtime_s(&time_info, &now_time) == 0)
105-
{
106-
std::stringstream ss;
107-
ss << std::put_time(&time_info, "%y-%m-%d %H:%M:%S");
108-
const std::string timestamp_str = ss.str();
109-
110-
// Write the log message to the file with the timestamp
111-
log_file_ << timestamp_str << " " << type << " " << message << std::endl;
112-
}
113-
else
114-
{
115-
// Write the log message to the file without the timestamp
116-
log_file_ << type << " - " << message << std::endl;
117-
}
118-
}
58+
/**
59+
* @brief Constructs the logger and opens the file for writing log lines to.
60+
* @param localAppDataFolderName The application folder name.
61+
*
62+
* If the folder or file do not exist, they will be created.
63+
*/
64+
Logger(const std::string& localAppDataFolderName, const std::string& logFileName);
65+
66+
std::ofstream log_file_; ///< The log file stream.
67+
std::string log_file_path_; ///< The path to the log file.
68+
69+
/**
70+
* @brief Writes a log message to the file with the specified log type.
71+
* @param type The log type, such as [INFO], [DEBUG], [WARNING], or [ERROR].
72+
* @param message The log message.
73+
*/
74+
void WriteLog(const std::string& type, const std::string& message);
11975
};
76+
77+
#define INFO(msg) Logger::GetInstance().Info(msg)
78+
#define DEBUG(msg) Logger::GetInstance().Debug(msg)
79+
#define WARNING(msg) Logger::GetInstance().Warning(msg)
80+
#define ERROR(msg) Logger::GetInstance().Error(msg)

0 commit comments

Comments
 (0)