Announcement

Collapse
No announcement yet.

command line tool to log messages into a log file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • command line tool to log messages into a log file

    i could not find a simple a simple logger so here is one i created.
    i was not interested in logging to windows os event log.

    you can log to any file provided it is given on the command line
    it does not create any directorys and it will timeout if it cannot open or create a file after so many tries.
    it opens a file for exclusive use while logging then closes it after the log is written, but it will try for 200 times with 30 microseconds pauses between each try, maybe that will be enough for multi users if the demands for the log file are not too high or often.

    program has a default log message and default log file name but can easily be patched to something else.

    any negative comments would be welcomed now before it hits the source code section
    there is always that one last change, you know what i mean!


    logitnow.bas


    Code:
    REM logitnow.bas
    REM compiled with pbcc 5.02
    REM this should compile with pbcc 4.0? too
    REM program is to log a message from the comamnd line
    REM a file name can be placed on the command line
    REM or the default file name logitnow.log is used
    
    
    
    #COMPILE EXE
    #DIM ALL
    REM $SQ equate is not in powerbasic versions before pbcc 5.00 or pbwin 9.0
    
    GLOBAL gssinglequote AS STRING
    REM function to provide day of week
    
    FUNCTION AstroDay(year AS LONG, month AS LONG, day AS LONG) AS LONG
        DIM y AS DOUBLE
        y = year + (month - 2.85) / 12
        AstroDay = INT(INT(INT(367 * y) - 1.75 * INT(y) + day) -0.75 * INT(0.01 * y)) + 1721116
    END FUNCTION
    
    REM removes single and double quotes from a string
    FUNCTION removequotes(A AS STRING) AS STRING
    removethemquotes:
    a=TRIM$(a)
    REPLACE $DQ WITH "" IN a
    REPLACE gssinglequote WITH "" IN a
    IF INSTR(a,$DQ) THEN GOTO removethemquotes
    IF INSTR(a,gssinglequote) THEN GOTO removethemquotes
    a=TRIM$(a)
    FUNCTION=a
    END FUNCTION
    
    REM removes double spaces from a string
    FUNCTION removedoublespaces(A AS STRING) AS STRING
    removethemdoublespaces:
    a=TRIM$(a)
    REPLACE "  " WITH " " IN a
    REPLACE gssinglequote WITH "" IN a
    IF INSTR(a,"  ") THEN GOTO removethemdoublespaces
    a=TRIM$(a)
    FUNCTION=a
    END FUNCTION
    
    REM writes to a log file placed on the command line or the default file name "logitnow.log"
    REM writes date,time,computername,domainname,username, then a note if placed on the command line
    REM if no note is placed on the command line, the note of "no msg" is used
    REM program will try to open log file up to 200 times with 30 microseconds waits between tries then abort
    
    FUNCTION logit(smessage AS STRING,slogfilename AS STRING) AS LONG
    DIM scomputername AS STRING
    DIM susername AS STRING
    DIM suserdomain AS STRING
    DIM stime AS STRING
    DIM sdate AS STRING
    DIM sdayofweek AS STRING
    DIM idate AS STRING
    DIM slogline AS STRING
    DIM i AS LONG
    
    sdate=DATE$
    stime=TIME$
    scomputername=ENVIRON$("COMPUTERNAME")
    susername=ENVIRON$("USERNAME")
    suserdomain=ENVIRON$("USERDOMAIN")
    
    SELECT CASE (AstroDay(VAL(MID$(DATE$,7,4)),VAL(MID$(DATE$,1,2)),VAL(MID$(DATE$,4,2))) MOD 7)
             CASE 0: sdayofweek="Sun"
             CASE 1: sdayofweek="Mon"
             CASE 2: sdayofweek="Tue"
             CASE 3: sdayofweek="Wed"
             CASE 4: sdayofweek="Thu"
             CASE 5: sdayofweek="Fri"
             CASE 6: sdayofweek="Sat"
    END SELECT
    
    
    smessage=TRIM$(smessage)
    IF LEN(smessage)=0 THEN
        smessage=TRIM$("no msg                                                                                                           ")
        ELSE
        smessage=removedoublespaces(smessage)
    END IF
    
    slogline=MID$(DATE$,7,4)+"-"+ MID$(DATE$,1,2)+"-"+MID$(DATE$,4,2)+" "+stime+" "+sdayofweek+" ["+scomputername+":"+suserdomain+":"+susername+"] "+smessage
    
    slogfilename=TRIM$(slogfilename)
    IF LEN(slogfilename)=0 THEN
        slogfilename=TRIM$("logitnow.log                                                                                                                           ")
    END IF
    
    i=0&
    recordinlogfile:
    INCR i
    TRY
    OPEN slogfilename FOR APPEND AS #1
    CATCH
    IF i=200 THEN EXIT FUNCTION
    SLEEP 30
    GOTO recordinlogfile
    END TRY
    PRINT #1,USING$("&",slogline)
    CLOSE #1
    END FUNCTION
    
    
    
    
    
    FUNCTION PBMAIN () AS LONG
    DIM i AS LONG,j AS LONG
    DIM scommand AS STRING
    DIM sdefaultfilename AS STRING
    DIM sfilename AS STRING
    DIM sendoffilenamemarker AS STRING
    
    scommand=TRIM$(COMMAND$)
    
    REM assign the global string variable gssinglequote to asc 39 the ' single quote character
    REM $DQ is a global string variable preassigned  to asc 34 the " double quote character
    gssinglequote=CHR$(39)
    
    IF LEN(scommand)=0 THEN
        logit(scommand,sfilename)
        GOTO exittheprogram
    END IF
    scommand=" "+scommand+" "
    
    IF INSTR(scommand,"?") THEN
        STDOUT "This prgram was created to log messages to a file"
        STDOUT "It logs date,time,computername,domain,username"
        STDOUT "and a message given on the command line."
        STDOUT "IF no message is given, then -no msg- is placed in the log file."
        STDOUT "To change the default log file name from logitlog.log, use"
        STDOUT "/F immediately before a filename on the command line, quotes are permitted."
        STDOUT "If this program cannot create or open the log file within 200 tries"
        STDOUT "with a 30 microseconds pause in between each try, it will timeout."
        STDOUT "This program can be patched to change the default log name."
        STDOUT "examples:"
        STDOUT "logitnow booting up the computer"
        STDOUT "logitnow /f"+$DQ+"c:\program files\logdirectory\bootup.log"+$DQ+"  booting up the computer"
        STDOUT "logitnow /fc:\logdirectory\bootup.log  booting up the computer"
        STDOUT "This program will not create any directory that does not already exist."
        GOTO exittheprogram
    END IF
    
    
    REM if a filename was placed on the command line, parse it out of the command line
    REM then remove the filename with the /F from from the command line
    REM the parameter on the comamd line is "/F"
    REM the filename can be in double or single quotes
    REM the filename must immediately follow the /f
    
    i=INSTR(UCASE$(scommand),"/F")
    IF I THEN
       sendoffilenamemarker=" "
       IF MID$(scommand,i+2,1)=$DQ THEN sendoffilenamemarker=$DQ
       IF MID$(scommand,i+2,1)=gssinglequote THEN sendoffilenamemarker=gssinglequote
       IF sendoffilenamemarker<>" " THEN
          j=INSTR(i+3,scommand,sendoffilenamemarker)
         ELSE
          j=INSTR(i+2,scommand,sendoffilenamemarker)
       END IF
    sfilename=MID$(scommand,i,j-i+1)
    REPLACE sfilename WITH " " IN scommand
    sfilename=removequotes(sfilename)+" "
    IF LEFT$(UCASE$(sfilename),2)="/F" THEN sfilename=RIGHT$(sfilename,(LEN(sfilename)-2))
    sfilename=TRIM$(sfilename)
    END IF
    
    
    scommand=TRIM$(scommand)
    
    IF LEN(scommand)=0 THEN
        logit(scommand,sfilename)
        GOTO exittheprogram
    END IF
    
    scommand=removequotes(scommand)
    logit(scommand,sfilename)
    
    exittheprogram:
    END FUNCTION
    Attached Files
    Last edited by Paul Purvis; 13 Nov 2009, 09:46 PM.
    p purvis

  • #2
    program is now compiled with pbcc 5.0 in case pbcc 4.0 version has problems with vista or windows 7
    but the program still compiles with pbcc 4.0

    also added the day of week stamp in the log file so you do not have look at a calendar.
    p purvis

    Comment

    Working...
    X