Solved

creatin and using log files

Posted on 1998-06-11
8
187 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
Getting IP address 8 71
How to programmatically differentiate between C and Java 10 188
Best book to learn C++ 4 70
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now