Page 1 of 2

Append to Log File

Posted: Tue Aug 03, 2010 2:28 pm
by melliott42
Hello,

What is the easiest way in xHarbour\HMG to append to a line to a log file?

Thanks,

Michael

Re: Append to Log File

Posted: Tue Aug 03, 2010 6:17 pm
by mol
I'm writing to log files with my InfoMssg function:

Code: Select all

------------------------------
procedure InfoMssg
	parameters info, NrLog
	private plik_id, ObRob, k, p1:="", p2:=""
	default NrLog to 1

        eol := chr(13)+chr(10)

	LogFile := cPathToLogFiles
	if NrLog = 1
		LogFile += "diary.msg"
	elseif NrLog = 2
		LogFile	+= "tango.msg"
	elseif NrLog = 3
		LogFile	+= "viking.msg"
	endif

	if !file(LogFile)
		plik_id = fcreate(LogFile,0)
	else
		plik_id = fopen(LogFile,2)
	endif
	fseek(plik_id,0,2)
	fwrite(plik_id,"---"+eol)
	fwrite(plik_id, dtoc(date())+" "+time()+;
			" Op."+str(opnr,1)+" "+alltrim(OpNaz))

	fwrite(plik_id,eol+"   "+INFO+eol)
	fclose(plik_id)
 return

Re: Append to Log File

Posted: Wed Aug 04, 2010 2:48 am
by melliott42
Marek,

Very nicely done. Thanks for posting.

Michael

Re: Append to Log File

Posted: Wed Aug 04, 2010 11:32 am
by esgici

Code: Select all

HB_MemoWrit( <cLogFileName>, MemoRead( <cLogFileName> ) + <cStringToAppend> ) 

Re: Append to Log File

Posted: Wed Aug 04, 2010 12:46 pm
by melliott42
esgici,

>> HB_MemoWrit( ... )

I love you man! :-)

Re: Append to Log File

Posted: Wed Aug 04, 2010 12:50 pm
by esgici
MemoWrit() add EOF (end of file marker = CHR( 26 ) ) at end of file, HB_MemoWrit() not.

Re: Append to Log File

Posted: Wed Aug 04, 2010 8:30 pm
by mol
I think, the problem may occurs whe the log file is too big. The way presented by Esgici causes all log file is read to memory and after that saved on disk.
Maybe my mind is determined by older DOS and Windows delimiters??? :D

Best regards, Marek

Re: Append to Log File

Posted: Wed Aug 04, 2010 9:04 pm
by danielmaximiliano
Hola melliott:
una posible solución sobre los archivos LOG es crear una aplicacion que lea la estructura del mismo, ese mismo archivo log tiene que tener una estructura pensada para poder manejar dicha informacion, como ser fecha, tipo de LOG y el LOG de datos.
al estar fechado tendrias un manejo eficiente de la informacion que guardarias en el archivo LOG busqueda por fecha, entre fechas, por tipo de LOG o datos.
ahi no tendrias problema con el tamaño del archivo LOG

Transalte Google
Hello melliott:
a possible solution on the log files is to create an application that read the same structure, same log file must have a structure designed to handle such information, such as date, type of LOG and LOG data.
the date would be effective management of the information you stored on the LOG file search by date, including dates, type of log or data.
there would have no problem with the size of log file

DaNiElMaXiMiLiAnO

Re: Append to Log File

Posted: Wed Aug 04, 2010 9:28 pm
by esgici
mol wrote:I think, the problem may occurs whe the log file is too big. The way presented by Esgici causes all log file is read to memory and after that saved on disk.
Maybe my mind is determined by older DOS and Windows delimiters??? :D
You are right.

As far as I remember, in DOS, limit of MemoRead() and MemoWrit() was 64K, because this is max length of a string.

In windows (and Harbour of course) that barriers already broken; I had tested bigger than 64K and strings in many way and didn't noticed any problem.

Surely, anything can't be limitless, and all of these considerations must have their own limits. At least, virtual memory manipulation of Windows may will be extremely slow down, though it isn't crashed.

Anyway, the file is big or not, we haven't read (take into memory) whole file for adding a single line (or some lines) at end of it ;)

In this case we have another possibility :

Code: Select all

FILEAPPEND( cSrc, cDest )
As far as I understood, this function add a file (cSrc) to (end of) another one (cDest).

So, we can do somethings like :

Code: Select all

    cTempFileName :=  "MyTempFile.log"        // or we may use hb_fsCreateTemp() or hb_fsCreateEx()

   HB_MemoWrit( cTempFileName, <cStringToAppend> )

   FILEAPPEND( cTempFileName, <cLogFileName> )

   ERASE (cTempFileName)
In fact, FILEAPPEND() use low level file functions ( FCREATE(), FOPEN(), FWRITE() ... ) like Marek. Only difference is, this is a system function; expecting it will be more secure and more fast.

In other hand, I never used FILEAPPEND(). So please use carefully, test adequately and please inform me too about your experiences :)

Regards

--

Esgici

Re: Append to Log File

Posted: Wed Aug 04, 2010 9:47 pm
by esgici
And ...

The opinion of Daniel may be good choice.

If you haven't any compelling reason to use text file format for your log file, using table based log file may be more appropriate; at least without these append matters.

Regards

--

Esgici