Tom -
I have a log-writer I use for this. It (optionally) outputs messages to a
window and
logs the messages in a file as well.
It follow my signature here if you're interested (about 183 lines).
Devon
--
Devon McCormick
^me^ at acm.
org is my
preferred e-mail
+--------------------------------+
NB.* statusWin.ijs: status window display code. Fns here are for
NB. displaying & logging text info.
NB.EG logMsg_logger_ 'This message gets written to a window and logged to
file'
NB.* txtEOL2Lns: text delimited by end-of-lines chars -> vec of text vecs.
NB.* deepDisclose: disclose enclose arrays, to simplest level->simple text
vec
NB.* getTempDir: get name of temporary directory.
NB.* pickFontSz: pick 10 or 12 point font size depending on ratio of
NB.* initStatusWin: initialize status window.
NB.* setBuffLen: set buffer length=number of lines to buffer
NB.* logMsg: log message to file and show in window. Title window by x..
NB.* initLogFl: set up log file; pick new name for today's file if none
spec'd.
NB.* initWin: initialize message window.
NB.* initLog: initialize log file if necessary.
NB.* flushLog: write all text to log file and empty the cumulative output.
NB.* closeLog: write remaining text to log file, clean up vars, close
window.
NB.* todayfile: Create unique file name based on today's date.
NB.* lastlines: return only last x. lines from message y. (text vec w/EOL
NB. Use "logMsg_logger_" to show messages in a standard window and log
NB. them to a file.
coclass 'logger'
coinsert 'base'
load 'winapi winlib dt'
DISPLAY=: 1 NB. Display messages on screen.
LINELEN=: 77 NB. Maximum length of line to display
NB.* txtEOL2Lns: text delimited by end-of-lines chars -> vec of text vecs.
txtEOL2Lns=: 3 : '<;._1 ((LF~:{.y.)#LF),y.=. (|.+./\|.y.~:LF)#y.=. y.-.CR'
deepDisclose=: 3 : 0
NB.* deepDisclose: disclose enclose arrays, to simplest level->simple text
vec
NB. with CRLF end-of-line char delimiters.
if. 0=L. y. do. ,((0~:#y.)#CRLF),"1~":y.
else.
;deepDisclose&.>y.
end.
)
NB. Don't know why I have to redefine these 2 from DHMUtils.
getTempPath=: 'GetTempPath' win32api
getTempDir=: 3 : 0
NB.* getTempDir: get name of temporary directory.
td=. >2{getTempPath 256;256$' '
td=. td{.~td i. 0{a.
)
pickFontSz=: 3 : 0
NB.* pickFontSz: pick 10 or 12 point font size depending on ratio of
NB. screen units to pixels.
scrnum=. 0.1 roundNums ({:".wd 'qchildxywhx s0')%{:".wd 'qchildxywh s0'
closest=. 0 i.~ /:|2 2.5-scrnum NB. See which ratio is closer to.
closest{12 10 NB. Closer to 2 for 12-pt, 2.5 for 10-pt.
)
initStatusWin=: 4 : 0
NB.* initStatusWin: initialize status window.
x. wdstatus y.
wd 'pmove 10 10 250 100'
wd 'setxywh s0 1 1 327 96'
fontsz=. ":pickFontSz ''
wd 'setfont s0 "Courier New" ',fontsz
scrsz=. <;._1 ' ',wd 'qm' NB. Find screen size so we can
scrsz=. ;".&.>2{.scrsz NB. shrink window to about 8x60 chars
loc=. ":scrsz-654 190 NB. and put in LR corner; 25=200-175
wd 'pmovex ',loc,' 654 170' NB. to avoid covering task bar at
bottom.
NB. wd 'qformx' NB. To find form's location and size.
)
NB. c:/amisc/code/j406/system/extras/help/dictionary/dx004.htm
NB. wd 'qformx' NB. location, size
status_close=: 3 : 0
'Log closed.' 1!:2 <'\Temp\closed.log'
closeLog ''
)
setBuffLen=: 3 : 0
NB.* setBuffLen: set buffer length=number of lines to buffer
NB. in variable CUMOUT before writing to file FNM.
if. 0={.0$y. do. BUFLEN=: y. end.
if. 0>4!:0 <'BUFLEN' do. BUFLEN=: 100 end.
)
logMsg=: 3 : 0
NB.* logMsg: log message to file and show in window. Title window by x..
'' logMsg y.
:
txt=. y. NB. Text to output; may be matrix.
x. initWin '' [ initLog ''
setBuffLen ''
if. (0=L. txt) do. NB. Change any simple array
if. (#$txt)e. 0 1 do. NB. from EOL-delimited vector to
txt=. txtEOL2Lns txt NB. encl. vec.
elseif. 2=#$txt do. NB. Simple mat to
txt=. dtsp&.><"1 txt NB. encl. text vec.
end.
else. NB. If txt is already enclosed.
end. NB. Assume encl. already OK.
CUMOUT=: CUMOUT,,txt NB. Accumulate text.
msg=. lastlines CUMOUT NB. Show only last bit that fits
if. DISPLAY do. wdstatus msg end. NB. into window.
if. BUFLEN<:#CUMOUT do. NB. Write out accumulated text?
flushLog ''
end.
)
initLogFl=: 3 : 0
NB.* initLogFl: set up log file; pick new name for today's file if none
spec'd.
if. 0>4!:0 <'FNM' do.
FNM=: todayfile y.
end.
)
initWin=: 3 : 0
NB.* initWin: initialize message window.
'' initWin y.
:
initLogFl ''
if. DISPLAY do.
if. -.('status',LF)-:wd 'qp' do. NB. Need to initialize window?
if. 0 e. $x. do. titleStr=. 'Log@',FNM
else. titleStr=. x. end.
titleStr initStatusWin ' ' NB. Avoid moving or resizing
end. NB. programmatically after
this.
end.
)
initLog=: 3 : 0
NB.* initLog: initialize log file if necessary.
initLogFl ''
if. 0>4!:0 <'CUMOUT' do. NB. Need to initialize cumulative
CUMOUT=: '[Logfile=',FNM NB. log var?
CUMOUT=: ,<CUMOUT,' at ',(showdate ''),']'
(;CUMOUT,&.><CRLF) 1!:3 <FNM
CUMOUT=: ''
end. NB. Accumulate output, last lines
if. 0=L. CUMOUT do. CUMOUT=: ,<CUMOUT end.
)
flushLog=: 3 : 0
NB.* flushLog: write all text to log file and empty the cumulative output.
initWin '' [ initLog ''
(deepDisclose CUMOUT) 1!:3 <FNM
(CRLF,~'Log flushed: ',showdate '') 1!:3 <FNM
[CUMOUT=: ''
)
closeLog=: 3 : 0
NB.* closeLog: write remaining text to log file, clean up vars, close
window.
if. 0=4!:0 <'CUMOUT' do. if. 0~:#CUMOUT do. flushLog '' end. end.
4!:55 'CUMOUT';<'FNM'
wd 'reset'
)
todayfile=: 3 : 0
NB.* todayfile: Create unique file name based on today's date.
if. 0=#y. do. y.=. 'OUT' end.
type=. 4{.(y.-.' '),4$'_' NB. 4-letter filename prefix
tmpdir=. getTempDir ''
letts=. '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_$'
dtix=. ((($letts),10)#:{.TS),1 2{TS=. qts ''
flnm=. tmpdir,type,(dtix{letts),'.LOG'
)
lastlines=: 3 : 0
NB.* lastlines: return only last x. lines from message y. (text vec w/EOL
NB. chars).
8 lastlines y. NB. 8 lines should fit in 12-pt, 175 pixels high window.
:
numlns=. -x.
if. 0=L. msg=. y. do.
msg=. txtEOL2Lns y.
end.
msg=. ;txtEOL2Lns&.>deepDisclose&.>msg
msg=. (numlns>.-#msg){.msg
lens=. ;#&.>msg
msg=. ((LINELEN<.lens){.&.>msg),&.>(LINELEN<lens)#&.><'...'
msg=. (+./\0~:;#&.>msg-.&.>' ')#msg NB. Remove leading blank lines.
msg=. (-#CRLF)}.;msg,&.><CRLF NB. Convert back to EOL-delimited
chars.
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm