summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-07-25 21:36:37 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-07-25 21:36:37 +0000
commite6d8d2dd8eff63c23f0ffad25c5e2529bde93485 (patch)
treecce9c591350ce1d29c67df3339c526b182e8763e /src
parent33e19fb0822308a630ca6e1122b9b97882a120a7 (diff)
downloadMana-e6d8d2dd8eff63c23f0ffad25c5e2529bde93485.tar.gz
Mana-e6d8d2dd8eff63c23f0ffad25c5e2529bde93485.tar.bz2
Mana-e6d8d2dd8eff63c23f0ffad25c5e2529bde93485.tar.xz
Mana-e6d8d2dd8eff63c23f0ffad25c5e2529bde93485.zip
Added support for writing log to standard output.
Diffstat (limited to 'src')
-rw-r--r--src/configuration.cpp11
-rw-r--r--src/log.cpp9
-rw-r--r--src/log.h11
-rw-r--r--src/main.cpp137
-rw-r--r--src/resources/resourcemanager.cpp5
5 files changed, 109 insertions, 64 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp
index 4085b20b..9c3ff008 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -79,6 +79,8 @@ void Configuration::write()
// Do not attempt to write to file that cannot be opened for writing
FILE *testFile = fopen(mConfigPath.c_str(), "w");
if (!testFile) {
+ logger->log("Configuration::write() couldn't open %s for writing",
+ mConfigPath.c_str());
return;
}
else {
@@ -89,15 +91,14 @@ void Configuration::write()
if (writer)
{
+ logger->log("Configuration::write() writing configuration...");
+
xmlTextWriterSetIndent(writer, 1);
xmlTextWriterStartDocument(writer, NULL, NULL, NULL);
xmlTextWriterStartElement(writer, BAD_CAST "configuration");
for (OptionIterator i = mOptions.begin(); i != mOptions.end(); i++)
{
- logger->log("Configuration::write(%s, \"%s\")",
- i->first.c_str(), i->second.c_str());
-
xmlTextWriterStartElement(writer, BAD_CAST "option");
xmlTextWriterWriteAttribute(writer,
BAD_CAST "name", BAD_CAST i->first.c_str());
@@ -109,6 +110,10 @@ void Configuration::write()
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
}
+ else
+ {
+ logger->log("Configuration::write() error while creating writer");
+ }
}
void Configuration::setValue(const std::string &key, std::string value)
diff --git a/src/log.cpp b/src/log.cpp
index 3d101d29..07eb55f7 100644
--- a/src/log.cpp
+++ b/src/log.cpp
@@ -31,6 +31,10 @@
#include <iostream>
#include <sstream>
+Logger::Logger():
+ mLogToStandardOut(false)
+{
+}
Logger::~Logger()
{
@@ -85,6 +89,11 @@ void Logger::log(const char *log_text, ...)
mLogFile << timeStr.str() << buf << std::endl;
+ if (mLogToStandardOut)
+ {
+ std::cout << timeStr.str() << buf << std::endl;
+ }
+
// Delete temporary buffer
delete[] buf;
}
diff --git a/src/log.h b/src/log.h
index 4876a9aa..36328527 100644
--- a/src/log.h
+++ b/src/log.h
@@ -32,6 +32,11 @@ class Logger
{
public:
/**
+ * Constructor.
+ */
+ Logger();
+
+ /**
* Destructor, closes log file.
*/
~Logger();
@@ -42,6 +47,11 @@ class Logger
void setLogFile(const std::string &logFilename);
/**
+ * Sets whether the log should be written to standard output.
+ */
+ void setLogToStandardOut(bool value) { mLogToStandardOut = value; }
+
+ /**
* Enters a message in the log. The message will be timestamped.
*/
void log(const char *log_text, ...);
@@ -54,6 +64,7 @@ class Logger
private:
std::ofstream mLogFile;
+ bool mLogToStandardOut;
};
extern Logger *logger;
diff --git a/src/main.cpp b/src/main.cpp
index 0c75c653..ca70b0dc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -93,6 +93,7 @@ SERVER_INFO **server_info;
unsigned char state;
std::string errorMessage;
+std::string homeDir;
unsigned char screen_mode;
Sound sound;
@@ -109,24 +110,12 @@ namespace {
}
/**
- * Do all initialization stuff
+ * Initializes the home directory. On UNIX and FreeBSD, ~/.tmw is used. On
+ * Windows and other systems we use the current working directory.
*/
-void init_engine()
+void initHomeDir()
{
- // Initialize SDL
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
- std::cerr << "Could not initialize SDL: " <<
- SDL_GetError() << std::endl;
- exit(1);
- }
- atexit(SDL_Quit);
-
- SDL_EnableUNICODE(1);
- SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
-
- std::string homeDir = "";
#if !(defined __USE_UNIX98 || defined __FreeBSD__)
- // In Windows and other systems we currently store data next to executable.
homeDir = ".";
#else
homeDir = std::string(PHYSFS_getUserDir()) + "/.tmw";
@@ -141,35 +130,13 @@ void init_engine()
exit(1);
}
#endif
+}
- // Set log file
- logger->setLogFile(homeDir + std::string("/tmw.log"));
-
- ResourceManager *resman = ResourceManager::getInstance();
-
- if (!resman->setWriteDir(homeDir)) {
- std::cout << homeDir
- << " couldn't be set as home directory! Exitting."
- << std::endl;
- exit(1);
- }
-
- // Add the user's homedir to PhysicsFS search path
- resman->addToSearchPath(homeDir, false);
- // Creating and checking the updates folder existence and rights.
- if (!resman->isDirectory("/updates")) {
- if (!resman->mkdir("/updates")) {
- std::cout << homeDir << "/updates "
- << "can't be made, but it doesn't exist! Exitting."
- << std::endl;
- exit(1);
- }
- }
-
- // Add the main data directory to our PhysicsFS search path
- resman->addToSearchPath("data", true);
- resman->addToSearchPath(TMW_DATADIR "data", true);
-
+/**
+ * Initialize configuration.
+ */
+void initConfiguration()
+{
// Fill configuration with defaults
config.setValue("host", "animesites.de");
config.setValue("port", 9601);
@@ -185,13 +152,13 @@ void init_engine()
config.setValue("remember", 1);
config.setValue("sfxVolume", 100);
config.setValue("musicVolume", 60);
- config.setValue("fpslimit", 50);
+ config.setValue("fpslimit", 0);
config.setValue("updatehost", "http://themanaworld.org/files");
config.setValue("customcursor", 1);
config.setValue("homeDir", homeDir);
- // Checking if the configuration file exists... otherwise creates it with
- // default options !
+ // Checking if the configuration file exists... otherwise create it with
+ // default options.
FILE *tmwFile = 0;
std::string configPath = homeDir + "/config.xml";
tmwFile = fopen(configPath.c_str(), "r");
@@ -203,15 +170,57 @@ void init_engine()
}
if (tmwFile == NULL) {
std::cout << "Can't create " << configPath << ". "
- "Using Defaults." << std::endl;
+ << "Using Defaults." << std::endl;
} else {
fclose(tmwFile);
config.init(configPath);
}
+}
+
+/**
+ * Do all initialization stuff
+ */
+void init_engine()
+{
+ // Initialize SDL
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
+ std::cerr << "Could not initialize SDL: " <<
+ SDL_GetError() << std::endl;
+ exit(1);
+ }
+ atexit(SDL_Quit);
+
+ SDL_EnableUNICODE(1);
+ SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
SDL_WM_SetCaption("The Mana World", NULL);
SDL_WM_SetIcon(IMG_Load(TMW_DATADIR "data/icons/tmw-icon.png"), NULL);
+ ResourceManager *resman = ResourceManager::getInstance();
+
+ if (!resman->setWriteDir(homeDir)) {
+ std::cout << homeDir
+ << " couldn't be set as home directory! Exitting."
+ << std::endl;
+ exit(1);
+ }
+
+ // Add the user's homedir to PhysicsFS search path
+ resman->addToSearchPath(homeDir, false);
+ // Creating and checking the updates folder existence and rights.
+ if (!resman->isDirectory("/updates")) {
+ if (!resman->mkdir("/updates")) {
+ std::cout << homeDir << "/updates "
+ << "can't be made, but it doesn't exist! Exitting."
+ << std::endl;
+ exit(1);
+ }
+ }
+
+ // Add the main data directory to our PhysicsFS search path
+ resman->addToSearchPath("data", true);
+ resman->addToSearchPath(TMW_DATADIR "data", true);
+
int width, height, bpp;
bool fullscreen, hwaccel;
@@ -238,12 +247,12 @@ void init_engine()
graphics = new Graphics();
#endif
-
// Try to set the desired video mode
if (!graphics->setVideoMode(width, height, bpp, fullscreen, hwaccel))
{
- std::cerr << "Couldn't set " << width << "x" << height << "x" <<
- bpp << " video mode: " << SDL_GetError() << std::endl;
+ std::cerr << "Couldn't set "
+ << width << "x" << height << "x" << bpp << " video mode: "
+ << SDL_GetError() << std::endl;
exit(1);
}
@@ -258,7 +267,7 @@ void init_engine()
if (!playerset[1]) logger->error("Couldn't load female player spriteset!");
- for (int i=0; i < NR_HAIR_STYLES; i++)
+ for (int i = 0; i < NR_HAIR_STYLES; i++)
{
Spriteset *tmp = ResourceManager::getInstance()->getSpriteset(
"graphics/sprites/hairstyle" + toString(i + 1) + ".png",
@@ -312,7 +321,6 @@ void exit_engine()
sound.close();
ResourceManager::deleteInstance();
- delete logger;
}
/**
@@ -486,20 +494,30 @@ int main(int argc, char *argv[])
#ifdef PACKAGE_VERSION
std::cout << "The Mana World v" << PACKAGE_VERSION << std::endl;
#endif
- logger = new Logger();
+ // Parse command line options
Options options;
-
parseOptions(argc, argv, options);
-
if (options.printHelp)
{
printHelp();
return 0;
}
+ // Initialize PhysicsFS
+ PHYSFS_init(argv[0]);
+
+ initHomeDir();
+ initConfiguration();
+
+ // Configure logger
+ logger = new Logger();
+ logger->setLogFile(homeDir + std::string("/tmw.log"));
+ logger->setLogToStandardOut(config.getValue("logToStandardOut", 0));
+
// Initialize libxml2 and check for potential ABI mismatches between
// compiled version and the shared library actually used.
+ logger->log("Initializing libxml2...");
xmlInitParser();
LIBXML_TEST_VERSION;
@@ -507,13 +525,8 @@ int main(int argc, char *argv[])
FILE *nullFile = fopen("/dev/null", "w");
xmlSetGenericErrorFunc(nullFile, NULL);
- // Initialize PhysicsFS
- PHYSFS_init(argv[0]);
-
init_engine();
- SDL_Event event;
-
if (options.skipUpdate && state != ERROR_STATE) {
state = LOGIN_STATE;
}
@@ -547,6 +560,9 @@ int main(int argc, char *argv[])
logger->error("An error occurred while initializing ENet.");
}
Network *network = new Network();
+
+ SDL_Event event;
+
while (state != EXIT_STATE)
{
// Handle SDL events
@@ -712,5 +728,6 @@ int main(int argc, char *argv[])
logger->log("State: EXIT");
exit_engine();
PHYSFS_deinit();
+ delete logger;
return 0;
}
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index 26899d77..6353afac 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -72,7 +72,10 @@ void
ResourceManager::cleanUp(Resource *res)
{
logger->log("ResourceManager::~ResourceManager() cleaning up %d "
- "references to %s", res->mRefCount, res->mIdPath.c_str());
+ "reference%s to %s",
+ res->mRefCount,
+ (res->mRefCount == 1) ? "" : "s",
+ res->mIdPath.c_str());
delete res;
}