Printing login and exit date ip hour minute second to server

Started by Matite, May 23, 2023, 05:22 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Matite

In the above code, a file named "logFile" is defined, and in the "OnGameModeInit" function, it opens the "log.txt" file in write mode. If the file cannot be opened, an error message is displayed.

Additionally, in the "OnPlayerConnect" and "OnPlayerDisconnect" functions, we record logs for player connections and player disconnections. These functions will be executed when the respective events occur, and they will retrieve the player's IP address and include it in the log. Each log line includes the date, time, player ID, and IP address.

Finally, the log file is closed in the "OnGameModeExit" function.

By using this code, you can create a file named "log.txt" on your SA-MP server and save events such as player connections and disconnections to this file. You can customize the relevant parts according to your requirements.



new File:logFile;

public OnGameModeInit()
{
    logFile = fopen("log.txt", "a");
    if (logFile == INVALID_FILE)
    {
        printf("ERROR: Failed to open log.txt file for writing!");
        return 0;
    }

    return 1;
}

public OnPlayerConnect(playerid)
{
    new date[24];
    getdate(date, sizeof(date));

    new time[24];
    gettime(time, sizeof(time));

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    fprintf(logFile, "[%s %s] Player %d connected. IP: %s\n", date, time, playerid, ip);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    new date[24];
    getdate(date, sizeof(date));

    new time[24];
    gettime(time, sizeof(time));

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    fprintf(logFile, "[%s %s] Player %d disconnected. IP: %s Reason: %d\n", date, time, playerid, ip, reason);
    return 1;
}

public OnGameModeExit()
{
    fclose(logFile);
    return 1;
}