• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

write errors to file

  • Thread starter Thread starter Emky
  • Start date Start date
E

Emky

Guest
ok, so i managed to get it to write the last error message to the file, but i want it to list all before the last aswell


Code:
uint32_t totalWarnings = g_config.getNumber(ConfigManager::CONSOLE_SPAWN_WARNING);
    static uint8_t qtdWarnings = 0;
    if (qtdWarnings < totalWarnings){ //we stop spamming the screen with errors after the last message
        Tile* tile = g_game.getTile(_pos);

        if(!tile || tile->isMoveableBlocking()){
           
            ofstream myfile("spawn_errors.txt");

            if (myfile.is_open())
            {
                myfile << _name << " - position : " << _pos << "." << std::endl;
                myfile.close();
            }
            else {
                cout << "Unable to open file";
            }

            std::cout << "Warning: [Spawn::addMonster] Position " << _pos << " is not valid. Could not place " << _name << "." << std::endl;
            qtdWarnings++;
           

            if (qtdWarnings == totalWarnings){
                std::cout << "Too many monsters at invalid positions. We will skip informing you of further errors." << std::endl;
            }
        }
    }
 
Hello Emky,

If that short piece of code is inside a for loop or it is called multiple times, the file is being overwritten. That happens because you open the file, write something to it and then close the file, if you do that again it will be written at the beginning of the file.

So, you can try to open the file with the append option, like this:
Code:
uint32_t totalWarnings = g_config.getNumber(ConfigManager::CONSOLE_SPAWN_WARNING);
    static uint8_t qtdWarnings = 0;
    if (qtdWarnings < totalWarnings){ //we stop spamming the screen with errors after the last message
        Tile* tile = g_game.getTile(_pos);

        if(!tile || tile->isMoveableBlocking()){
          
            ofstream myfile("spawn_errors.txt", ios::out | ios::app);

            if (myfile.is_open())
            {
                myfile << _name << " - position : " << _pos << "." << std::endl;
                myfile.close();
            }
            else {
                cout << "Unable to open file";
            }

            std::cout << "Warning: [Spawn::addMonster] Position " << _pos << " is not valid. Could not place " << _name << "." << std::endl;
            qtdWarnings++;
          

            if (qtdWarnings == totalWarnings){
                std::cout << "Too many monsters at invalid positions. We will skip informing you of further errors." << std::endl;
            }
        }
    }

The line that I changed is this one:
Code:
ofstream myfile("spawn_errors.txt", ios::out | ios::app);

Good luck.
 

Similar threads

Back
Top