summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/resourcemanager.cpp52
-rw-r--r--src/common/resourcemanager.hpp16
-rw-r--r--src/utils/logger.cpp46
3 files changed, 89 insertions, 25 deletions
diff --git a/src/common/resourcemanager.cpp b/src/common/resourcemanager.cpp
index 5d2a8cf4..9c52a8e7 100644
--- a/src/common/resourcemanager.cpp
+++ b/src/common/resourcemanager.cpp
@@ -24,6 +24,7 @@
#include "utils/logger.h"
+#include <sys/stat.h>
#include <cstdlib>
#include <cstring>
@@ -53,8 +54,42 @@ void ResourceManager::initialize()
PHYSFS_addToSearchPath(serverDataPath.c_str(), 1);
}
-bool ResourceManager::exists(const std::string &path)
+/**
+ * This function tries to check if a file exists
+ * based on the existence of stats about it.
+ * Because simply trying to open it and check for failure
+ * is a bad thing, as you don't know if you weren't able
+ * to open it because it doesn't exist or because
+ * you don't have the right to.
+ */
+static bool fileExists(const std::string& filename)
{
+ struct stat fileStat;
+ bool fileExist = false;
+ int statValue = -1;
+
+ // Try to get the file attributes
+ statValue = stat(filename.c_str(), &fileStat);
+
+ if(statValue == 0)
+ {
+ // The file attributes exists, so the file does, too.
+ fileExist = true;
+ }
+ else
+ {
+ // Impossible to get the file attributes.
+ fileExist = false;
+ }
+
+ return fileExist;
+}
+
+bool ResourceManager::exists(const std::string &path, bool lookInSearchPath)
+{
+ if (!lookInSearchPath)
+ return fileExists(path);
+
return PHYSFS_exists(path.c_str());
}
@@ -100,3 +135,18 @@ char *ResourceManager::loadFile(const std::string &fileName, int &fileSize)
buffer[fileSize] = 0;
return buffer;
}
+
+ResourceManager::splittedPath ResourceManager::splitFileNameAndPath(
+ const std::string &fullFilePath)
+{
+ // We'll reversed-search for '/' or'\' and extract the substrings
+ // corresponding to the filename and the path separately.
+ size_t slashPos = fullFilePath.find_last_of("/\\");
+
+ ResourceManager::splittedPath splittedFilePath;
+ // Note the last slash is kept in the path name.
+ splittedFilePath.path = fullFilePath.substr(0, slashPos + 1);
+ splittedFilePath.file = fullFilePath.substr(slashPos + 1);
+
+ return splittedFilePath;
+}
diff --git a/src/common/resourcemanager.hpp b/src/common/resourcemanager.hpp
index ffbefd10..3d722a46 100644
--- a/src/common/resourcemanager.hpp
+++ b/src/common/resourcemanager.hpp
@@ -25,6 +25,13 @@
namespace ResourceManager
{
+ // A structure retaining the path and file names separately.
+ struct splittedPath
+ {
+ std::string path;
+ std::string file;
+ };
+
/**
* Searches for zip files and adds them to PhysFS search path.
*/
@@ -33,7 +40,7 @@ namespace ResourceManager
/**
* Checks whether the given file or directory exists in the search path
*/
- bool exists(const std::string &path);
+ bool exists(const std::string &path, bool lookInSearchPath = true);
/**
* Returns the real file-system path of the resource with the given
@@ -53,6 +60,13 @@ namespace ResourceManager
* @note The array contains an extra \0 character at position fileSize.
*/
char *loadFile(const std::string &fileName, int &fileSize);
+
+ /**
+ * Returns the filePath sub-part corresponding to the filename only.
+ * @return splittedPath: the file path ending with '/' or '\'
+ * and the file name alone.
+ */
+ splittedPath splitFileNameAndPath(const std::string &fullFilePath);
}
#endif
diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp
index 45a68eb5..6ad008c9 100644
--- a/src/utils/logger.cpp
+++ b/src/utils/logger.cpp
@@ -70,12 +70,12 @@ static std::string getCurrentTime()
// constituents.
local = *(localtime(&now));
- // Stringify the time, the format is: [hh-mm-ss]
+ // Stringify the time, the format is: hh:mm:ss
using namespace std;
ostringstream os;
os << setw(2) << setfill('0') << local.tm_hour
- << "-" << setw(2) << setfill('0') << local.tm_min
- << "-" << setw(2) << setfill('0') << local.tm_sec;
+ << ":" << setw(2) << setfill('0') << local.tm_min
+ << ":" << setw(2) << setfill('0') << local.tm_sec;
return os.str();
}
@@ -112,10 +112,10 @@ static std::string getCurrentDate()
*
* @return whether the day has changed.
*/
-static bool getDayChanged()
+bool getDayChanged()
{
static std::string date = getCurrentDate();
- if (mLastCallDate.compare(date))
+ if (mLastCallDate != date)
{
// Reset the current date for next call.
mLastCallDate = date;
@@ -209,49 +209,49 @@ void Logger::switchLogs()
return;
// Update current filesize
- long mFileSize = mLogFile.tellp();
+ long fileSize = mLogFile.tellp();
- if ((mFileSize >= mMaxFileSize * 1024)
+ if ((fileSize >= (mMaxFileSize * 1024))
|| (mSwitchLogEachDay && getDayChanged()))
{
// Close logfile, rename it and open a new one
mLogFile.flush();
mLogFile.close();
- // Stringify the time, the format is: yyyy-mm-dd_hh-mm-ss-logFilename.
+ // Stringify the time, the format is: path/yyyy-mm-dd-n_logFilename.
using namespace std;
ostringstream os;
os << getCurrentDate();
int fileNum = 1;
- std::string newFileName = os.str() + "-" + toString<int>(fileNum)
- + "_" + mFilename;
+ ResourceManager::splittedPath filePath =
+ ResourceManager::splitFileNameAndPath(mFilename);
+
+ std::string newFileName;
// Keeping a hard limit of 100 files per day.
- while (ResourceManager::exists(newFileName) && fileNum < 100)
+ do
{
- fileNum++;
- newFileName = os.str() + "-" + toString<int>(fileNum)
- + "_" + mFilename;
+ newFileName = filePath.path + os.str()
+ + "-" + toString<int>(fileNum)
+ + "_" + filePath.file;
}
+ while (ResourceManager::exists(newFileName, false) && ++fileNum < 100);
- if (rename(mFilename.c_str(), newFileName.c_str()))
+ if (rename(mFilename.c_str(), newFileName.c_str()) != 0)
{
- ostringstream errorOs;
- errorOs << "Error renaming file: " << mFilename << " to: "
- << newFileName << std::endl << "Continuing on the same log file.";
- perror(errorOs.str().c_str());
-
// Continue appending on the original file.
setLogFile(mFilename, true);
+ mLogFile << "Error renaming file: " << mFilename << " to: "
+ << newFileName << std::endl << "Keep logging on the same log file."
+ << std::endl;
}
else
{
// Keep the logging after emptying the original log file.
setLogFile(mFilename);
+ mLogFile << "---- Continue logging from former file " << newFileName
+ << " ----" << std::endl;
}
-
- mLogFile << "---- Continue logging from former file " << os.str()
- << " ----" << std::endl;
}
}