Append to Log File

Discuss anything else that does not suite other forums.

Moderator: Rathinagiri

melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Append to Log File

Post by melliott42 »

Hello,

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

Thanks,

Michael
User avatar
mol
Posts: 3817
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Append to Log File

Post 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
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: Append to Log File

Post by melliott42 »

Marek,

Very nicely done. Thanks for posting.

Michael
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Append to Log File

Post by esgici »

Code: Select all

HB_MemoWrit( <cLogFileName>, MemoRead( <cLogFileName> ) + <cStringToAppend> ) 
Viva INTERNATIONAL HMG :D
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: Append to Log File

Post by melliott42 »

esgici,

>> HB_MemoWrit( ... )

I love you man! :-)
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Append to Log File

Post by esgici »

MemoWrit() add EOF (end of file marker = CHR( 26 ) ) at end of file, HB_MemoWrit() not.
Viva INTERNATIONAL HMG :D
User avatar
mol
Posts: 3817
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Append to Log File

Post 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
User avatar
danielmaximiliano
Posts: 2647
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Append to Log File

Post 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
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Append to Log File

Post 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
Viva INTERNATIONAL HMG :D
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Append to Log File

Post 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
Viva INTERNATIONAL HMG :D
Post Reply