Server Chatlogging Help
#1

This crashes my server. This was taken from this: https://sampforum.blast.hk/showthread.php?tid=264770

The reason I didn't reply to that thread was because it would be bumping and I think it would perhaps be better to make a new thread - sorry if it was the wrong decision.

The code used:

pawn Код:
public OnPlayerText(playerid, text[])
{
    ChatLog(playerid, text);
    return 1;
}


stock ChatLog(playerid, text[])
{
    new
     File:lFile = fopen("Logs/Chat.txt", io_append),
     logData[178],
        fyear, fmonth, fday,
        fhour, fminute, fsecond;

    getdate(fyear, fmonth, fday);
    gettime(fhour, fminute, fsecond);

    format(logData, sizeof(logData),"[%02d/%02d/%04d %02d:%02d:%02d] %s: %s \r\n", fday, fmonth, fyear, fhour, fminute, fsecond, GetName(playerid), text);
    fwrite(lFile, logData);

    fclose(lFile);
    return 1;
}

stock GetName(playerid)
{
    new
     pName[MAX_PLAYER_NAME];

    GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
    return pName;
}
Is there a reason this would crash my server? I tried both return 1 and return 0 for the OnPlayerText.

Thanks.
Reply
#2

Writing to a file or closing a file without checking if the file handle (whether it opened successfully) is valid can crash the server.
pawn Код:
stock ChatLog(playerid, text[])
{
    new
        File:lFile = fopen("Logs/Chat.txt", io_append),
        logData[178],
        fyear, fmonth, fday,
        fhour, fminute, fsecond;
   
    getdate(fyear, fmonth, fday);
    gettime(fhour, fminute, fsecond);
   
    format(logData, sizeof(logData),"[%02d/%02d/%04d %02d:%02d:%02d] %s: %s \r\n", fday, fmonth, fyear, fhour, fminute, fsecond, GetName(playerid), text);
    if (lFile)
    {
        fwrite(lFile, logData);
        fclose(lFile);
    }
    return 1;
}
Another thing is the folders. Make sure you've created the correct folder (Logs) in scriptfiles because fwrite will crash the server.
Reply
#3

1. I've tried your code - no more crashing (good!) but do you have any idea why the logs don't save in the Chat.txt file?
2. Indeed, good point - but I did already have a Chat.txt file in the Logs folder (all capitalised correctly as well)
Reply
#4

Well the code inside the if statement will be executed only if the file handle is valid. If it's not (that might be your case), it will neither write to, nor close the file (since it never opened successfully).

Make sure that you've set the correct permissions in the scriptfiles folder (both readable and writable).
Reply
#5

I think you might be on to something there.

Owner permissions are read and write, the group permissions are just read. For my userfiles, both types of permissions have read and write, so I'm going to try doing the same for the Chat.txt.



---

You've fixed it. Thanks very much. I would never have thought of that, to be fair.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)