AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Generate file depend date (https://forums.alliedmods.net/showthread.php?t=336766)

Lmfao 03-10-2022 10:56

Generate file depend date
 
Hello.
I want to generate a file with the date. This following code work.

But it doesn't generate a new file when the date change.
It change only if the server restart or if the plugin is reloaded.

Here's my code actualy.
PHP Code:

FormatTime(g_sDatesizeof(g_sDate), "%F"GetTime());
    
BuildPath(Path_SMg_sPathsizeof(g_sPath), "logs/test/custom_%s.log"g_sDate); 


But i don't know wich part of this original code is really needed for me.
https://github.com/alliedmodders/sou...ogger.cpp#L360

MAGNAT2645 03-10-2022 11:06

Re: Generate file depend date
 
Post the full code because i don't see how you create files.

EDIT 1: Here are some examples how to write logs (if that's what you want)

Code:

// Using LogToFile/LogToFileEx
// https://sourcemod.dev/#/logging/function.LogToFile
// https://sourcemod.dev/#/logging/function.LogToFileEx
LogToFile( g_sPath, "text 1" );
LogToFileEx( g_sPath, "text 2" );

// Using LogToOpenFile/LogToOpenFileEx
// https://sourcemod.dev/#/files/function.LogToOpenFile
// https://sourcemod.dev/#/files/function.LogToOpenFileEx
File hFile = OpenFile( g_sPath, "a" );
if ( hFile ) {
    LogToOpenFile( hFile, "text 3" );
    LogToOpenFileEx( hFile, "text 4" );
}

// Using File.WriteLine (https://sourcemod.dev/#/files/methodmap.File/function.WriteLine)
if ( hFile ) {
    char sTime[32];
    FormatTime( sTime, sizeof sTime, "%m/%d/%Y - %H:%M:%S", GetTime() );
    hFile.WriteLine( "L %s: text 5", sTime );
    hFile.Flush();
}

EDIT 2: Generating files with the date depends on where you create them (in which forward you build path and create files).

Lmfao 03-10-2022 12:16

Re: Generate file depend date
 
Not updated with new declaration yet.
https://pastebin.com/LvqAkHqX

MAGNAT2645 03-10-2022 12:34

Re: Generate file depend date
 
That's because you're doing it only in OnPluginStart which is called only once after server start or when you (re)load the plugin manually.
You should rebuild the path at least in OnMapStart which is called after every map change.
The better way is to create a repeating timer (for example, with interval of 1 minute) and rebuild path in timer's callback.
And the best way is to rebuild path before every LogToFile call which is slightly unoptimized but still fast enough.

Code:

stock void LogToDateFile(const char[] message, any ...) {
    char sMessage[512];
    VFormat( sMessage, sizeof sMessage, message, 2 );

    char sTime[12], sPath[PLATFORM_MAX_PATH];
    FormatTime( sTime, sizeof sTime, "%F", GetTime() );
    BuildPath( Path_SM, sPath, sizeof sPath, "logs/test/custom_%s.log", sTime );
    LogToFile( sPath, "%s", sMessage );
}

In this case you don't need g_sPath and can just call LogToDateFile every time.

Lmfao 03-11-2022 09:01

Re: Generate file depend date
 
Works ! Thanks :)


All times are GMT -4. The time now is 05:58.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.