Vai al contenuto

StringToLogFile, salva stringa in un file di log

Vai all indice del manuale di programmazione
Tipo: Blocco funzione
Libreria LogicLab: eLLabLogLib
Libreria Codesys: Non disponibile

Questo blocco funzione da eseguire in task Back, effettua la scrittura di una stringa in un file su disco, ogni riga viene terminata con CR-LF. Il FB ha esecuzione atomica, può essere eseguito come una funzione.

Eseguendo il FB viene salvata su disco la stringa puntata da LData per la dimensione definita in LDLength. Nel caso sia definito il campo Header la stringa puntata verrà scritta nel file anteposta alla stringa di log.

In caso di errore esecuzione viene attivata per un loop di programma l’uscita Fault.

Descrizione

Filename (@STRING) Percorso e nome del file in cui è eseguita la scrittura (es.: ‘C:/MyFile.txt’).
Mode (DINT) Modo scrittura dati su file. 0:Ascii, 1:AsciiHex, 2:Hex.
Header (@STRING) Puntatore a stringa header che verrà scritta nel file prima della stringa log. se eNULL non viene scritta nessuna stringa.
LData (@STRING) Puntatore a stringa da scrivere nel file.
LDLength (UDINT) Definisce lunghezza stringa di log da scrivere nel file. Se 0 viene utilizzato il valore di lunghezza stringa (Solo se  stringhe Ascii).
Fault (BOOL) Attivo per un loop su errore esecuzione comando.

Immagine FB StringToLogFile_v3

Esempi

Come utilizzare gli esempi.

ST_StringToLogFile: Viene dimostrato l’utilizzo del FB ogni volta che da debug si attiva LogOn viene salvato un record di log nel file definito.

ST_MontlyLog: Attivando da debug le variabili ELog[x] viene scritto un record di log in un file. Ogni scrittura è accodata alla scrittura precedente.

ST_DataLoggerOnCSVFile: Ogni 5 minuti viene eseguita la scrittura di un record di dati in un file CSV. Il file potrà essere così facilmente importato in Excel per un esame dei dati.

