Solved

creatin and using log files

Posted on 1998-06-11
8
189 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ getting the first 10 characters of a char* string 11 99
What is sub-make ? 2 71
VS2015 Redefinition errors 4 70
How to Correctly derive class from CWinThread in MFC 4 66
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
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.

828 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