Solved

creatin and using log files

Posted on 1998-06-11
8
186 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

22 Experts available now in Live!

Get 1:1 Help Now