Solved

creatin and using log files

Posted on 1998-06-11
8
190 Views
Last Modified: 2010-04-10
I want to create a log file of some events that take place in my program.
I would like to be able to write to it and then retrieve parts of the data as and when I need it at run time. At the moment I dont no where to start.

eg. of log file:
USER - DATE/TIME - AGE - FAVORITECOLOUR    etc

Then If possible, at runtime, to arrange these in a list according to there category.
I am using Borland 4.52 and just the API not OWL.
Thanks for any help.
0
Comment
Question by:dsch
8 Comments
 
LVL 4

Expert Comment

by:sganta
ID: 1165709
Hi !

To Create your own log file, by giving the log file name as "filename.log" then
do the following :

Suppose let us assume that your code is like this :

#include <iostream.h>
#include <fstream.h>

---

---

void main(int argc,*argv[])
{
   /* You know that the argv[0] will contain the name of this file */
   char *logfile;

   strcpy(logfile,argv[0]); // Copy the file name into logfile
   strcat(logfile,".log"); //Concatenate the logfile with .log

   Now you open this file in write mode and
   write it whatever you want, using fstream funtions


This works file. I hope this is acceptable to you.

JESUS LOVES YOU - sganta
0
 
LVL 6

Expert Comment

by:alamo
ID: 1165710
sganta, do you ever actually test the code you post and claim to test?

On Q10057177, you said "I've checked my earlier solution but it works fine. " when in fact it did not work.

Here, you say "this works fine" when in fact it has an obvious bug that will crash the program (though a compiler warning should alert dsch).

If in fact your code is testing Ok for you, then at the very least you need to change the way you test.
0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1165711
How much overhead are you wiling to put up with?

One way which is quite neat is to provide a DBGPrintf(int flag, chat * fmt, ...) which, depending on flag, printf-s to the logfile. (If you're going to go to this much trouble, might as well make it run-time configurable, using and env-var a cmd-line parameter). As for sorting it, use sort?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1165712
How much overhead are you wiling to put up with?

One way which is quite neat is to provide a DBGPrintf(int flag, chat * fmt, ...) which, depending on flag, printf-s to the logfile. (If you're going to go to this much trouble, might as well make it run-time configurable, using and env-var a cmd-line parameter). As for sorting it, use sort?
0
 
LVL 1

Author Comment

by:dsch
ID: 1165713
To sprinkmeier:
I cant find any info on  DBGPrintf(), is this used in Borland 4.52. Remember I am reasonably new to all this.
Speak to you soon.

to sganta:
iostream functions wont work properly under windows. I mentioned that I am using the API. Thanks anyway.
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1165714
Who told you that iostream functions won't work properly under Windows?  As a matter of fact, they work perfectly well.  All you need to do is open your log file in append mode, write your log data to it, and close it.  You can use iostream, standard C file IO stuff, or Windows API file access functions.  Whatever you prefer.

Here is an example using C functions:

#include <stdio.h>

void LogData(char *l);

int main()
{
      printf("Program starting...\n");
      printf("Logging something\n");
      LogData("This is a log entry\n");
      printf("Program Ending...\n");
      return 0;
}

void LogData(char *l)
{
      FILE *fp;
      fp = fopen("./log.txt", "a");
      if(fp == NULL)
            return;

      fputs(l, fp);
      fclose(fp);
}
0
 
LVL 1

Author Comment

by:dsch
ID: 1165715
jhance: What I meant was the #include <iostream.h> functions like cin & cout have always crashed my computor when I tried to use them under windows. I will try what you say. Thanks for all your help.
0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1165716
Oops, sorry about that... I should have mentioned that you hae to write that one yourself.

I usually do something like test the flag provided agains a runtime option, decide wether to print it or not, then either return immediatly or print out the who/what/where/why information followed by the user supplied (fmt, ...) (using varargs and the vaprintf functions).

Works quite well, little RT overhead (unless you're actually printing) but like I said, takes a bit of work.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 153
show out valin of json for debugging in visaul c++ 1 48
How do i run a c++ file? 15 52
I could not set window to top 4 45
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question