473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

fwrite for log


Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
Jan 7 '08 #1
9 2150
Hanoi wrote:
Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
Hi,

Yes, you should prepare yourself for that indeed.
Use filelocking:
http://nl2.php.net/manual/en/function.flock.php

Filelocking is reliable, even on Windows.

You'll find a basic (but working) example on that page.

Regards,
Erwin Moller
Jan 7 '08 #2
On 7 Jan, 08:36, Hanoi <hin...@inwind.itwrote:
Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
Use an asynchronous logging mechanism (see the docs page for syslog())

C.
Jan 7 '08 #3
On Jan 7, 10:36 am, Hanoi <hin...@inwind.itwrote:
Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks
If you use OS as gnu/linux, cpu will handle each process sequently.
Jan 8 '08 #4
On Jan 8, 9:03 am, Betikci Boris <pard...@gmail.comwrote:
On Jan 7, 10:36 am, Hanoi <hin...@inwind.itwrote:
Hi,
I would like to have a php file that log informations sent by an image
request.
For logging, I use fwrite to write on a file.
If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?
Thanks

If you use OS as gnu/linux, cpu will handle each process sequently.
No it won't. At the level of PHP code they will effectively be running
concurrently, not sequentially. You can have multiple processes
writing to a file, but in the absence of a mutex there is no guarantee
that one write will not occurr in the middle of a seperate write -
large writes are not atomic.

C.
Jan 8 '08 #5
If you use OS as gnu/linux, cpu will handle each process sequently.

Yes, I use a debian OS ...

So I can simply use fwrite without flock or any other asynchronous
logging mechanism?

Thanks


Jan 8 '08 #6
Betikci Boris wrote:
On Jan 7, 10:36 am, Hanoi <hin...@inwind.itwrote:
>Hi,

I would like to have a php file that log informations sent by an image
request.

For logging, I use fwrite to write on a file.

If more requests are done on the same time, there could be a lost of
data because the file is open by one process and not available for the
other processes?

Thanks

If you use OS as gnu/linux, cpu will handle each process sequently.
Incorrect. Processes are run concurrently, not sequentially. Any
interrupt (i.e. disk i/o, timer, network traffic) will cause the OS to
switch back to the system to handle the interrupt. When processing
resumes, it will be the next process in the list. That is not
necessarily the same process which was executing before the interrupt.

That's why things like mutex semaphores and file locks exist.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 8 '08 #7
Hanoi wrote:
>If you use OS as gnu/linux, cpu will handle each process sequently.


Yes, I use a debian OS ...

So I can simply use fwrite without flock or any other asynchronous
logging mechanism?

Thanks


Not safely. If it were safe, there would be no need for flock().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jan 8 '08 #8

Ok. it's clear.

Thank you very much for your answers.
Jan 10 '08 #9
Greetings, Hanoi.
In reply to Your message dated Monday, January 7, 2008, 11:36:10,
I would like to have a php file that log informations sent by an image
request.
For logging, I use fwrite to write on a file.
For the logging purposes (considering there is not much of data sent to log)
I'm using this filter function:

function ob_debugfilter($str)
{
$str = date('Y-m-d H:i:s') . " {$str}\n";

if(defined('toolDebugFilterTarget') && toolDebugFilterTarget)
{
error_log($str, 3, toolDebugFilterTarget);
$str = NULL;
}

return $str;
}

To initialize it, You should define a constant toolDebugFilterTarget with the
path to Your logfile and start output buffering in Your code.
Don't forget to use ob_flush(); at each point of log output, itherwise all
Your log output from single run will be written in a single line.

I.e.

var_export($rc); @ob_flush();
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jan 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Antoine Bloncourt | last post by:
Hello everybody Sorry to bother you but I have a problem writing datas into a file ... I want to make a backup of my MySQL database and put the result into a ..sql file. To do this, I use...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
17
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
15
by: Suraj Kurapati | last post by:
Hello, I'm having a rather strange bug with this code: for certain values of 'buf', a segmentation fault occurs when 'free(buf)' is followed by an 'fwrite()'. In the program output, there is no...
4
by: ibrahimover | last post by:
typedef struct{ char name; int no; }TAM; typedef struct{ char name; char ch; }HARF;
3
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
10
by: Sheldon | last post by:
Hi, I am trying to learn C from scratch and, though I do know how to program in Python, many things in C are hard to understand - even after reading the examples. I guess because so many...
12
by: hemant.gaur | last post by:
I have an application which writes huge number of bytes into the binary files which is just some marshalled data. int len = Data.size(); //arrary size for (int i = 0; i < len; ++i)...
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.