Here's what I did:
1.) Move the CALL :get Stringlength out of the FOR loop. I put it just under the @echo off. It only needs to run once to define the macro, not every time you want to call the macro. That was my point last night, though I admit I didn't make it clear. Just make sure it's always before you enable delayed expansion and you'll be fine.
2.) Because of #1, not only do we get rid of two CALLs per FOR iteration, but we don't need the setlocal/endlocal inside the FOR either, so I deleted those. These changes should greatly improve the speed.
* At this point the program "ran" fine, but gave "missing operand" errors and output only ""s *3.) Missing operaand is a dead giveaway for a set /A statement problem, so I changed the %%'s to !!'s and that error went away.
4.) Outputting ""s leads me to believe your print statements were outputting null, so again I changed the %%'s to !!'s and that seems to have fixed it. I now get an output log that looks like what you want, with padding before the location.
Code: Select all
"07/23/13 10:16:00am Service [stunnel-sslh] connected remote server from: c-24-12-152-129.hsd1.il.comcast.net [24.12.152.129] - United States of America"
"07/23/13 10:17:53am Service [stunnel-sslh] accepted connection from: c-71-194-51-232.hsd1.il.comcast.net [71.194.51.232] - United States of America"
"07/23/13 10:17:53am connect_blocking: connected: c-24-12-152-129.hsd1.il.comcast.net [24.12.152.129] - United States of America"
"07/23/13 10:17:53am Service [stunnel-sslh] connected remote server from: c-24-12-152-129.hsd1.il.comcast.net [24.12.152.129] - United States of America"
"07/23/13 10:17:53am Service [stunnel-sslh] accepted connection from: c-71-194-51-232.hsd1.il.comcast.net [71.194.51.232] - United States of America"
"07/23/13 10:17:53am Service [stunnel-sslh] accepted connection from: c-71-194-51-232.hsd1.il.comcast.net [71.194.51.232] - United States of America"
"07/23/13 10:17:53am Service [stunnel-sslh] accepted connection from: c-71-194-51-232.hsd1.il.comcast.net [71.194.51.232] - United States of America"
A few other minor changes I wanted to point out:
5.) In the IF DEFINED skippadding block I added a 'set skippadding=' to "reset" the flag. So you check the length, set the flag if it's longer than 170, run whichever code block based on the flag, but then reset the flag if it's set, so it doesn't stay set forever. BTW this use of a flag to determine code path vs. labels and GOTOs is just something I've been doing for years so it's old habit. I don't know if it's the best, or even a good way, but it does work for me. I just don't want to be accused of teaching you bad coding habits
6.) Another old habit I don't know whether it's good or bad: I changed your 'CALL set' to a FOR loop in the NOT DEFINED skippadding block. This is just a method I came up with when I need something like !VAR[!index!]! inside a FOR loop, I let a second FOR expand the index as %%b and then get !VAR[%%b]!. I believe this will be faster than a 'CALL set', but it accomplishes the same thing. If I'm off base in my belief, or if there's an even better way, I'd love to learn it. (penpen?)
7.) I wasn't sure what you were trying to do with the ELSE before the 'echo print >>log'. The way I was reading the logic it was saying If skippadding is defined do this, otherwise print to the log. I assume you wanted all lines printed, whether you padded them or not, so I removed the ELSE. I looked above to see if it belonged to an earlier code block, but found nothing to tie it to...
Here's the final code:
Code: Select all
@ECHO OFF
call :getStringLength
setlocal enableDelayedExpansion
set "log=stunnel.log"
set "logx=stunnelx.log"
for /f "tokens=1-3* delims= " %%a in ('findstr /V /I /C:"127.0.0" /C:"192.168" /C:"byte"
/C:"WSAECONNRESET" /C:"Peer suddenly disconnected" "%log%"') do (
set "dateValue=%%a"
set "dateValue=!dateValue:~5,2!/!dateValue:~8,2!/!dateValue:~2,2!"
for /F "tokens=1* delims=:" %%e in ("%%b") do (
set /A "timeValue=%%e"
if !timeValue! == 0 (
set "timeValue=12:%%fam"
) else if !timeValue! Lss 12 (
set "timeValue=%%e:%%fam"
) else if !timeValue! == 12 (
set "timeValue=12:%%fpm"
) else (
set /A "timeValue-=12"
set "timeValue=0!timeValue!"
set "timeValue=!timeValue:~-2!:%%fpm"
)
)
set "info=%%d"
set "ipPort="
for %%e in (!info!) do set "ipPort=%%e"
for /F "tokens=1,2 delims=:" %%f in ("!ipPort!") do (
if not defined IP[%%f] for /f "tokens=2 delims= " %%h in ('nslookup %%f 2^> nul ^| findstr
/I /C:"Name" ') do set "IP[%%f]= %%h"
if not defined IP[%%f] set "IP[%%f]= "
set "host=!IP[%%f]:~1!"
if defined host (
set "info=!info: %%f:%%g=!: !host! [%%f]"
) else (
set "info=!info: %%f:%%g=!: [%%f]"
)
)
set "location=- United States of America"
::::::::::::::::::::: Padding Code :::::::::::::::::::::
set "padding=
"
set "info=!dateValue! !timeValue! !info!"
set "info.length="
set "location.length="
%$strlen% "info.length" "info"
%$strlen% "location.length" "location"
set /A "min=!location.length!+!info.length!"
if !min! GEQ 170 set skippadding=1
IF NOT DEFINED skippadding (
set "print=!padding!!location!"
set "print=!print:~-170!"
FOR %%b IN (!info.length!) DO (
set "print=!info!!print:~%%b!"
)
)
IF DEFINED skippadding (
set "print=!info!!location!"
set skippadding=
)
echo("!print!" >> "%logx%"
)
GOTO:EOF
:getStringLength
set LF=^
::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
:::: StrLen pResult pString
set $strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
for /F "tokens=1,2 delims=, " %%1 in ("!argv!") do (%\n%
set "str=A!%%~2!"%\n%
set "len=0"%\n%
for /l %%A in (12,-1,0) do (%\n%
set /a "len|=1<<%%A"%\n%
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"%\n%
)%\n%
for %%v in (!len!) do endlocal^&if "%%~b" neq "" (set "%%~1=%%v") else echo %%v%\n%
) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,
exit /b