A
arnuld
I am trying to write a program that creates and renames log file on the
system. I have created the initial design ( which of course is not
compilable). Just asking whether this is the right way to do it in C:
/* The Logging Program
* ASSUMPTION: Incoming data (or log) will always be less than LOG_SIZE.
* We will create log files, not more than LOG_CNT_MAX in number. First file will be 0.log.
* Each logfile will have size <= LOG_SIZE. If adding new data makes it larger than LOG_SIZE,
* then we will simply create a new file and rename the old ones. see next paragraph for
* renaming scheme.
* If we will have some logs (say 6 files) already in the system, from 0.log to 5.log, then program
* will preserve them by renaming each file, from 0.log to 1.log, 1.log to 2.log and finally 5.log
* to 6.log. Hence 0.log will always be the latest file
* when LOG_CNT_MAX has reached, then there will be no increment to log count, we will just delete the
* oldest file which is with highest number (LOG_CNT_MAX - 1) and rename each file like we did in
* previous paragraph
*
*
* NOTES:
* File will be opened and closed for 1 time only, it could be written 10 times. It means
* after opening and writing, we will not close the file there. It will remain open. We will
* close it only when writing new data increases its size beyond LOG_SIZE. e.g. If writing
* data 10 times does not reache the alloted limit of LOG_SIZE but 11th write does, then file
* be closed after 11th write.
*
* The above explanation means, we need to have global file pointer always pointing to
* current file being written to. or you have some other better idea.
*
*
* VERSION 1.0 ,
*
* Trying to conform to C90.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define LOG_DIR "/home/arnuld/programs/CLS/"
#define LOG_NAME_FORMAT "%d.log"
enum { LOG_SIZE = 10,
LOG_PATH_SIZE = 50,
LOG_CNT_MAX = 6,
};
int main( void )
{
int log_cnt;
log_cnt = check_for_archives( .. );
start_logging( ... )
return 0;
}
void start_logging( ... )
{
int i;
for (i = 0; i != 3; ++i )
{
if( log_cnt )
{
write_datacnt(... );
}
else
{
write_data_first(..);
}
}
void write_data_cnt( .. )
{
if( log_cnt > LOG_CNT_MAX )
{
write_data_max(..);
}
else
{
write_data_notmax( ... );
}
}
void write_date_first( ... )
{
create_first_log(..);
}
system. I have created the initial design ( which of course is not
compilable). Just asking whether this is the right way to do it in C:
/* The Logging Program
* ASSUMPTION: Incoming data (or log) will always be less than LOG_SIZE.
* We will create log files, not more than LOG_CNT_MAX in number. First file will be 0.log.
* Each logfile will have size <= LOG_SIZE. If adding new data makes it larger than LOG_SIZE,
* then we will simply create a new file and rename the old ones. see next paragraph for
* renaming scheme.
* If we will have some logs (say 6 files) already in the system, from 0.log to 5.log, then program
* will preserve them by renaming each file, from 0.log to 1.log, 1.log to 2.log and finally 5.log
* to 6.log. Hence 0.log will always be the latest file
* when LOG_CNT_MAX has reached, then there will be no increment to log count, we will just delete the
* oldest file which is with highest number (LOG_CNT_MAX - 1) and rename each file like we did in
* previous paragraph
*
*
* NOTES:
* File will be opened and closed for 1 time only, it could be written 10 times. It means
* after opening and writing, we will not close the file there. It will remain open. We will
* close it only when writing new data increases its size beyond LOG_SIZE. e.g. If writing
* data 10 times does not reache the alloted limit of LOG_SIZE but 11th write does, then file
* be closed after 11th write.
*
* The above explanation means, we need to have global file pointer always pointing to
* current file being written to. or you have some other better idea.
*
*
* VERSION 1.0 ,
*
* Trying to conform to C90.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define LOG_DIR "/home/arnuld/programs/CLS/"
#define LOG_NAME_FORMAT "%d.log"
enum { LOG_SIZE = 10,
LOG_PATH_SIZE = 50,
LOG_CNT_MAX = 6,
};
int main( void )
{
int log_cnt;
log_cnt = check_for_archives( .. );
start_logging( ... )
return 0;
}
void start_logging( ... )
{
int i;
for (i = 0; i != 3; ++i )
{
if( log_cnt )
{
write_datacnt(... );
}
else
{
write_data_first(..);
}
}
void write_data_cnt( .. )
{
if( log_cnt > LOG_CNT_MAX )
{
write_data_max(..);
}
else
{
write_data_notmax( ... );
}
}
void write_date_first( ... )
{
create_first_log(..);
}