Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

creatin and using log files

Posted on 1998-06-11
8
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

604 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