summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-05-09 10:00:10 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-07-14 17:51:42 +0200
commitf3eb1efc933eec853430967807822ab8523c02b5 (patch)
tree5d23834e467533c804609298019de37978164bfc
parent5e36b827321aab0e89458f08531d622a85ab58f3 (diff)
downloadmana-f3eb1efc933eec853430967807822ab8523c02b5.tar.gz
mana-f3eb1efc933eec853430967807822ab8523c02b5.tar.bz2
mana-f3eb1efc933eec853430967807822ab8523c02b5.tar.xz
mana-f3eb1efc933eec853430967807822ab8523c02b5.zip
Reduced scope of static ChatLogger functions
No need for them to be part of the header.
-rw-r--r--src/chatlogger.cpp115
-rw-r--r--src/chatlogger.h9
2 files changed, 57 insertions, 67 deletions
diff --git a/src/chatlogger.cpp b/src/chatlogger.cpp
index 119d3fc1..854f9780 100644
--- a/src/chatlogger.cpp
+++ b/src/chatlogger.cpp
@@ -38,6 +38,51 @@
#include "utils/stringutils.h"
+static std::string getDateString()
+{
+ time_t t = time(nullptr);
+ struct tm *now = localtime(&t);
+ char buffer[11];
+ strftime(buffer, sizeof(buffer), "%Y-%m-%d", now);
+ return buffer;
+}
+
+static std::string &secureName(std::string &name)
+{
+ const size_t sz = name.length();
+ for (size_t f = 0; f < sz; f ++)
+ {
+ const unsigned char ch = name[f];
+ if ((ch < '0' || ch > '9') &&
+ (ch < 'a' || ch > 'z') &&
+ (ch < 'A' || ch > 'Z') &&
+ ch != '-' &&
+ ch != '+' &&
+ ch != '=' &&
+ ch != '.' &&
+ ch != ',' &&
+ ch != ')' &&
+ ch != '(' &&
+ ch != '[' &&
+ ch != ']' &&
+ ch != '#')
+ {
+ name[f] = '_';
+ }
+ }
+ return name;
+}
+
+static void makeDir(const std::string &dir)
+{
+#ifdef _WIN32
+ mkdir(dir.c_str());
+#else
+ mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP);
+#endif
+}
+
+
ChatLogger::~ChatLogger() = default;
void ChatLogger::setLogFile(const std::string &logFilename)
@@ -74,69 +119,32 @@ void ChatLogger::log(std::string str)
if (!mLogFile.is_open() || dateStr != mLogDate)
{
mLogDate = dateStr;
- setLogFile(strprintf("%s/%s/#General_%s.log", mLogDir.c_str(),
- mServerName.c_str(), dateStr.c_str()));
+ setLogFile(strprintf("%s/%s/#General_%s.log",
+ mLogDir.c_str(),
+ mServerName.c_str(),
+ dateStr.c_str()));
}
removeColors(str);
- writeTo(mLogFile, str);
+ mLogFile << str << std::endl;
}
void ChatLogger::log(std::string name, std::string str)
{
std::ofstream logFile;
- logFile.open(strprintf("%s/%s/%s_%s.log", mLogDir.c_str(), mServerName.c_str(),
- secureName(name).c_str(), getDateString().c_str()).c_str(),
+ logFile.open(strprintf("%s/%s/%s_%s.log",
+ mLogDir.c_str(),
+ mServerName.c_str(),
+ secureName(name).c_str(),
+ getDateString().c_str())
+ .c_str(),
std::ios_base::app);
if (!logFile.is_open())
return;
removeColors(str);
- writeTo(logFile, str);
-
- if (logFile.is_open())
- logFile.close();
-}
-
-std::string ChatLogger::getDateString()
-{
- time_t t = time(nullptr);
- struct tm *now = localtime(&t);
- char buffer[11];
- strftime(buffer, sizeof(buffer), "%Y-%m-%d", now);
- return buffer;
-}
-
-std::string ChatLogger::secureName(std::string &name)
-{
- const size_t sz = name.length();
- for (size_t f = 0; f < sz; f ++)
- {
- const unsigned char ch = name[f];
- if ((ch < '0' || ch > '9') &&
- (ch < 'a' || ch > 'z') &&
- (ch < 'A' || ch > 'Z') &&
- ch != '-' &&
- ch != '+' &&
- ch != '=' &&
- ch != '.' &&
- ch != ',' &&
- ch != ')' &&
- ch != '(' &&
- ch != '[' &&
- ch != ']' &&
- ch != '#')
- {
- name[f] = '_';
- }
- }
- return name;
-}
-
-void ChatLogger::writeTo(std::ofstream &file, const std::string &str)
-{
- file << str << std::endl;
+ logFile << str << std::endl;
}
void ChatLogger::setServerName(const std::string &serverName)
@@ -158,12 +166,3 @@ void ChatLogger::setServerName(const std::string &serverName)
closedir(dir);
}
}
-
-void ChatLogger::makeDir(const std::string &dir)
-{
-#ifdef _WIN32
- mkdir(dir.c_str());
-#else
- mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP);
-#endif
-}
diff --git a/src/chatlogger.h b/src/chatlogger.h
index 59d9f28c..8e4118dc 100644
--- a/src/chatlogger.h
+++ b/src/chatlogger.h
@@ -35,13 +35,8 @@ class ChatLogger
* Enters a message in the log. The message will be timestamped.
*/
void log(std::string str);
-
void log(std::string name, std::string str);
- static std::string getDateString();
-
- static std::string secureName(std::string &str);
-
void setServerName(const std::string &serverName);
private:
@@ -50,10 +45,6 @@ class ChatLogger
*/
void setLogFile(const std::string &logFilename);
- static void writeTo(std::ofstream &file, const std::string &str);
-
- static void makeDir(const std::string &dir);
-
std::ofstream mLogFile;
std::string mLogDir;
std::string mServerName;