Jump to content

Batch to Log & Console


Recommended Posts

Hello

I'm trying to add a log export ability to my Run on first boot batch file after an XP silent installation. I want to know if everything went fine (or not) so I can tweak further and for reference as well.

I really don't understand batch much, not at least at this level. I found this example that renders out everything shown in the console window, which is fine, but in return now the console is blank.

@ECHO ON
call :Logit>>C:\mylog.log 2>&1
exit /b 0
:Logit

::Your bat code here

Then the following code examples IIRC were the opposite, show everything on the console and log only the commands you attach /log flag to.

That's what I tried, but I got no log. Do I need to add something else somewhere, what am I missing?

@echo off
if /I ?%1? NEQ ?/log? goto :LogIt
set LogCmd=%*
echo on
call %0 %LogCmd:/log=% >"%~dpn0.log" 2>&1
set LogCmd=
goto :eof
:LogIt
REM the start of your batch file

Thank you!

Edited by Dogway
Link to comment
Share on other sites


I changed the line but nothing happens. I have the log code first, then the variables, then the below command.

%ENCODER% --level 4.1 --preset %PRESET% --tune %TUNE% --pass 1 --bitrate %BITRATE% --stats "%STATS%" --me %ME% --deblock -2:-1 --keyint %KEY% --min-keyint %MKEY% --frames %FRAMES% --demuxer %DEMUX% --vbv-bufsize 24000 --no-fast-pskip --sar %SAR% --aq-strength 1.2 --vbv-maxrate 24000 --fps %FPS% --output NUL "%INPUT%" /log

Link to comment
Share on other sites

I would suggest something along these lines:

MyScript.cmd

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "_OUT=CALL :SUB"
FOR /F "DELIMS=/-" %%# IN ("%*") DO (
IF /I %%# EQU L SET "_OUT=(%_OUT%)>MYLOG.LOG 2>&1")
%_OUT%
GOTO :EOF
:SUB
your command(s) here

This means that your script will accept L, l, /L, /l, -L or -l as parameters for logging the output to file. anything else should log the output to the console window.

for this to work your script should have no spaces in its file name or at least be quoted

Link to comment
Share on other sites

Thanks a lot Yzöwl, but again no log is output, here is my script, did I miss something? (edit: my bat is called hey.bat so no spaces)

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "_OUT=CALL :SUB"
FOR /F "DELIMS=/-" %%# IN ("%*") DO (
IF /I %%# EQU L SET "_OUT=(%_OUT%)>MYLOG.LOG 2>&1")
%_OUT%
GOTO :EOF
:SUB

::Your bat code here
D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs /L

jaclaz, that's a bit over my head, at least for the time being, I don't discard to have a deeper look later on.

Link to comment
Share on other sites

that is because you asked for the information to be output to a file.

If you want it to be logged to two places simultaneously then you need to tee it, as jaclaz has already indicated.

You can either use a 3rd party utility for that purpose or possibly this vbscript.

tee.vbs

set con = createobject("scripting.filesystemobject").opentextfile("con:", 2)
do until wsh.stdin.atendofstream
s = wsh.stdin.readline
wsh.echo s : con.writeline s
loop

to use it you'd probably just pipe your script into it.

hey.cmd

@D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs

Then run it like this:

hey.cmd | cscript tee.vbs >mylog.log

Link to comment
Share on other sites

Woow, indeed I'm not asking something easy. Thanks a bunch for the help, the log is blank, well I get this:

Microsoft (R) Windows Script Host versi¢n 5.7
Copyright (C) Microsoft Corporation 1996-2006. Reservados todos los derechos.

but I see the point.

Yes, the title is misleading but in my OP I made clear I wanted to output to both, console and log, at the same time.

EDIT:

Now I'm using jaclaz's suggestion. In the same vein:

Downloaded Tee.bat and make the call as:

hey.cmd  ¦  TEE.BAT  [ -a ]  C:\OEM\mylog.log

but I don't get any log. Should be the bat encoded in some format? different from ANSI? is the ¦ character fine.

Edited by Dogway
Link to comment
Share on other sites

Ok, for the second alternative (using TEE.BAT) I managed to output something.

I had to add > before the log filename, and also remove the @ before the command in the bat file.

But, all I get in the log is the question, my input command:

D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs

and not my answer (or Q&A together):

C:\OEM>D:\Programas\VIDEO\x264-2245.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs 
avs [info]: 720x480p 0:0 @ 30000/1001 fps (cfr)
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.1 Cache64
x264 [info]: profile Main, level 4.1
[1.0%] 1/101 frames, 1.10 fps, 2075.60 kb/s, eta 0:01:30
[14.9%] 15/101 frames, 12.98 fps, 1109.24 kb/s, eta 0:00:06
[34.7%] 35/101 frames, 24.89 fps, 1600.93 kb/s, eta 0:00:02
[74.3%] 75/101 frames, 45.29 fps, 1476.59 kb/s, eta 0:00:00

