Raised This Month: $51 Target: $400
 12% 

Solved Generate file depend date


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Lmfao
Junior Member
Join Date: Oct 2011
Old 03-10-2022 , 10:56   Generate file depend date
Reply With Quote #1

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

Last edited by Lmfao; 03-11-2022 at 09:01.
Lmfao is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 03-10-2022 , 11:06   Re: Generate file depend date
Reply With Quote #2

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).
__________________

Last edited by MAGNAT2645; 03-10-2022 at 11:24.
MAGNAT2645 is offline
Lmfao
Junior Member
Join Date: Oct 2011
Old 03-10-2022 , 12:16   Re: Generate file depend date
Reply With Quote #3

Not updated with new declaration yet.
https://pastebin.com/LvqAkHqX
Lmfao is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 03-10-2022 , 12:34   Re: Generate file depend date
Reply With Quote #4

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.
__________________

Last edited by MAGNAT2645; 03-10-2022 at 12:42.
MAGNAT2645 is offline
Lmfao
Junior Member
Join Date: Oct 2011
Old 03-11-2022 , 09:01   Re: Generate file depend date
Reply With Quote #5

Works ! Thanks
Lmfao is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:44.


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