Home AMX User Forum NetLinx Studio

Determining file size

I want to write module, which write some diagnostic messages (like in notifications) from devices to NetLinx controller's file system.
I want to limit file size with some, but i don't know how to determine size of this file.
Yes, i can count number of char in for loops, but this is not good way.
Also, i want to know how many free space on controller.
Do you know how to do this?

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    File Size

    When writing to my log file I do a FILE_OPEN and then I do a FILE_SEEK to the end of the file (-1 for Pos parameter.)

    The return value of FILE_SEEK if positive = the byte size of the file.

    If the new message length + the current file length is > my max size it’s time to start a new file.

    HTH
  • whitewhite Posts: 7
    My debug to file module

    Thanks.
    This is my new module. It writes notifications to file.
    MODULE_NAME='DebugToFile'(dev dvAll[],char FileName[30],LONG MaxFileSize)
    (*
    Author: Kurbanov Radik (WiseHouse)
    Date: 31.08.10
    Version: 0.1a
    License: AsIs
    Support: dostup at mail dot ru
    *)
    (*
    TODO:
    //File Size Limitation
    change formating postoa 21:1:0---->00021:01:0
    *)

    #INCLUDE 'DEBUGMSG_Include'

    DEFINE_CONSTANT
    INTEGER HEADER_LENGTH=64
    INTEGER MESSAGE_LENGHT=512
    LONG ALL_LENGTH=HEADER_LENGTH+MESSAGE_LENGHT

    DEFINE_VARIABLE
    SLONG hFile
    CHAR Header[HEADER_LENGTH]
    CHAR Message[MESSAGE_LENGHT]
    LONG CurentFileSize
    DEFINE_FUNCTION pWrite()
    {
    IF (hFile>0)
    {
    IF (CurentFileSize+LENGTH_ARRAY(Header)+LENGTH_ARRAY(Message)<MaxFileSize)
    {
    FILE_WRITE_LINE(hFile,"Header,Message",ALL_LENGTH)
    CurentFileSize=FILE_SEEK(hFile,-1)
    }
    ELSE
    {
    //CreateNewFile?
    }
    }
    ELSE
    pDebugMSG('Invalid file handle',MSG_INFO)
    }
    DEFINE_START
    hFile=FILE_OPEN(FileName, FILE_RW_APPEND)
    IF (hFile>0)
    {
    CurentFileSize=FILE_SEEK(hFile,-1)
    }
    DEFINE_EVENT
    DATA_EVENT[dvAll]
    {
    ONLINE:
    {
    Header="DATE,'|',TIME,'|',dpstoa(DATA.DEVICE),' |','Online :'"
    Message=''
    pWrite()
    }
    OFFLINE:
    {
    Header="DATE,'|',TIME,'|',dpstoa(DATA.DEVICE),' |','Offline:'"
    Message=''
    pWrite()
    }
    STRING:
    {
    Header="DATE,'|',TIME,'|',dpstoa(DATA.DEVICE),' |','String :'"
    Message=DATA.TEXT
    pWrite()
    }
    COMMAND:
    {
    Header="DATE,'|',TIME,'|',dpstoa(DATA.DEVICE),' |','Command:'"
    Message=DATA.TEXT
    pWrite()
    }
    AWAKE:
    {
    Header="DATE,'|',TIME,'|',dpstoa(DATA.DEVICE),' |','Awake :'"
    Message=DATA.TEXT
    pWrite()
    }
    }

    BUTTON_EVENT[dvAll,0]
    {
    PUSH:
    {
    Header="DATE,'|',TIME,'|',dpstoa(BUTTON.INPUT.DEVICE),' |','Button :'"
    Message="'Push :',ITOA(BUTTON.INPUT.CHANNEL)"
    pWrite()
    }
    RELEASE:
    {
    Header="DATE,'|',TIME,'|',dpstoa(BUTTON.INPUT.DEVICE),' |','Button :'"
    Message="'Release:',ITOA(BUTTON.INPUT.CHANNEL)"
    pWrite()
    }
    }

    CHANNEL_EVENT[dvAll,0]
    {
    ON:
    {
    Header="DATE,'|',TIME,'|',dpstoa(CHANNEL.DEVICE),' |','Channel:'"
    Message="'On :',ITOA(CHANNEL.CHANNEL)"
    pWrite()
    }
    OFF:
    {
    Header="DATE,'|',TIME,'|',dpstoa(CHANNEL.DEVICE),' |','Channel:'"
    Message="'Off :',ITOA(CHANNEL.CHANNEL)"
    pWrite()
    }
    }
    +
    PROGRAM_NAME='DebugMSG_INCLUDE'
    (***********************************************************)
    (* FILE_LAST_MODIFIED_ON: 08/02/2010 AT: 10:56:47 *)
    (***********************************************************)
    #IF_NOT_DEFINED __DebugMSG_INCLUDE__
    #DEFINE __DebugMSG_INCLUDE__

    DEFINE_CONSTANT
    CHAR MSG_ALL=0
    CHAR MSG_DONTSHOW=0
    CHAR MSG_SPAM =0
    CHAR MSG_INFO =1
    CHAR MSG_WARNING=2
    CHAR MSG_ERROR =3
    CHAR MSG_TEXT[4][10]=
    {
    {'Spam'},
    {'Info'},
    {'WARNING'},
    {'ERROR'}
    }
    DEFINE_VARIABLE
    CHAR strCompName[30]=''
    CHAR cDebugMode=MSG_INFO

    DEFINE_FUNCTION pDebugSetTitle(char zzz[30])
    {
    strCompName=zzz
    }

    DEFINE_FUNCTION char pDebugMSG (char dbgtext[],char MSG_CODE)
    {
    if (MSG_CODE>=cDebugMode)
    IF (LENGTH_ARRAY(strCompName)>0)
    send_string 0,"MSG_TEXT[MSG_CODE+1],':',strCompName,':',dbgtext"
    ELSE
    send_string 0,"MSG_TEXT[MSG_CODE+1],':',dbgtext"
    }

    //*******************************************************************
    // Function : println *
    // Purpose : to print a line to the telnet session *
    // Params : sName - name of the file to print *
    // Params : sTxt - the data to print to the telnet session *
    // Return : none *
    //*******************************************************************
    define_function println (char sTxt[])
    {
    if (cDebugMode > 2)
    {
    if (LENGTH_STRING(strCompName) > 0)
    send_string 0, "'** Message from ', strCompName, ': ',sTxt"
    else
    send_string 0, "'** Message: ',sTxt"
    }
    }
    //*******************************************************************
    // Function : dpstoa *
    // Purpose : to convert a device's DPS to ascii for displaying *
    // Params : dvIn - the device to be represented in ascii *
    // Return : char[20] - the ascii representation of the dvIn's DPS *
    //*******************************************************************
    define_function char[20] dpstoa (dev dvIn)
    {
    return "itoa(dvIn.number),':',itoa(dvIn.port),':',itoa(dvIn.system)"
    }

    DEFINE_FUNCTION CHAR[765] HexArrayToText (char HexArray[255])
    {
    INTEGER Ind
    CHAR OutText[765]
    CHAR TMP[2]
    LONG LNGTH
    OutText=''
    LNGTH=LENGTH_ARRAY(HexArray)
    SET_LENGTH_ARRAY(OutText,LNGTH*3)
    FOR(Ind=1;Ind<=LNGTH;Ind++)
    {
    TMP=ITOHEX(HexArray[Ind])
    OutText[3*(Ind-1)+1]=TMP[1]
    OutText[3*(Ind-1)+2]=TMP[2]
    OutText[3*(Ind-1)+3]=','
    }
    RETURN OutText
    }

    DEFINE_FUNCTION CHAR[255] HexTextToArray (char HexText[765])
    {
    INTEGER Ind
    CHAR OutArray[255]
    CHAR cTMP[255]
    // LONG TMP
    CHAR TMP
    OutArray=''
    cTMP='_'//init
    FOR(Ind=1;cTMP!='';Ind++)
    {
    cTMP=REMOVE_STRING(HexText,',',1)
    TMP=HEXTOI(cTMP)
    // IF(TMP>255) pDebugMSG('HexTextToArray TMP>255',MSG_ERROR)
    OutArray[Ind]=TMP
    }
    OutArray[Ind+1]=HEXTOI(HexText)
    RETURN OutArray
    }
    #END_IF
  • whitewhite Posts: 7
    rename DebugMSG_INCLUDE.axi.axs -->DebugMSG_INCLUDE.axi
Sign In or Register to comment.