x264 [info]: frame I:1 Avg QP:21.54 size: 8657
x264 [info]: frame P:29 Avg QP:18.16 size: 9658
x264 [info]: frame B:71 Avg QP:18.83 size: 4739
x264 [info]: consecutive B-frames: 2.0% 2.0% 32.7% 63.4%
x264 [info]: mb I I16..4: 87.7% 0.0% 12.3%
x264 [info]: mb P I16..4: 59.9% 0.0% 0.0% P16..4: 32.6% 0.0% 0.0% 0.0% 0.0% skip: 7.6%
x264 [info]: mb B I16..4: 18.1% 0.0% 0.0% B16..8: 31.6% 0.0% 0.0% direct:29.6% skip:20.7% L0:42.6% L1:43.7% BI:13.7%
x264 [info]: final ratefactor: 18.81
x264 [info]: direct mvs spatial:97.2% temporal:2.8%
x264 [info]: coded y,uvDC,uvAC intra: 45.7% 62.9% 8.7% inter: 19.3% 46.7% 0.5%
x264 [info]: i16 v,h,dc,p: 37% 28% 28% 7%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 11% 23% 24% 7% 6% 4% 8% 5% 12%
x264 [info]: i8c dc,h,v,p: 53% 23% 23% 2%
x264 [info]: Weighted P-Frames: Y:0.0% UV:0.0%
x264 [info]: kb/s:1484.21

encoded 101 frames, 56.20 fps, 1484.21 kb/s

Link to comment
Share on other sites

x264 encoder use the error channel for its normal output so if you need to log its output, you'll need to add "1>NUL 2>%temp%\x264_encoding.log" for example to the line launching it.

For the second point as you need the output in the console and in the log file, you could try to launch x264 with log (as explained before) as background process and then print the log with "tail -f" (from the unix tools).

So something like this will work:

start /B /separate "encoding" x264.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL Copia.avs 1>NUL 2>%temp%\log_x264.log
tail.exe -f %temp%\log_x264.log

As tail -f will wait forever, and as the --pid option doesn't work in this dos port (and some newer mingw32 port), you'll have to interrupt it with a control+C after its ending or kill tail process. This lastest option might be automated if you don't run many x264 process at the same time as batch doesn't provide any simple way of getting the pid of a launched process.

Edit1: as a side note, doing this with a higher programming language (C++, VB, vbs, autoit ....) will be a lot easier.

Edit2:

In batch using third party tools - unix tools sleep, tail, gawk, wc and sysinternals handle -, i was able to make it work:


@echo off
start /B /separate "encoding" x264.exe --level 4.1 --preset slower --tune film --pass 1 --bitrate 1500 --deblock -2:-1 --keyint 300 --min-keyint 30 --aq-strength 1.2 --fps 29.970 --output NUL E:\encodage\dimitri.avs 1>NUL 2>%temp%\log_x264.log
start /B /separate "logging" d:\utils\command\tail.exe -f %temp%\log_x264.log
setlocal
:wait
set ended=0
for /f "usebackq" %%i in (`handle %temp%\log_x264.log ^|findstr /i "pid:" ^|wc -l`) do (if "%%i"=="1" set ended=1)
if "%ended%"=="1" goto killpid
sleep 1
goto wait
:killpid
handle %temp%\log_x264.log |findstr /i "pid:" |gawk -F"pid:" "{print $2}" |gawk "{print $1}" >%temp%\tail_pid.txt
for /F %%i in (%temp%\tail_pid.txt) do (taskkill /pid %%i /t /f >NUL)
:end
endlocal
pause

Edited by allen2
Link to comment
Share on other sites

Wonderful, this almost feel like hacking!

I think using x264 wasn't the best example since it's only for my tests. I will use the logging for the "Install at first Boot" batch of an OS install, so I don't think I will have the described problems.

Most likely I will get along with the TEE.BAT route.

I also used your first example with tail.exe. It exported the answer (error log), I guess this is all I want to know (discarding the call line). Also I can anyways run a kill process command for tail.exe.

So I should always append the next for any of my commands right?

start /B /separate (mycommands+flags) 1>NUL 2>%temp%\log_x264.log
start /B /separate (mycommands+flags) 1>NUL 2>%temp%\log_x264.log
start /B /separate (mycommands+flags) 1>NUL 2>%temp%\log_x264.log
...

I couldn't run your second example, I got a BSOD and it scared the hell out of me. I think with all these example it's more than enough.

Edited by Dogway
Link to comment
Share on other sites

So I should always append the next for any of my commands right?

No.

You need to understand redirection (and the concepts of "standard output" and "standard error" first:

http://www.robvanderwoude.com/redirection.php

Every program might "decide" to write *something* to standard output and *something else* to standard error, so you should append BOTH standard output and standard error to the log:

command >> file 2>&1

jaclaz

Link to comment
Share on other sites

I see.

This?

start /B /separate (mycommands+flags) >> %temp%\log_x264.log 2>&1
start /B /separate (mycommands+flags) >> %temp%\log_x264.log 2>&1
...

I guess it was ignoring call line because I was writing 1>NUL

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...