Clean up comment formatting in LogManager and StringUtils cpp files

This commit is contained in:
Adam Honse 2025-07-06 14:39:37 -05:00
parent c0c772b11b
commit 37cd743315
2 changed files with 157 additions and 122 deletions

View file

@ -20,13 +20,17 @@
const char* LogManager::log_codes[] = {"FATAL:", "ERROR:", "Warning:", "Info:", "Verbose:", "Debug:", "Trace:", "Dialog:"}; const char* LogManager::log_codes[] = {"FATAL:", "ERROR:", "Warning:", "Info:", "Verbose:", "Debug:", "Trace:", "Dialog:"};
const char* TimestampPattern = "%04d%02d%02d_%02d%02d%02d"; const char* TimestampPattern = "%04d%02d%02d_%02d%02d%02d";
const char* TimestampRegex = "[0-9]{8}_[0-9]{6}"; // relies on the structure of the template above
/*---------------------------------------------------------*\
| Relies on the structure of the template above |
\*---------------------------------------------------------*/
const char* TimestampRegex = "[0-9]{8}_[0-9]{6}";
LogManager::LogManager() LogManager::LogManager()
{ {
base_clock = std::chrono::steady_clock::now(); base_clock = std::chrono::steady_clock::now();
log_console_enabled = false; log_console_enabled = false;
log_file_enabled = true; log_file_enabled = true;
} }
LogManager* LogManager::get() LogManager* LogManager::get()
@ -35,9 +39,9 @@ LogManager* LogManager::get()
static std::mutex instance_mutex; static std::mutex instance_mutex;
std::lock_guard<std::mutex> grd(instance_mutex); std::lock_guard<std::mutex> grd(instance_mutex);
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Create a new instance if one does not exist | | Create a new instance if one does not exist |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(!_instance) if(!_instance)
{ {
_instance = new LogManager(); _instance = new LogManager();
@ -62,20 +66,20 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
{ {
std::lock_guard<std::recursive_mutex> grd(entry_mutex); std::lock_guard<std::recursive_mutex> grd(entry_mutex);
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| If the log is not open, create a new log file | | If the log is not open, create a new log file |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(!log_stream.is_open()) if(!log_stream.is_open())
{ {
/*----------------------------------------------------*\ /*-------------------------------------------------*\
| If a limit is declared in the config for the maximum | | If a limit is declared in the config for the |
| number of log files, respect the limit | | maximum number of log files, respect the limit |
| Log rotation will remove the files matching the | | Log rotation will remove the files matching the |
| current "logfile", starting with the oldest ones | | current "logfile", starting with the oldest ones |
| (according to the timestamp in their filename) | | (according to the timestamp in their filename) |
| i.e. with the lexicographically smallest filename | | i.e. with the lexicographically smallest filename |
| 0 or less equals no limit (default) | | 0 or less equals no limit (default) |
\*----------------------------------------------------*/ \*-------------------------------------------------*/
int loglimit = 0; int loglimit = 0;
if(config.contains("file_count_limit") && config["file_count_limit"].is_number_integer()) if(config.contains("file_count_limit") && config["file_count_limit"].is_number_integer())
{ {
@ -87,19 +91,18 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
log_file_enabled = config["log_file"]; log_file_enabled = config["log_file"];
} }
/*-----------------------------------------*\ /*-------------------------------------------------*\
| Default template for the logfile name | | Default template for the logfile name |
| The # symbol is replaced with a timestamp | | The # symbol is replaced with a timestamp |
\*-----------------------------------------*/ \*-------------------------------------------------*/
std::string logtempl = "OpenRGB_#.log"; std::string logtempl = "OpenRGB_#.log";
if(log_file_enabled) if(log_file_enabled)
{ {
/*---------------------------------------------*\
/*-------------------------------------------------*\ | If the logfile is defined in the |
| If the logfile is defined in the configuration, | | configuration, use the configured name |
| use the configured name | \*---------------------------------------------*/
\*-------------------------------------------------*/
if(config.contains("logfile")) if(config.contains("logfile"))
{ {
const json& logfile_obj = config["logfile"]; const json& logfile_obj = config["logfile"];
@ -112,10 +115,10 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
} }
} }
} }
/*-------------------------------------------------*\ /*---------------------------------------------*\
| If the # symbol is found in the log file name, | | If the # symbol is found in the log file |
| replace it with a timestamp | | name, replace it with a timestamp |
\*-------------------------------------------------*/ \*---------------------------------------------*/
time_t t = time(0); time_t t = time(0);
struct tm* tmp = localtime(&t); struct tm* tmp = localtime(&t);
char time_string[64]; char time_string[64];
@ -128,9 +131,9 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
logname.replace(oct, 1, time_string); logname.replace(oct, 1, time_string);
} }
/*-------------------------------------------------*\ /*---------------------------------------------*\
| If the path is relative, use logs dir | | If the path is relative, use logs dir |
\*-------------------------------------------------*/ \*---------------------------------------------*/
filesystem::path p = filesystem::u8path(logname); filesystem::path p = filesystem::u8path(logname);
if(p.is_relative()) if(p.is_relative())
{ {
@ -138,20 +141,20 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
} }
filesystem::create_directories(p.parent_path()); filesystem::create_directories(p.parent_path());
/*----------------------------------------------*\ /*---------------------------------------------*\
| "Log rotation": remove old log files exceeding | | "Log rotation": remove old log files |
| the current configured limit | | exceeding the current configured limit |
\*----------------------------------------------*/ \*---------------------------------------------*/
rotate_logs(p.parent_path(), filesystem::u8path(logtempl).filename(), loglimit); rotate_logs(p.parent_path(), filesystem::u8path(logtempl).filename(), loglimit);
/*-------------------------------------------------*\ /*---------------------------------------------*\
| Open the logfile | | Open the logfile |
\*-------------------------------------------------*/ \*---------------------------------------------*/
log_stream.open(p); log_stream.open(p);
/*-------------------------------------------------*\ /*---------------------------------------------*\
| Print Git Commit info, version, etc. | | Print Git Commit info, version, etc. |
\*-------------------------------------------------*/ \*---------------------------------------------*/
log_stream << " OpenRGB v" << VERSION_STRING << std::endl; log_stream << " OpenRGB v" << VERSION_STRING << std::endl;
log_stream << " Commit: " << GIT_COMMIT_ID << " from " << GIT_COMMIT_DATE << std::endl; log_stream << " Commit: " << GIT_COMMIT_ID << " from " << GIT_COMMIT_DATE << std::endl;
log_stream << " Launched: " << time_string << std::endl; log_stream << " Launched: " << time_string << std::endl;
@ -160,9 +163,9 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
} }
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Check loglevel configuration | | Check loglevel configuration |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(config.contains("loglevel")) if(config.contains("loglevel"))
{ {
const json& loglevel_obj = config["loglevel"]; const json& loglevel_obj = config["loglevel"];
@ -176,32 +179,34 @@ void LogManager::configure(json config, const filesystem::path& defaultDir)
} }
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Check log console configuration | | Check log console configuration |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(config.contains("log_console")) if(config.contains("log_console"))
{ {
log_console_enabled = config["log_console"]; log_console_enabled = config["log_console"];
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Flush the log | | Flush the log |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
_flush(); _flush();
} }
void LogManager::_flush() void LogManager::_flush()
{ {
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| If the log is open, write out buffered messages | | If the log is open, write out buffered messages |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(log_stream.is_open()) if(log_stream.is_open())
{ {
for(size_t msg = 0; msg < temp_messages.size(); ++msg) for(size_t msg = 0; msg < temp_messages.size(); ++msg)
{ {
if(temp_messages[msg]->level <= loglevel || temp_messages[msg]->level == LL_DIALOG) if(temp_messages[msg]->level <= loglevel || temp_messages[msg]->level == LL_DIALOG)
{ {
// Put the timestamp here /*-----------------------------------------*\
| Put the timestamp here |
\*-----------------------------------------*/
std::chrono::milliseconds counter = std::chrono::duration_cast<std::chrono::milliseconds>(temp_messages[msg]->counted_second); std::chrono::milliseconds counter = std::chrono::duration_cast<std::chrono::milliseconds>(temp_messages[msg]->counted_second);
log_stream << std::left << std::setw(6) << counter.count() << "|"; log_stream << std::left << std::setw(6) << counter.count() << "|";
log_stream << std::left << std::setw(9) << log_codes[temp_messages[msg]->level]; log_stream << std::left << std::setw(9) << log_codes[temp_messages[msg]->level];
@ -236,10 +241,10 @@ void LogManager::flush()
void LogManager::_append(const char* filename, int line, unsigned int level, const char* fmt, va_list va) void LogManager::_append(const char* filename, int line, unsigned int level, const char* fmt, va_list va)
{ {
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| If a critical message occurs, enable source | | If a critical message occurs, enable source |
| printing and set loglevel and verbosity to highest| | printing and set loglevel and verbosity to highest |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(level == LL_FATAL) if(level == LL_FATAL)
{ {
print_source = true; print_source = true;
@ -247,14 +252,14 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con
verbosity = LL_DEBUG; verbosity = LL_DEBUG;
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Create a new message | | Create a new message |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
PLogMessage mes = std::make_shared<LogMessage>(); PLogMessage mes = std::make_shared<LogMessage>();
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Resize the buffer, then fill in the message text | | Resize the buffer, then fill in the message text |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
va_list va2; va_list va2;
va_copy(va2, va); va_copy(va2, va);
int len = vsnprintf(nullptr, 0, fmt, va); int len = vsnprintf(nullptr, 0, fmt, va);
@ -262,18 +267,18 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con
vsnprintf(&(mes->buffer[0]), len + 1, fmt, va2); vsnprintf(&(mes->buffer[0]), len + 1, fmt, va2);
va_end(va2); va_end(va2);
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Fill in message information | | Fill in message information |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
mes->level = level; mes->level = level;
mes->filename = filename; mes->filename = filename;
mes->line = line; mes->line = line;
mes->counted_second = std::chrono::steady_clock::now() - base_clock; mes->counted_second = std::chrono::steady_clock::now() - base_clock;
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| If this is a dialog message, call the dialog show | | If this is a dialog message, call the dialog show |
| callback | | callback |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(level == LL_DIALOG) if(level == LL_DIALOG)
{ {
for(size_t idx = 0; idx < dialog_show_callbacks.size(); idx++) for(size_t idx = 0; idx < dialog_show_callbacks.size(); idx++)
@ -282,11 +287,11 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con
} }
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| If the message is within the current verbosity, | | If the message is within the current verbosity, print |
| print it on the screen | | it on the screen |
| TODO: Put the timestamp here | | TODO: Put the timestamp here |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(level <= verbosity || level == LL_DIALOG) if(level <= verbosity || level == LL_DIALOG)
{ {
std::cout << mes->buffer; std::cout << mes->buffer;
@ -297,9 +302,9 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con
std::cout << std::endl; std::cout << std::endl;
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Add the message to the logfile queue | | Add the message to the logfile queue |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
temp_messages.push_back(mes); temp_messages.push_back(mes);
if(log_console_enabled) if(log_console_enabled)
@ -307,9 +312,9 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con
all_messages.push_back(mes); all_messages.push_back(mes);
} }
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Flush the queues | | Flush the queues |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
_flush(); _flush();
} }
@ -336,10 +341,10 @@ void LogManager::append(const char* filename, int line, unsigned int level, cons
void LogManager::setLoglevel(unsigned int level) void LogManager::setLoglevel(unsigned int level)
{ {
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Check that the new log level is valid, otherwise | | Check that the new log level is valid, otherwise set |
| set it within the valid range | | it within the valid range |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(level > LL_TRACE) if(level > LL_TRACE)
{ {
level = LL_TRACE; level = LL_TRACE;
@ -347,19 +352,18 @@ void LogManager::setLoglevel(unsigned int level)
LOG_DEBUG("[LogManager] Loglevel set to %d", level); LOG_DEBUG("[LogManager] Loglevel set to %d", level);
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Set the new log level | | Set the new log level |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
loglevel = level; loglevel = level;
} }
void LogManager::setVerbosity(unsigned int level) void LogManager::setVerbosity(unsigned int level)
{ {
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Check that the new verbosity is valid, otherwise | | Check that the new verbosity is valid, otherwise set |
| set it within the valid range | | it within the valid range |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
if(level > LL_TRACE) if(level > LL_TRACE)
{ {
level = LL_TRACE; level = LL_TRACE;
@ -367,9 +371,9 @@ void LogManager::setVerbosity(unsigned int level)
LOG_DEBUG("[LogManager] Verbosity set to %d", level); LOG_DEBUG("[LogManager] Verbosity set to %d", level);
/*-------------------------------------------------*\ /*-----------------------------------------------------*\
| Set the new verbosity | | Set the new verbosity |
\*-------------------------------------------------*/ \*-----------------------------------------------------*/
verbosity = level; verbosity = level;
} }
@ -407,15 +411,21 @@ void LogManager::rotate_logs(const filesystem::path& folder, const filesystem::p
std::string templ2 = templ.filename().generic_u8string(); std::string templ2 = templ.filename().generic_u8string();
// Process the templ2 into a usable regex /*-----------------------------------------------------*\
// The # symbol is replaced with a timestamp regex | Process the templ2 into a usable regex |
// Any regex-unfriendly symbols are escaped with a backslash | The # symbol is replaced with a timestamp regex |
| Any regex-unfriendly symbols are escaped with a |
| backslash |
\*-----------------------------------------------------*/
std::string regex_templ = "^"; std::string regex_templ = "^";
for(size_t i = 0; i < templ2.size(); ++i) for(size_t i = 0; i < templ2.size(); ++i)
{ {
switch(templ2[i]) switch(templ2[i])
{ {
// Symbols that have special meanings in regex'es need backslash escaping /*-------------------------------------------------*\
| Symbols that have special meanings in regex'es |
| need backslash escaping |
\*-------------------------------------------------*/
case '.': case '.':
case '^': case '^':
case '$': case '$':
@ -428,12 +438,20 @@ void LogManager::rotate_logs(const filesystem::path& folder, const filesystem::p
case ']': case ']':
case '*': case '*':
case '-': case '-':
case '\\': // Should have been filtered out by the filesystem processing, but... who knows /*-------------------------------------------------*\
| Should have been filtered out by the filesystem |
| processing, but... who knows |
\*-------------------------------------------------*/
case '\\':
regex_templ.push_back('\\'); regex_templ.push_back('\\');
regex_templ.push_back(templ2[i]); regex_templ.push_back(templ2[i]);
break; break;
// The # symbol is reserved for the timestamp and thus is replaced with the timestamp regex template /*-------------------------------------------------*\
| The # symbol is reserved for the timestamp and |
| thus is replaced with the timestamp regex |
| template |
\*-------------------------------------------------*/
case '#': case '#':
regex_templ.append(TimestampRegex); regex_templ.append(TimestampRegex);
break; break;
@ -462,15 +480,24 @@ void LogManager::rotate_logs(const filesystem::path& folder, const filesystem::p
} }
std::sort(valid_paths.begin(), valid_paths.end()); std::sort(valid_paths.begin(), valid_paths.end());
size_t remove_count = valid_paths.size() - max_count + 1; // NOTE: the "1" extra file to remove creates space for the one we're about to create /*-----------------------------------------------------*\
if(remove_count > valid_paths.size()) // for max_count <= 0 and to prevent any possible errors in the above logic | NOTE: the "1" extra file to remove creates space for |
| the one we're about to create for max_count <= 0 and |
| to prevent any possible errors in the above logic |
\*-----------------------------------------------------*/
size_t remove_count = valid_paths.size() - max_count + 1;
if(remove_count > valid_paths.size())
{ {
remove_count = valid_paths.size(); remove_count = valid_paths.size();
} }
for(size_t i = 0; i < remove_count; ++i) for(size_t i = 0; i < remove_count; ++i)
{ {
std::error_code ec; // Uses error code to force the `remove` call to be `noexcept` /*-------------------------------------------------*\
| Uses error code to force the `remove` call to be |
| `noexcept` |
\*-------------------------------------------------*/
std::error_code ec;
if(filesystem::remove(valid_paths[i], ec)) if(filesystem::remove(valid_paths[i], ec))
{ {
LOG_VERBOSE("[LogManager] Removed log file [%s] during rotation", valid_paths[i].u8string().c_str()); LOG_VERBOSE("[LogManager] Removed log file [%s] during rotation", valid_paths[i].u8string().c_str());

View file

@ -14,15 +14,18 @@
const char* StringUtils::wchar_to_char(const wchar_t* pwchar) const char* StringUtils::wchar_to_char(const wchar_t* pwchar)
{ {
if (pwchar == nullptr) if(pwchar == nullptr)
{ {
return ""; return "";
} }
// get the number of characters in the string.
/*-----------------------------------------------------*\
| Get the number of characters in the string. |
\*-----------------------------------------------------*/
int currentCharIndex = 0; int currentCharIndex = 0;
char currentChar = (char)pwchar[currentCharIndex]; char currentChar = (char)pwchar[currentCharIndex];
while (currentChar != '\0') while(currentChar != '\0')
{ {
currentCharIndex++; currentCharIndex++;
currentChar = (char)pwchar[currentCharIndex]; currentChar = (char)pwchar[currentCharIndex];
@ -30,12 +33,17 @@ const char* StringUtils::wchar_to_char(const wchar_t* pwchar)
const int charCount = currentCharIndex + 1; const int charCount = currentCharIndex + 1;
// allocate a new block of memory size char (1 byte) instead of wide char (2 bytes) /*-----------------------------------------------------*\
| Allocate a new block of memory size char (1 byte) |
| instead of wide char (2 bytes) |
\*-----------------------------------------------------*/
char* filePathC = (char*)malloc(sizeof(char) * charCount); char* filePathC = (char*)malloc(sizeof(char) * charCount);
for (int i = 0; i < charCount; i++) for(int i = 0; i < charCount; i++)
{ {
// convert to char (1 byte) /*-------------------------------------------------*\
| Convert to char (1 byte) |
\*-------------------------------------------------*/
char character = (char)pwchar[i]; char character = (char)pwchar[i];
*filePathC = character; *filePathC = character;
@ -43,11 +51,12 @@ const char* StringUtils::wchar_to_char(const wchar_t* pwchar)
filePathC += sizeof(char); filePathC += sizeof(char);
} }
filePathC += '\0'; filePathC += '\0';
filePathC -= (sizeof(char) * charCount); filePathC -= (sizeof(char) * charCount);
return filePathC; return(filePathC);
} }
std::string StringUtils::wstring_to_string(const std::wstring wstring) std::string StringUtils::wstring_to_string(const std::wstring wstring)
@ -66,11 +75,10 @@ std::string StringUtils::u16string_to_string(const std::u16string wstring)
const std::string StringUtils::remove_null_terminating_chars(std::string input) const std::string StringUtils::remove_null_terminating_chars(std::string input)
{ {
while (!input.empty() && input.back() == 0) while(!input.empty() && input.back() == 0)
{ {
input.pop_back(); input.pop_back();
} }
return input; return(input);
} }