LogicLab (Ptp136, ST_StringToLogFile)
PROGRAM ST_StringToLogFile
VAR
    LogOn : BOOL; (* Log command *)
    Header : STRING[ 32 ]; (* Header string *)
    Logger : StringToLogFile_v3; (* Data logger *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_StringToLogFile"
// *****************************************************************************
// Every time is set by debug the LogOn variable a log string is written on the
// defined file.
//
// The log row is like the following.
// 07/04/21 08:20:39|Example| Log string[0D][0A]
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Execute program initialization.

    IF (SysFirstLoop) THEN
        Logger.Mode:=1; //AsciiHex mode
        Logger.LDLength:=0; //Log data length
        Logger.Header:=ADR(Header); //Header string
        Logger.Filename:=ADR('C:/Log.txt'); //Log file
    END_IF;

    // -------------------------------------------------------------------------
    // DATA SAMPLE
    // -------------------------------------------------------------------------
    // Log command.

    IF NOT(LogOn) THEN RETURN; END_IF;
    LogOn:=FALSE; //Log command

    // Create the log header with Date/Time and a label.

    eTO_JUNK(DateTimeFormat(TO_LDATE_AND_TIME(SysDateGetNs()), ADR('^d/m/y H\:i\:s'), ADR(Header), SIZEOF(Header))); //06/04/21 15:56:59
    eTO_JUNK(SysCVsnprintf(ADR(Header), SIZEOF(Header), ADR('|%s| '), STRING_TYPE, ADR('Example')));

    // Execute the log.

    Logger.LData:=ADR('Log string$r$n'); //Data to log
    Logger(); //Write data to file

// [End of file]
LogicLab (Ptp136, ST_MontlyLog)
PROGRAM ST_MontlyLog
VAR
    i : UDINT; (* Auxiliary variable *)
    j : INT; (* Auxiliary variable *)
    AMonth : INT; (* Actual month *)
    ELog : ARRAY[0..3] OF BOOL; (* Event log trigger *)
    Filename : STRING[ 32 ]; (* Log file name *)
    Months : ARRAY[0..11] OF @STRING; (* Months definitions *)
    LDTS : LDATETIMESTRUCT; (* Long Date/Time struct *)
    Logger : StringToLogFile_v3; (* Data logger *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_MontlyLogs"
// *****************************************************************************
// A monthly log is maintained, setting by debug the "ELog" variables, a log
// record is written in a file named with the name of the current month.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Execute program initialization.

    IF (SysFirstLoop) THEN

        // Defines the months,

        Months[0]:=ADR('January'); 
        Months[1]:=ADR('February');
        Months[2]:=ADR('March');
        Months[3]:=ADR('April');
        Months[4]:=ADR('May');
        Months[5]:=ADR('June');
        Months[6]:=ADR('July');
        Months[7]:=ADR('August');
        Months[8]:=ADR('September');
        Months[9]:=ADR('October');
        Months[10]:=ADR('November');
        Months[11]:=ADR('December');

        // Inizialize the logger it's set in queing mode.

        Logger.Mode:=1; //AsciiHex mode
        Logger.LDLength:=0; //Log data length
        Logger.Header:=eNULL; //Header string
        Logger.Filename:=ADR(Filename); //Log file
    END_IF;

    // -------------------------------------------------------------------------
    // LOG FILENAME
    // -------------------------------------------------------------------------
    // Assign data values.

    eTO_JUNK(SPLIT_DT(TO_DATE_AND_TIME(SysDateGetS()), ADR(LDTS.Year), ADR(LDTS.Month), ADR(LDTS.Day), ADR(LDTS.Hours), ADR(LDTS.Minutes), ADR(LDTS.Seconds)));

    // At everyAMonth new month a log filename is generated. 

    IF (SysFirstLoop OR (LDTS.Month <> AMonth)) THEN
        AMonth:=LDTS.Month; //Actual month

        // To save disk space only the last 6 months are kept, so the 6 months
        // files which name is following the current month are deleted.

        FOR j:=(AMonth+1) TO (AMonth+6) DO
            IF (j > 12) THEN i:=TO_UDINT(j-12); ELSE i:=TO_UDINT(j); END_IF;

            eTO_JUNK(SysVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('C:/%s.txt'), STRING_TYPE, Months[i-1]));
            IF (SysGetFileLen(ADR(Filename)) <> eEOF) THEN eTO_JUNK(SysFileRemove(ADR(Filename))); END_IF;
            eTO_JUNK(SysWrSpyData(SPY_ASCII, 0, 16#00000001, ADR('Checked'), ADR(Filename)));
        END_FOR;

        // Log file name is defined here.

        eTO_JUNK(SysVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('C:/%s.txt'), STRING_TYPE, Months[AMonth-1]));
    END_IF;

    // -------------------------------------------------------------------------
    // EVENT LOGS
    // -------------------------------------------------------------------------
    // Some event logs are generated, just to show how it's possible to store in
    // the log file the events.

    IF (ELog[0]) THEN ELog[0]:=FALSE; Logger(LData:=ADR('ELog[0] triggered event')); END_IF;
    IF (ELog[1]) THEN ELog[1]:=FALSE; Logger(LData:=ADR('ELog[1] triggered event')); END_IF;
    IF (ELog[2]) THEN ELog[2]:=FALSE; Logger(LData:=ADR('ELog[2] triggered event')); END_IF;
    IF (ELog[3]) THEN ELog[3]:=FALSE; Logger(LData:=ADR('ELog[3] triggered event')); END_IF;

// [End of file]
LogicLab (Ptp136, ST_DataLoggerOnCSVFile)
PROGRAM ST_DataLoggerOnCSVFile
VAR
    i : UDINT; (* Auxiliary variable *)
    j : UDINT; (* Auxiliary variable *)
    Pulse : BOOL; (* Pulse status *)
    Months : ARRAY[0..11] OF @STRING; (* Month names definition *)
    SSize : UINT := 512; (* String size (SysRMalloc) *)
    SAddress : @STRING; (* String address (SysRMalloc) *)
    Filename : STRING[ 64 ]; (* Log file name *)
    FPath : STRING[ 32 ] := 'C:/Storage/'; (* Log file path *)
    Variable : ARRAY[0..2] OF REAL := [12.3, 1200.5, 74328.4]; (* Example variables *)
    LDTS : LDATETIMESTRUCT; (* Long Date/Time struct *)
    Logger : StringToLogFile_v3; (* Log manager *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_DataLoggerOnCSVFile"
// *****************************************************************************
// This program schedules the storing of variable values on a CSV file.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Execute program initialization.

    IF (SysFirstLoop) THEN

        // Defines the months,

        Months[0]:=ADR('January'); 
        Months[1]:=ADR('February');
        Months[2]:=ADR('March');
        Months[3]:=ADR('April');
        Months[4]:=ADR('May');
        Months[5]:=ADR('June');
        Months[6]:=ADR('July');
        Months[7]:=ADR('August');
        Months[8]:=ADR('September');
        Months[9]:=ADR('October');
        Months[10]:=ADR('November');
        Months[11]:=ADR('December');

        // Inizialize the logger it's set in queing mode.

        Logger.Mode:=1; //AsciiHex mode
        Logger.LDLength:=0; //Log data length
        Logger.Header:=eNULL; //Header string
        Logger.Filename:=ADR(Filename); //Log file
    END_IF;

    // -------------------------------------------------------------------------
    // LOG EXECUTION TIMING
    // -------------------------------------------------------------------------
    // Every 5 minutes execute a log report write.

    IF (SAddress <> eNULL) THEN eTO_JUNK(SysRMFree(ADR(SAddress))); END_IF;
    IF (MOD(SysDateTime, 300) <> 0) THEN Pulse:=FALSE; RETURN; END_IF;
    IF (Pulse) THEN RETURN; END_IF;

    // Allocate the log string buffer.

    IF NOT(SysRMAlloc(SSize, ADR(SAddress))) THEN RETURN; END_IF;
    Pulse:=TRUE; //Pulse status

    // -------------------------------------------------------------------------
    // MANAGE THE DISK FILE
    // -------------------------------------------------------------------------
    // Assign data values.

    eTO_JUNK(SPLIT_DT(TO_DATE_AND_TIME(SysDateGetS()), ADR(LDTS.Year), ADR(LDTS.Month), ADR(LDTS.Day), ADR(LDTS.Hours), ADR(LDTS.Minutes), ADR(LDTS.Seconds)));

    // Deletes the 6 months older than the current one.

    FOR j:=TO_UDINT(LDTS.Month+1) TO TO_UDINT(LDTS.Month+6) DO
        eTO_JUNK(SysVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('%s'), STRING_TYPE, ADR(FPath)));

        IF (j > 12) THEN i:=j-12; ELSE i:=j; END_IF;
        eTO_JUNK(SysCVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('C:/%s.txt'), STRING_TYPE, Months[i-1]));
        IF (SysGetFileLen(ADR(Filename)) <> eEOF) THEN eTO_JUNK(SysFileRemove(ADR(Filename))); END_IF;
        eTO_JUNK(SysWrSpyData(SPY_ASCII, 0, 16#00000001, ADR('Checked'), ADR(Filename)));
    END_FOR;

    // Create the log file name.

    eTO_JUNK(SysVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('%s'), STRING_TYPE, ADR(FPath)));
    eTO_JUNK(SysCVsnprintf(ADR(Filename), SIZEOF(Filename), ADR('%s.csv'), STRING_TYPE, Months[LDTS.Month-1]));

    // If file doesn't exist it will be created with the columns definition.

    IF (SysGetFileLen(ADR(Filename)) = eEOF) THEN
        eTO_JUNK(SysVsnprintf(SAddress, SSize, ADR('%s, '), STRING_TYPE, ADR('Date')));
        eTO_JUNK(SysCVsnprintf(SAddress, SSize, ADR('%s, '), STRING_TYPE, ADR('VariableA')));
        eTO_JUNK(SysCVsnprintf(SAddress, SSize, ADR('%s, '), STRING_TYPE, ADR('VariableB')));
        eTO_JUNK(SysCVsnprintf(SAddress, SSize, ADR('%s'), STRING_TYPE, ADR('VariableC')));
        Logger(LData:=SAddress); //FB gestione log
    END_IF;

    // -------------------------------------------------------------------------
    // WRITE THE LOG RECORD
    // -------------------------------------------------------------------------
    // Initialize it with date and time.

    eTO_JUNK(DateTimeFormat(TO_LDATE_AND_TIME(SysDateGetNs()), ADR('^d/m/Y H\:i\:s\, '), SAddress, SSize)); //Date/Time 22/09/2022 16:21:08

    // Insert column values separated by a ",".

    eTO_JUNK(SysCVsnprintf(SAddress, SSize, ADR('%.1f, '), REAL_TYPE, ADR(Variable[0])));
    eTO_JUNK(SysCVsnprintf(SAddress, SSize, ADR('%.1f, '), REAL_TYPE, ADR(Variable[1])));
    eTO_JUNK(SysCVsnprintf(SAddress, SSize, ADR('%.1f'), REAL_TYPE, ADR(Variable[2])));
    Logger(LData:=SAddress); //Store record on disk

// [End of file]
Was this article helpful?